dtolnay / request-for-implementation

Crates that don't exist, but should
610 stars 6 forks source link

Derive macro to compute Bincode size of type #26

Closed dtolnay closed 5 years ago

dtolnay commented 5 years ago

From https://github.com/TyOverby/bincode/issues/263:

This would be helpful speeding up serialization/deserialization because we can preallocate correctly sized buffers independent of the concrete value being processed.

This would be its own derive macro:

#[derive(Serialize, Deserialize, BincodeSize)]
struct S {
    /* ... */
}

together with a simple trait implemented by the macro:

trait BincodeSize {
    fn bincode_size_min() -> usize;
    fn bincode_size_max() -> Option<usize>;
}

Types containing variably sized data like a String or Vec would have a minimum size but no max size.

dtolnay commented 5 years ago

FYI @jrmuizel

jrmuizel commented 5 years ago

I started implementing this here: https://github.com/jrmuizel/bincode-size

It roughly works. One issue I've run into is calling associated methods on more complicated types. Currently I'm just using quote!(#ty :: method_name()). This works for simple types but doesn't for types like Option<u32> where I need to turn it into Option::<u32>::method_name(). @dtolnay do you have a suggestion for the best way to do this transformation?

The relevant code is here: https://github.com/jrmuizel/bincode-size/blob/master/bincode-maxsize-derive/src/lib.rs#L10

dtolnay commented 5 years ago

Looks like you want <#ty as bincode_maxsize::BincodeMaxSize>::bincode_max_size().

jrmuizel commented 5 years ago

Yes. That works great. I've updated https://github.com/jrmuizel/bincode-size. It should be somewhat usable now. I'll test out using it in WebRender next.

dtolnay commented 5 years ago

Nice. I'll close this out and we can follow up on your issue tracker as necessary.

jonhoo commented 5 years ago

I wonder if support for this could be incorporated into upstream bincode?

dtolnay commented 5 years ago

Yep, @jrmuizel originally suggested this in https://github.com/TyOverby/bincode/issues/263. The situation is that bincode is not being maintained right now so anything that may need iteration or involvement from a maintainer is better off in a different crate.