xref: /openbmc/qemu/rust/qemu-api/src/zeroable.rs (revision 6e50bde1)
1*6e50bde1SPaolo Bonzini // SPDX-License-Identifier: GPL-2.0-or-later
2*6e50bde1SPaolo Bonzini 
3*6e50bde1SPaolo Bonzini /// Encapsulates the requirement that
4*6e50bde1SPaolo Bonzini /// `MaybeUninit::<Self>::zeroed().assume_init()` does not cause
5*6e50bde1SPaolo Bonzini /// undefined behavior.
6*6e50bde1SPaolo Bonzini ///
7*6e50bde1SPaolo Bonzini /// # Safety
8*6e50bde1SPaolo Bonzini ///
9*6e50bde1SPaolo Bonzini /// Do not add this trait to a type unless all-zeroes is
10*6e50bde1SPaolo Bonzini /// a valid value for the type.  In particular, remember that raw
11*6e50bde1SPaolo Bonzini /// pointers can be zero, but references and `NonNull<T>` cannot
12*6e50bde1SPaolo Bonzini /// unless wrapped with `Option<>`.
13*6e50bde1SPaolo Bonzini pub unsafe trait Zeroable: Default {
14*6e50bde1SPaolo Bonzini     /// SAFETY: If the trait was added to a type, then by definition
15*6e50bde1SPaolo Bonzini     /// this is safe.
16*6e50bde1SPaolo Bonzini     const ZERO: Self = unsafe { ::core::mem::MaybeUninit::<Self>::zeroed().assume_init() };
17*6e50bde1SPaolo Bonzini }
18*6e50bde1SPaolo Bonzini 
19*6e50bde1SPaolo Bonzini unsafe impl Zeroable for crate::bindings::Property__bindgen_ty_1 {}
20*6e50bde1SPaolo Bonzini unsafe impl Zeroable for crate::bindings::Property {}
21*6e50bde1SPaolo Bonzini unsafe impl Zeroable for crate::bindings::VMStateDescription {}
22*6e50bde1SPaolo Bonzini unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_1 {}
23*6e50bde1SPaolo Bonzini unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_2 {}
24