mpack icon indicating copy to clipboard operation
mpack copied to clipboard

File IO incompatible with custom context

Open Dolkar opened this issue 2 months ago • 2 comments

All of the functions that operate on files, such as mpack_reader_init_filename store the file handle in the custom context. This means that the context can't be set by the user to anything else, like for use in error handlers.

I feel like either the file IO helpers should use their own, separate context, or it should be documented that calling mpack_reader_set_context and similar is not allowed when file IO is used.

Dolkar avatar Apr 21 '24 12:04 Dolkar

The only purpose of the context is for callbacks like fill, flush, etc. mpack_reader_init_filename() installs its own fill and destroy callbacks so it uses the context. The documentation of mpack_reader_set_context() does indeed say that the context is for callbacks.

I suppose I could clarify the documentation of functions like mpack_reader_init_filename() to say that they install callbacks and use the context so you can't use your own callbacks and context with them.

Is the problem that you are trying to use an error callback with your own context? It occurred to me that the reader doesn't install an error callback; that's the only reason I can think of to want to use your own context. In that case you're probably right, and I should make these functions use their own context.

As a workaround you could wrap the reader in a struct with your context. You could then cast a pointer from the reader to the containing struct.

ludocode avatar Apr 24 '24 00:04 ludocode

Yes, my intention was to have error callbacks with my own context while reading out of a file. I suppose you're right that there isn't much of a need to set your own context otherwise, so apologies for overgeneralizing the problem.

Dolkar avatar Apr 24 '24 10:04 Dolkar