Trait zerocopy::FromBytes

source ·
pub unsafe trait FromBytes { }
Expand description

Types for which any byte pattern is valid.

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

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:
    • It must be repr(C) or repr(transparent)
    • 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 be repr(u8), repr(u16), repr(u32), or repr(u64)
    • The maximum number of discriminants must be used (so that every possible bit pattern is a valid one)

Implementations on Foreign Types

Implementors