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<O> FromZeros for I16<O>impl<T> FromZeros for Unalign<T>impl<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 usizeimpl<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<O> FromZeros for U16<O>impl<T> FromZeros for Option<&T>impl<T: FromZeros, N: usize> FromZeros for [T; N]impl<J, K, L, M> FromZeros for Option<unsafe fn(_: J, _: K, _: L) -> M>impl<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 f64impl FromZeros for AtomicBoolimpl<M> FromZeros for Option<fn() -> M>impl<T> FromZeros for *mut Timpl<O> FromZeros for U128<O>impl<G, H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<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 strimpl FromZeros for AtomicU32impl<J, K, L, M> FromZeros for Option<fn(_: J, _: K, _: L) -> M>impl FromZeros for __m128iimpl<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 u8impl<K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: K, _: L) -> M>impl FromZeros for Option<NonZeroU16>impl FromZeros for AtomicI64impl<G, H, I, J, K, L, M> FromZeros for Option<fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for __m256iimpl<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 FromZeros for i16impl<H, I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroI32>impl<T> FromZeros for AtomicPtr<T>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>impl FromZeros for __m512dimpl<K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: K, _: L) -> M>impl FromZeros for u64impl<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 FromZeros for Option<NonZeroU128>impl<T> FromZeros for CoreMaybeUninit<T>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>impl<O> FromZeros for I128<O>impl<H, I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for i128impl<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 FromZeros for Option<NonZeroIsize>impl<T: ?Sized + FromZeros> FromZeros for UnsafeCell<T>impl<K, L, M> FromZeros for Option<unsafe fn(_: K, _: L) -> M>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 f32impl FromZeros for AtomicI8impl<O> FromZeros for Usize<O>impl<O> FromZeros for F64<O>impl<T> FromZeros for Option<NonNull<T>>impl<T> FromZeros for *const Timpl<H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: H, _: I, _: J, _: K, _: L) -> M>impl<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<O> FromZeros for I32<O>impl FromZeros for charimpl FromZeros for AtomicI16impl<K, L, M> FromZeros for Option<fn(_: K, _: L) -> M>impl FromZeros for __m128dimpl<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 ()impl<L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: L) -> M>impl FromZeros for Option<NonZeroI8>impl FromZeros for AtomicU64impl<H, I, J, K, L, M> FromZeros for Option<fn(_: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for __m256dimpl<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 u16impl<I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroU32>impl FromZeros for AtomicIsizeimpl<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 __m512impl<L, M> FromZeros for Option<extern C { unwind: false } fn(_: L) -> M>impl FromZeros for i32impl<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 FromZeros for Option<NonZeroI64>impl<T: FromZeros> FromZeros for Wrapping<T>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>impl<I, J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: I, _: J, _: K, _: L) -> M>impl<O> FromZeros for F32<O>impl FromZeros for u128impl<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 FromZeros for Option<NonZeroUsize>impl<T: ?Sized + FromZeros> FromZeros for Cell<T>impl<L, M> FromZeros for Option<unsafe fn(_: L) -> M>impl<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 isizeimpl FromZeros for AtomicU8impl<T> FromZeros for Option<&mut T>impl<T: FromZeros> FromZeros for [T]impl<I, J, K, L, M> FromZeros for Option<unsafe fn(_: I, _: J, _: K, _: L) -> M>impl<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 boolimpl FromZeros for AtomicU16impl<L, M> FromZeros for Option<fn(_: L) -> M>impl FromZeros for __m128impl<F, G, H, I, J, K, L, M> FromZeros for Option<unsafe fn(_: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl<O> FromZeros for I64<O>impl<M> FromZeros for Option<unsafe extern C { unwind: false } fn() -> M>impl FromZeros for Option<NonZeroU8>impl FromZeros for AtomicI32impl<I, J, K, L, M> FromZeros for Option<fn(_: I, _: J, _: K, _: L) -> M>impl FromZeros for __m256impl<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 i8impl<J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroI16>impl FromZeros for AtomicUsizeimpl<F, G, H, I, J, K, L, M> FromZeros for Option<fn(_: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for __m512bhimpl<M> FromZeros for Option<extern C { unwind: false } fn() -> M>impl FromZeros for u32impl<G, H, I, J, K, L, M> FromZeros for Option<unsafe extern C { unwind: false } fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>impl FromZeros for Option<NonZeroU64>impl<T: ?Sized> FromZeros for PhantomData<T>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>impl FromZeros for __m512iimpl<J, K, L, M> FromZeros for Option<extern C { unwind: false } fn(_: J, _: K, _: L) -> M>impl<O> FromZeros for U64<O>impl<O> FromZeros for U32<O>impl FromZeros for i64impl<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 FromZeros for Option<NonZeroI128>impl<O> FromZeros for Isize<O>impl<T: ?Sized + FromZeros> FromZeros for ManuallyDrop<T>impl<M> FromZeros for Option<unsafe fn() -> M>