xref: /qemu/rust/qemu-api/src/zeroable.rs (revision c5f122fdcc280a82e7c5f31de890f985aa7ba773)
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 
20 // bindgen does not derive Default here
21 #[allow(clippy::derivable_impls)]
22 impl Default for crate::bindings::VMStateFlags {
default() -> Self23     fn default() -> Self {
24         Self(0)
25     }
26 }
27 
28 unsafe impl Zeroable for crate::bindings::Property__bindgen_ty_1 {}
29 unsafe impl Zeroable for crate::bindings::Property {}
30 unsafe impl Zeroable for crate::bindings::VMStateFlags {}
31 unsafe impl Zeroable for crate::bindings::VMStateField {}
32 unsafe impl Zeroable for crate::bindings::VMStateDescription {}
33 unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_1 {}
34 unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_2 {}
35 unsafe impl Zeroable for crate::bindings::MemoryRegionOps {}
36 unsafe impl Zeroable for crate::bindings::MemTxAttrs {}
37 unsafe impl Zeroable for crate::bindings::CharBackend {}
38