not-fl3 / nanoserde

Serialisation library with zero dependencies
687 stars 39 forks source link

Add ser/deser from Read/Write #23

Open Yamakaky opened 3 years ago

Yamakaky commented 3 years ago

For the binary serialization especially, it would be nice to be able to serde using the standard Read and Write traits instead of using Vec and slice.

I'm working on interaction between rust and wasm (using wasmer). On the host, I first have to serialize the value to Vec then copy it to wasm. Same thing for deser. With custom Read/Write types, I could do it in one pass.

One other advantage is that you could do streaming deserialization with custom parsers, for example if you want to add all the u32 in a very big file with low constant memory usage.

It can be added as a backward compatible method since Vec is Read + Write. Then, in a future version, the Vec version can be dropped.

Yamakaky commented 3 years ago

Example of what it could look like: https://github.com/not-fl3/nanoserde/compare/master...Yamakaky:read-write

The Result<Result<>> is ugly, it would be merged in one Error type in a final version.

Yamakaky commented 3 years ago

Would you be willing to merge such feature? If not, I won't bother putting it in order.

not-fl3 commented 3 years ago

Sorry for such a delayed response on this.

I do agree that this is a great feature that would be nice to have, but I am not quite sure about the proposed API.

Yamakaky commented 3 years ago

Do you mean the Result<Result<>>?

ser_bin and de_bin could be put as free functions unless we find a usecase where the default implementation could be optimized.