Closed dtolnay closed 5 years ago
FYI @jrmuizel
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
Looks like you want <#ty as bincode_maxsize::BincodeMaxSize>::bincode_max_size()
.
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.
Nice. I'll close this out and we can follow up on your issue tracker as necessary.
I wonder if support for this could be incorporated into upstream bincode
?
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.
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:
together with a simple trait implemented by the macro:
Types containing variably sized data like a String or Vec would have a minimum size but no max size.