Trait zerocopy::FromBytes

source ·
pub unsafe trait FromBytes {
    // Provided methods
    fn read_from<B: ByteSlice>(bytes: B) -> Option<Self>
       where Self: Sized { ... }
    fn read_from_prefix<B: ByteSlice>(bytes: B) -> Option<Self>
       where Self: Sized { ... }
    fn read_from_suffix<B: ByteSlice>(bytes: B) -> Option<Self>
       where Self: Sized { ... }
    fn new_zeroed() -> Self
       where Self: Sized { ... }
    fn new_box_zeroed() -> Box<Self>
       where Self: Sized { ... }
    fn new_box_slice_zeroed(len: usize) -> Box<[Self]>
       where Self: Sized { ... }
    fn new_vec_zeroed(len: usize) -> Vec<Self>
       where Self: Sized { ... }
}
Expand description

Types for which any byte pattern is valid.

WARNING: Do not implement this trait yourself! Instead, use #[derive(FromBytes)].

FromBytes types can safely be deserialized from an untrusted sequence of bytes because any byte sequence corresponds to a valid instance of the type.

FromBytes is ignorant of byte order. For byte order-aware types, see the byteorder module.

Safety

If T: FromBytes, then unsafe code may assume that it is sound to treat any initialized sequence of bytes of length size_of::<T>() as a T. If a type is marked as FromBytes which violates this contract, it may cause undefined behavior.

If a type has the following properties, then it is safe to implement FromBytes for that type:

  • If the type is a struct:
    • All of its fields must implement FromBytes
  • If the type is an enum:
    • It must be a C-like enum (meaning that all variants have no fields)
    • It must have a defined representation (reprs C, u8, u16, u32, u64, usize, i8, i16, i32, i64, or isize).
    • The maximum number of discriminants must be used (so that every possible bit pattern is a valid one). Be very careful when using the C, usize, or isize representations, as their size is platform-dependent.

Rationale

Why isn’t an explicit representation required for structs?

Per the Rust reference,

The representation of a type can change the padding between fields, but does not change the layout of the fields themselves.

Since the layout of structs only consists of padding bytes and field bytes, a struct is soundly FromBytes if:

  1. its padding is soundly FromBytes, and
  2. its fields are soundly FromBytes.

The answer to the first question is always yes: padding bytes do not have any validity constraints. A discussion of this question in the Unsafe Code Guidelines Working Group concluded that it would be virtually unimaginable for future versions of rustc to add validity constraints to padding bytes.

Whether a struct is soundly FromBytes therefore solely depends on whether its fields are FromBytes.

Provided Methods§

source

fn read_from<B: ByteSlice>(bytes: B) -> Option<Self>
where Self: Sized,

Reads a copy of Self from bytes.

If bytes.len() != size_of::<Self>(), read_from returns None.

source

fn read_from_prefix<B: ByteSlice>(bytes: B) -> Option<Self>
where Self: Sized,

Reads a copy of Self from the prefix of bytes.

read_from_prefix reads a Self from the first size_of::<Self>() bytes of bytes. If bytes.len() < size_of::<Self>(), it returns None.

source

fn read_from_suffix<B: ByteSlice>(bytes: B) -> Option<Self>
where Self: Sized,

Reads a copy of Self from the suffix of bytes.

read_from_suffix reads a Self from the last size_of::<Self>() bytes of bytes. If bytes.len() < size_of::<Self>(), it returns None.

source

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes.

source

fn new_box_zeroed() -> Box<Self>
where Self: Sized,

Creates a Box<Self> from zeroed bytes.

This function is useful for allocating large values on the heap and zero-initializing them, without ever creating a temporary instance of Self on the stack. For example, <[u8; 1048576]>::new_box_zeroed() will allocate [u8; 1048576] directly on the heap; it does not require storing [u8; 1048576] in a temporary variable on the stack.

On systems that use a heap implementation that supports allocating from pre-zeroed memory, using new_box_zeroed (or related functions) may have performance benefits.

Note that Box<Self> can be converted to Arc<Self> and other container types without reallocation.

Panics

Panics if allocation of size_of::<Self>() bytes fails.

source

fn new_box_slice_zeroed(len: usize) -> Box<[Self]>
where Self: Sized,

Creates a Box<[Self]> (a boxed slice) from zeroed bytes.

This function is useful for allocating large values of [Self] on the heap and zero-initializing them, without ever creating a temporary instance of [Self; _] on the stack. For example, u8::new_box_slice_zeroed(1048576) will allocate the slice directly on the heap; it does not require storing the slice on the stack.

On systems that use a heap implementation that supports allocating from pre-zeroed memory, using new_box_slice_zeroed may have performance benefits.

If Self is a zero-sized type, then this function will return a Box<[Self]> that has the correct len. Such a box cannot contain any actual information, but its len() property will report the correct value.

Panics
  • Panics if size_of::<Self>() * len overflows.
  • Panics if allocation of size_of::<Self>() * len bytes fails.
source

fn new_vec_zeroed(len: usize) -> Vec<Self>
where Self: Sized,

Creates a Vec<Self> from zeroed bytes.

This function is useful for allocating large values of Vecs and zero-initializing them, without ever creating a temporary instance of [Self; _] (or many temporary instances of Self) on the stack. For example, u8::new_vec_zeroed(1048576) will allocate directly on the heap; it does not require storing intermediate values on the stack.

On systems that use a heap implementation that supports allocating from pre-zeroed memory, using new_vec_zeroed may have performance benefits.

If Self is a zero-sized type, then this function will return a Vec<Self> that has the correct len. Such a Vec cannot contain any actual information, but its len() property will report the correct value.

Panics
  • Panics if size_of::<Self>() * len overflows.
  • Panics if allocation of size_of::<Self>() * len bytes fails.

Implementations on Foreign Types§

source§

impl FromBytes for Option<NonZeroI8>

source§

impl FromBytes for Option<NonZeroI16>

source§

impl FromBytes for Option<NonZeroI32>

source§

impl FromBytes for Option<NonZeroI64>

source§

impl FromBytes for Option<NonZeroI128>

source§

impl FromBytes for Option<NonZeroIsize>

source§

impl FromBytes for Option<NonZeroU8>

source§

impl FromBytes for Option<NonZeroU16>

source§

impl FromBytes for Option<NonZeroU32>

source§

impl FromBytes for Option<NonZeroU64>

source§

impl FromBytes for Option<NonZeroU128>

source§

impl FromBytes for Option<NonZeroUsize>

source§

impl FromBytes for f32

source§

impl FromBytes for f64

source§

impl FromBytes for i8

source§

impl FromBytes for i16

source§

impl FromBytes for i32

source§

impl FromBytes for i64

source§

impl FromBytes for i128

source§

impl FromBytes for isize

source§

impl FromBytes for u8

source§

impl FromBytes for u16

source§

impl FromBytes for u32

source§

impl FromBytes for u64

source§

impl FromBytes for u128

source§

impl FromBytes for ()

source§

impl FromBytes for usize

source§

impl FromBytes for __m128

source§

impl FromBytes for __m128d

source§

impl FromBytes for __m128i

source§

impl FromBytes for __m256

source§

impl FromBytes for __m256d

source§

impl FromBytes for __m256i

source§

impl<T: FromBytes> FromBytes for [T]

source§

impl<T: FromBytes> FromBytes for Wrapping<T>

source§

impl<T: FromBytes> FromBytes for MaybeUninit<T>

source§

impl<T: FromBytes, const N: usize> FromBytes for [T; N]

source§

impl<T: ?Sized + FromBytes> FromBytes for ManuallyDrop<T>

source§

impl<T: ?Sized> FromBytes for PhantomData<T>

Implementors§

source§

impl<O> FromBytes for F32<O>

source§

impl<O> FromBytes for F64<O>

source§

impl<O> FromBytes for I16<O>

source§

impl<O> FromBytes for I32<O>

source§

impl<O> FromBytes for I64<O>

source§

impl<O> FromBytes for I128<O>

source§

impl<O> FromBytes for U16<O>

source§

impl<O> FromBytes for U32<O>

source§

impl<O> FromBytes for U64<O>

source§

impl<O> FromBytes for U128<O>

source§

impl<T> FromBytes for Unalign<T>
where T: FromBytes,