feat(containers): add function to decode Vec with given body len#245
feat(containers): add function to decode Vec with given body len#245kskalski wants to merge 1 commit intoanza-xyz:masterfrom
Conversation
| T: SchemaRead<'de, C>, | ||
| { | ||
| Len::prealloc_check::<T>(len)?; | ||
| let mut vec: vec::Vec<T::Dst> = vec::Vec::with_capacity(len); |
There was a problem hiding this comment.
maybe a better idea would be to provide Box<[T]> decoding instead - Vec should be trivial to create from box and with Box::<[T]>::new_uninit_slice we get exactly the requested capacity
wincode/src/schema/containers.rs
Outdated
| Len: SeqLen<C>, | ||
| T: SchemaRead<'de, C>, | ||
| { | ||
| Len::prealloc_check::<T>(len)?; |
There was a problem hiding this comment.
Shouldn't this use Len::read_prealloc_check::<T::Dst>(reader.by_ref())? ?
There was a problem hiding this comment.
No, read_prealloc_check reads the length from the reader and the point of this API is to use len provided in fn parameter instead of reading it. We still want to validate the length against the limit defined by Len: SeqLen<C>, the call here does exactly (and only) that.
There was a problem hiding this comment.
Ah sorry, I got confused I meant Len::prealloc_check::<T::Dst>(reader.by_ref())?
There was a problem hiding this comment.
Oh, it should be Len::prealloc_check::<T::Dst>(len)?, right.
3f47340 to
35352dc
Compare
It's often useful to decode just vector's body with known length (which in hand-tuned serialization formats may be stored in other fields or implied from previously decoded data).
Add function
wincode::containers::Vec::<T, SeqLen>::decode_body_with_len::<ConfigCore>(reader, len)that does:SeqLen<ConfigCore>