Trait as_with_bytes::TryWithBytes [] [src]

pub trait TryWithBytes {
    unsafe fn try_with_bytes<'a>(bytes: &'a [u8]) -> Option<&'a Self>;
}

A trait for converting from bytes while checking that the byte slice is long enough.

Required Methods

Returns Some(&Self) if there are enough bytes to encode Self, or None otherwise.

Unsafe

While this protects against reading from memory beyond the boundary of the bytes, it can still produce invalid data for some types such as enums. It will work as long as whatever you encode from a type, you decode into that same type.

Note

When used to decode dynamically sized slices, Some will almost always be returned, since the slice will be empty if there is not enough data. The only situation where None is ever returned is when you ask for a slice containing a type with a size of zero, and who would want that?

Implementors