Trait TryWithBytes

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

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

Required Methods§

Source

unsafe fn try_with_bytes<'a>(bytes: &'a [u8]) -> Option<&'a Self>

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?

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: Copy> TryWithBytes for [T]

Source§

unsafe fn try_with_bytes<'a>(bytes: &'a [u8]) -> Option<&'a [T]>

Implementors§

Source§

impl<T: Copy> TryWithBytes for T