Trait FromZeros
unsafe trait FromZeros: TryFromBytes
Types for which a sequence of 0 bytes is a valid instance.
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.
Warning: Padding bytes
Note that, when a value is moved or copied, only the non-padding bytes of
that value are guaranteed to be preserved. It is unsound to assume that
values written to padding bytes are preserved after a move or copy. For more
details, see the FromBytes docs.
Implementation
Do not implement this trait yourself! Instead, use
#[derive(FromZeros)]; e.g.:
# use ;
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
fn zero(self: &mut Self)Overwrites
selfwith zeros.Sets every byte in
selfto 0. While this is similar to doing*self = Self::new_zeroed(), it differs in thatzerodoes not semantically drop the current value and replace it with a new one — it simply modifies the bytes of the existing value.Examples
# use FromZeros; # use *; # let mut header = PacketHeader ; header.zero; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn new_zeroed() -> Self where Self: SizedCreates an instance of
Selffrom zeroed bytes.Examples
# use FromZeros; # use *; # let header: PacketHeader = new_zeroed; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
Implementors
impl FromZeros for charimpl FromZeros for __m128impl<H, I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroI16>impl FromZeros for __m256iimpl<C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroU128>impl<O> FromZeros for I16<O>impl<K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: K, _: L) -> M>impl<T> FromZeros for Option<&mut T>impl<T> FromZeros for Unalign<T>impl<F, G, H, I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<O> FromZeros for I32<O>impl<J, K, L, M> FromZeros for Option<fn(_: J, _: K, _: L) -> M>impl<A, B, C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<E, F, G, H, I, J, K, L, M> FromZeros for Option<fn(_: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for AtomicI16impl<M> FromZeros for Option<unsafe fn() -> M>impl FromZeros for u16impl FromZeros for AtomicUsizeimpl<H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for i64impl<O> FromZeros for Isize<O>impl<T> FromZeros for CoreMaybeUninit<T>impl<C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for f32impl<T: FromZeros> FromZeros for [T]impl<K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: K, _: L) -> M>impl<O> FromZeros for Usize<O>impl FromZeros for Option<NonZeroU8>impl FromZeros for __m128iimpl<F, G, H, I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroI32>impl FromZeros for __m512impl<A, B, C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroUsize>impl<I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: I, _: J, _: K, _: L) -> M>impl<M> FromZeros for Option<fn() -> M>impl<O> FromZeros for U64<O>impl<D, E, F, G, H, I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<H, I, J, K, L, M> FromZeros for Option<fn(_: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for AtomicI8impl<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>impl FromZeros for ()impl FromZeros for AtomicI32impl<K, L, M> FromZeros for Option<unsafe fn(_: K, _: L) -> M>impl FromZeros for u32impl<T> FromZeros for AtomicPtr<T>impl<F, G, H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for i128impl<T: ?Sized + FromZeros> FromZeros for Cell<T>impl<A, B, C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<O> FromZeros for U16<O>impl FromZeros for boolimpl<T> FromZeros for *mut Timpl<I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroU16>impl FromZeros for __m256dimpl<D, E, F, G, H, I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroI64>impl FromZeros for __m512iimpl<L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: L) -> M>impl<T> FromZeros for Option<&T>impl<G, H, I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<K, L, M> FromZeros for Option<fn(_: K, _: L) -> M>impl<O> FromZeros for F64<O>impl<B, C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<F, G, H, I, J, K, L, M> FromZeros for Option<fn(_: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<O> FromZeros for I128<O>impl FromZeros for AtomicU16impl<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>impl FromZeros for i8impl FromZeros for AtomicI64impl<I, J, K, L, M> FromZeros for Option<unsafe fn(_: I, _: J, _: K, _: L) -> M>impl FromZeros for u64impl<T: FromZeros> FromZeros for Wrapping<T>impl<D, E, F, G, H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for isizeimpl<T: FromZeros, N: usize> FromZeros for [T; N]impl<L, M> FromZeros for Option<extern C { unwind: false } fn(_: L) -> M>impl FromZeros for strimpl FromZeros for __m128dimpl<G, H, I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroU32>impl FromZeros for __m512bhimpl<B, C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroI128>impl<J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: J, _: K, _: L) -> M>impl<T> FromZeros for Option<NonNull<T>>impl<E, F, G, H, I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<I, J, K, L, M> FromZeros for Option<fn(_: I, _: J, _: K, _: L) -> M>impl FromZeros for AtomicU8impl<D, E, F, G, H, I, J, K, L, M> FromZeros for Option<fn(_: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for AtomicU32impl<L, M> FromZeros for Option<unsafe fn(_: L) -> M>impl FromZeros for i16impl<O> FromZeros for U32<O>impl FromZeros for AtomicIsizeimpl<G, H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<O> FromZeros for U128<O>impl FromZeros for u128impl<T: ?Sized + FromZeros> FromZeros for ManuallyDrop<T>impl<B, C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for f64impl<T> FromZeros for *const Timpl<J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroI8>impl FromZeros for __m256impl<O> FromZeros for F32<O>impl<O> FromZeros for I64<O>impl<E, F, G, H, I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroU64>impl FromZeros for __m512dimpl<M> FromZeros for Option<unsafe extern C { unwind: false } fn() -> M>impl FromZeros for Option<NonZeroIsize>impl<H, I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: H, _: I, _: J, _: K, _: L) -> M>impl<L, M> FromZeros for Option<fn(_: L) -> M>impl<C, D, E, F, G, H, I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<G, H, I, J, K, L, M> FromZeros for Option<fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for AtomicBoolimpl<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>impl FromZeros for u8impl FromZeros for AtomicU64impl<J, K, L, M> FromZeros for Option<unsafe fn(_: J, _: K, _: L) -> M>impl FromZeros for i32impl<T: ?Sized> FromZeros for PhantomData<T>impl<E, F, G, H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for usizeimpl<T: ?Sized + FromZeros> FromZeros for UnsafeCell<T>impl<M> FromZeros for Option<extern C { unwind: false } fn() -> M>