xref: /openbmc/qemu/rust/common/src/zeroable.rs (revision ccafa85a97e38698b798115bba6c18c849846e25)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 //! Defines a trait for structs that can be safely initialized with zero bytes.
4 
5 /// Encapsulates the requirement that
6 /// `MaybeUninit::<Self>::zeroed().assume_init()` does not cause undefined
7 /// behavior.
8 ///
9 /// # Safety
10 ///
11 /// Do not add this trait to a type unless all-zeroes is a valid value for the
12 /// type.  In particular, raw pointers can be zero, but references and
13 /// `NonNull<T>` cannot.
14 pub unsafe trait Zeroable: Default {
15     /// Return a value of Self whose memory representation consists of all
16     /// zeroes, with the possible exclusion of padding bytes.
17     const ZERO: Self = unsafe { ::core::mem::MaybeUninit::<Self>::zeroed().assume_init() };
18 }
19