Trait zerocopy::FromZeros

source ·
pub unsafe trait FromZeros {
    // Provided methods
    fn zero(&mut self) { ... }
    fn new_zeroed() -> Self
       where Self: Sized { ... }
}
Expand description

Types for which a sequence of bytes all set to zero represents a valid instance of the type.

Any memory region of the appropriate length which is guaranteed to contain only zero bytes can be viewed as any FromZeros type with no runtime overhead. This is useful whenever memory is known to be in a zeroed state, such memory returned from some allocation routines.

§Implementation

Do not implement this trait yourself! Instead, use #[derive(FromZeros)] (requires the derive Cargo feature); e.g.:

#[derive(FromZeros)]
struct MyStruct {
    ...
}

#[derive(FromZeros)]
#[repr(u8)]
enum MyEnum {
    ...
}

#[derive(FromZeros)]
union MyUnion {
    ...
}

This derive performs a sophisticated, compile-time safety analysis to determine whether a type is FromZeros.

§Safety

This section describes what is required in order for T: FromZeros, and what unsafe code may assume of such types. If you don’t plan on implementing FromZeros manually, and you don’t plan on writing unsafe code that operates on FromZeros types, then you don’t need to read this section.

If T: FromZeros, then unsafe code may assume that it is sound to produce a T whose bytes are all initialized to zero. If a type is marked as FromZeros which violates this contract, it may cause undefined behavior.

#[derive(FromZeros)] only permits types which satisfy these requirements.

Provided Methods§

source

fn zero(&mut self)

Overwrites self with zeros.

Sets every byte in self to 0. While this is similar to doing *self = Self::new_zeroed(), it differs in that zero does not semantically drop the current value and replace it with a new one - it simply modifies the bytes of the existing value.

§Examples
#[derive(FromZeros)]
#[repr(C)]
struct PacketHeader {
    src_port: [u8; 2],
    dst_port: [u8; 2],
    length: [u8; 2],
    checksum: [u8; 2],
}

let mut header = PacketHeader {
    src_port: 100u16.to_be_bytes(),
    dst_port: 200u16.to_be_bytes(),
    length: 300u16.to_be_bytes(),
    checksum: 400u16.to_be_bytes(),
};

header.zero();

assert_eq!(header.src_port, [0, 0]);
assert_eq!(header.dst_port, [0, 0]);
assert_eq!(header.length, [0, 0]);
assert_eq!(header.checksum, [0, 0]);
source

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

Creates an instance of Self from zeroed bytes.

§Examples
#[derive(FromZeros)]
#[repr(C)]
struct PacketHeader {
    src_port: [u8; 2],
    dst_port: [u8; 2],
    length: [u8; 2],
    checksum: [u8; 2],
}

let header: PacketHeader = FromZeros::new_zeroed();

assert_eq!(header.src_port, [0, 0]);
assert_eq!(header.dst_port, [0, 0]);
assert_eq!(header.length, [0, 0]);
assert_eq!(header.checksum, [0, 0]);

Implementations on Foreign Types§

source§

impl FromZeros for Option<NonZeroI8>

source§

impl FromZeros for Option<NonZeroI16>

source§

impl FromZeros for Option<NonZeroI32>

source§

impl FromZeros for Option<NonZeroI64>

source§

impl FromZeros for Option<NonZeroI128>

source§

impl FromZeros for Option<NonZeroIsize>

source§

impl FromZeros for Option<NonZeroU8>

source§

impl FromZeros for Option<NonZeroU16>

source§

impl FromZeros for Option<NonZeroU32>

source§

impl FromZeros for Option<NonZeroU64>

source§

impl FromZeros for Option<NonZeroU128>

source§

impl FromZeros for Option<NonZeroUsize>

source§

impl FromZeros for bool

source§

impl FromZeros for char

source§

impl FromZeros for f32

source§

impl FromZeros for f64

source§

impl FromZeros for i8

source§

impl FromZeros for i16

source§

impl FromZeros for i32

source§

impl FromZeros for i64

source§

impl FromZeros for i128

source§

impl FromZeros for isize

source§

impl FromZeros for str

source§

impl FromZeros for u8

source§

impl FromZeros for u16

source§

impl FromZeros for u32

source§

impl FromZeros for u64

source§

impl FromZeros for u128

source§

impl FromZeros for ()

source§

impl FromZeros for usize

source§

impl FromZeros for __m128

source§

impl FromZeros for __m128d

source§

impl FromZeros for __m128i

source§

impl FromZeros for __m256

source§

impl FromZeros for __m256d

source§

impl FromZeros for __m256i

source§

impl<A, B, C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<A, B, C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<B, C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<fn(_: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<B, C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<extern "C" fn(_: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<fn(_: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<extern "C" fn(_: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<D, E, F, G, H, I, J, K, L, M> FromZeros for Option<fn(_: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<D, E, F, G, H, I, J, K, L, M> FromZeros for Option<extern "C" fn(_: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<E, F, G, H, I, J, K, L, M> FromZeros for Option<fn(_: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<E, F, G, H, I, J, K, L, M> FromZeros for Option<extern "C" fn(_: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<F, G, H, I, J, K, L, M> FromZeros for Option<fn(_: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<F, G, H, I, J, K, L, M> FromZeros for Option<extern "C" fn(_: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<G, H, I, J, K, L, M> FromZeros for Option<fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<G, H, I, J, K, L, M> FromZeros for Option<extern "C" fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<H, I, J, K, L, M> FromZeros for Option<fn(_: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<H, I, J, K, L, M> FromZeros for Option<extern "C" fn(_: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<I, J, K, L, M> FromZeros for Option<fn(_: I, _: J, _: K, _: L) -> M>

source§

impl<I, J, K, L, M> FromZeros for Option<extern "C" fn(_: I, _: J, _: K, _: L) -> M>

source§

impl<J, K, L, M> FromZeros for Option<fn(_: J, _: K, _: L) -> M>

source§

impl<J, K, L, M> FromZeros for Option<extern "C" fn(_: J, _: K, _: L) -> M>

source§

impl<K, L, M> FromZeros for Option<fn(_: K, _: L) -> M>

source§

impl<K, L, M> FromZeros for Option<extern "C" fn(_: K, _: L) -> M>

source§

impl<L, M> FromZeros for Option<fn(_: L) -> M>

source§

impl<L, M> FromZeros for Option<extern "C" fn(_: L) -> M>

source§

impl<M> FromZeros for Option<fn() -> M>

source§

impl<M> FromZeros for Option<extern "C" fn() -> M>

source§

impl<T> FromZeros for Option<&T>

source§

impl<T> FromZeros for Option<&mut T>

source§

impl<T> FromZeros for Option<NonNull<T>>

source§

impl<T> FromZeros for *const T

source§

impl<T> FromZeros for *mut T

source§

impl<T> FromZeros for MaybeUninit<T>

source§

impl<T: FromZeros> FromZeros for [T]

source§

impl<T: FromZeros> FromZeros for Wrapping<T>

source§

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

source§

impl<T: ?Sized + FromZeros> FromZeros for UnsafeCell<T>

source§

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

source§

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

Implementors§

source§

impl<O> FromZeros for F32<O>

source§

impl<O> FromZeros for F64<O>

source§

impl<O> FromZeros for I16<O>

source§

impl<O> FromZeros for I32<O>

source§

impl<O> FromZeros for I64<O>

source§

impl<O> FromZeros for I128<O>

source§

impl<O> FromZeros for Isize<O>

source§

impl<O> FromZeros for U16<O>

source§

impl<O> FromZeros for U32<O>

source§

impl<O> FromZeros for U64<O>

source§

impl<O> FromZeros for U128<O>

source§

impl<O> FromZeros for Usize<O>

source§

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