[][src]Trait zerocopy::FromBytes

pub unsafe trait FromBytes { }

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.

Implementations on Foreign Types

impl<T> FromBytes for PhantomData<T>[src]

Loading content...

Implementors

impl FromBytes for ()[src]

impl FromBytes for f32[src]

impl FromBytes for f64[src]

impl FromBytes for i8[src]

impl FromBytes for i16[src]

impl FromBytes for i32[src]

impl FromBytes for i64[src]

impl FromBytes for i128[src]

impl FromBytes for isize[src]

impl FromBytes for u8[src]

impl FromBytes for u16[src]

impl FromBytes for u32[src]

impl FromBytes for u64[src]

impl FromBytes for u128[src]

impl FromBytes for usize[src]

impl<O: ByteOrder> FromBytes for I128<O> where
    PhantomData<O>: FromBytes
[src]

impl<O: ByteOrder> FromBytes for I16<O> where
    PhantomData<O>: FromBytes
[src]

impl<O: ByteOrder> FromBytes for I32<O> where
    PhantomData<O>: FromBytes
[src]

impl<O: ByteOrder> FromBytes for I64<O> where
    PhantomData<O>: FromBytes
[src]

impl<O: ByteOrder> FromBytes for U128<O> where
    PhantomData<O>: FromBytes
[src]

impl<O: ByteOrder> FromBytes for U16<O> where
    PhantomData<O>: FromBytes
[src]

impl<O: ByteOrder> FromBytes for U32<O> where
    PhantomData<O>: FromBytes
[src]

impl<O: ByteOrder> FromBytes for U64<O> where
    PhantomData<O>: FromBytes
[src]

impl<T: FromBytes> FromBytes for [T; 0][src]

impl<T: FromBytes> FromBytes for [T; 1][src]

impl<T: FromBytes> FromBytes for [T; 2][src]

impl<T: FromBytes> FromBytes for [T; 3][src]

impl<T: FromBytes> FromBytes for [T; 4][src]

impl<T: FromBytes> FromBytes for [T; 5][src]

impl<T: FromBytes> FromBytes for [T; 6][src]

impl<T: FromBytes> FromBytes for [T; 7][src]

impl<T: FromBytes> FromBytes for [T; 8][src]

impl<T: FromBytes> FromBytes for [T; 9][src]

impl<T: FromBytes> FromBytes for [T; 10][src]

impl<T: FromBytes> FromBytes for [T; 11][src]

impl<T: FromBytes> FromBytes for [T; 12][src]

impl<T: FromBytes> FromBytes for [T; 13][src]

impl<T: FromBytes> FromBytes for [T; 14][src]

impl<T: FromBytes> FromBytes for [T; 15][src]

impl<T: FromBytes> FromBytes for [T; 16][src]

impl<T: FromBytes> FromBytes for [T; 17][src]

impl<T: FromBytes> FromBytes for [T; 18][src]

impl<T: FromBytes> FromBytes for [T; 19][src]

impl<T: FromBytes> FromBytes for [T; 20][src]

impl<T: FromBytes> FromBytes for [T; 21][src]

impl<T: FromBytes> FromBytes for [T; 22][src]

impl<T: FromBytes> FromBytes for [T; 23][src]

impl<T: FromBytes> FromBytes for [T; 24][src]

impl<T: FromBytes> FromBytes for [T; 25][src]

impl<T: FromBytes> FromBytes for [T; 26][src]

impl<T: FromBytes> FromBytes for [T; 27][src]

impl<T: FromBytes> FromBytes for [T; 28][src]

impl<T: FromBytes> FromBytes for [T; 29][src]

impl<T: FromBytes> FromBytes for [T; 30][src]

impl<T: FromBytes> FromBytes for [T; 31][src]

impl<T: FromBytes> FromBytes for [T; 32][src]

impl<T: FromBytes> FromBytes for [T; 33][src]

impl<T: FromBytes> FromBytes for [T; 34][src]

impl<T: FromBytes> FromBytes for [T; 35][src]

impl<T: FromBytes> FromBytes for [T; 36][src]

impl<T: FromBytes> FromBytes for [T; 37][src]

impl<T: FromBytes> FromBytes for [T; 38][src]

impl<T: FromBytes> FromBytes for [T; 39][src]

impl<T: FromBytes> FromBytes for [T; 40][src]

impl<T: FromBytes> FromBytes for [T; 41][src]

impl<T: FromBytes> FromBytes for [T; 42][src]

impl<T: FromBytes> FromBytes for [T; 43][src]

impl<T: FromBytes> FromBytes for [T; 44][src]

impl<T: FromBytes> FromBytes for [T; 45][src]

impl<T: FromBytes> FromBytes for [T; 46][src]

impl<T: FromBytes> FromBytes for [T; 47][src]

impl<T: FromBytes> FromBytes for [T; 48][src]

impl<T: FromBytes> FromBytes for [T; 49][src]

impl<T: FromBytes> FromBytes for [T; 50][src]

impl<T: FromBytes> FromBytes for [T; 51][src]

impl<T: FromBytes> FromBytes for [T; 52][src]

impl<T: FromBytes> FromBytes for [T; 53][src]

impl<T: FromBytes> FromBytes for [T; 54][src]

impl<T: FromBytes> FromBytes for [T; 55][src]

impl<T: FromBytes> FromBytes for [T; 56][src]

impl<T: FromBytes> FromBytes for [T; 57][src]

impl<T: FromBytes> FromBytes for [T; 58][src]

impl<T: FromBytes> FromBytes for [T; 59][src]

impl<T: FromBytes> FromBytes for [T; 60][src]

impl<T: FromBytes> FromBytes for [T; 61][src]

impl<T: FromBytes> FromBytes for [T; 62][src]

impl<T: FromBytes> FromBytes for [T; 63][src]

impl<T: FromBytes> FromBytes for [T; 64][src]

impl<T: FromBytes> FromBytes for [T; 98][src]

impl<T: FromBytes> FromBytes for [T; 126][src]

impl<T: FromBytes> FromBytes for [T; 128][src]

impl<T: FromBytes> FromBytes for [T; 236][src]

impl<T: FromBytes> FromBytes for [T; 255][src]

impl<T: FromBytes> FromBytes for [T; 256][src]

impl<T: FromBytes> FromBytes for [T; 512][src]

impl<T: FromBytes> FromBytes for [T; 1024][src]

impl<T: FromBytes> FromBytes for [T; 2048][src]

impl<T: FromBytes> FromBytes for [T; 4096][src]

impl<T: FromBytes> FromBytes for [T; 8192][src]

impl<T: FromBytes> FromBytes for [T; 16384][src]

impl<T: FromBytes> FromBytes for [T; 32768][src]

impl<T: FromBytes> FromBytes for [T; 65536][src]

impl<T: FromBytes> FromBytes for [T][src]

Loading content...