I use writeStruct() and takeStruct() to write / read things to / from a file.
Is there an (easy) way - without duplicating logical code - to use this mechanism for write to / read from memory?
Io.Writer.fixed and Io.Reader.fixed?
You probably want std.Io.Writer.Allocating, but yeah std.Io.Reader.fixed.
Ok sounds good.
And when ‘choosing’ between file or mem I could point a std.io.Writer or std.io.Reader to the correct implementation?
I would expect you to have saving/loading functions which take *std.Io.Writer/*std.Io.Reader. Then the code which calls those functions creates the actual writer/reader/file. So probably a “save to file”, and a “save to bytes” function; the same for loading. How you actually wire things depends on the larger context of the program, but that’s a first approximation.
Ok thanks! I will manage it with this info.
There’s a little sample here, only for writing, though; the whole top of the doc is file-centric, but I got tired of that and decided on a memory-rather-than-file destination for the Writer demonstration. I think it’s pretty easy to see, from that, how to go both ways. Of interest, perhaps: when the writer is .fixed(&buf) (memory destination), there is no need to .flush(), of course. It’s a no-op (but may be best left in the code so that when you swap a file in, rather than memory, all should still work).