-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: Add FromBytes::read_from_prefix_split and read_from_suffix_split #1051
Comments
Something like this (playground)? fn read_from_prefix_split<T>(bytes: &[u8]) -> Option<(T, &[u8])>
where
T: Sized + FromBytes,
{
Ref::<_, Unalign<T>>::new_unaligned_from_prefix(bytes)
.map(|(r, s)| (r.read().into_inner(), s))
} |
Yeah that looks about right. I mean there's a number of ways to do it, you can just call FromBytes::read_from_prefix and then index into your byte buffer by sizeof<T>, but having it provided on FromBytes and able to be used in a small oneliner would be nice. |
In our code, we often parse structures with fixed headers and then some dynamic portion, from a potentially unaligned buffer. To support this, it has been nice to have both options readily available (both preserving the suffix bytes and not), without having to reach for
Or developers write something like To solve this, we have a private crate that provides these kind of extra methods for use with |
@jstarks Is that private crate open source? It'd be great to see what else you're needing to work around so that we can support your use cases! |
Closed in #1059 |
It would be handy to have a FromBytes::read_from_(prefix/suffix)_split that returns a tuple of the T and the unused portion of the byte slice. Ref::new_from_prefix isn't quite the same in case the byte slice is unaligned, but we don't want to derive Unaligned on the type.
The text was updated successfully, but these errors were encountered: