aboutsummaryrefslogtreecommitdiff
path: root/rust/qemu-api/src/zeroable.rs
blob: 57cac96de06f97b27a8a83ce81da21c5992f2005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// SPDX-License-Identifier: GPL-2.0-or-later

use std::ptr;

/// Encapsulates the requirement that
/// `MaybeUninit::<Self>::zeroed().assume_init()` does not cause undefined
/// behavior.  This trait in principle could be implemented as just:
///
/// ```
/// pub unsafe trait Zeroable: Default {
///     const ZERO: Self = unsafe { ::core::mem::MaybeUninit::<Self>::zeroed().assume_init() };
/// }
/// ```
///
/// The need for a manual implementation is only because `zeroed()` cannot
/// be used as a `const fn` prior to Rust 1.75.0. Once we can assume a new
/// enough version of the compiler, we could provide a `#[derive(Zeroable)]`
/// macro to check at compile-time that all struct fields are Zeroable, and
/// use the above blanket implementation of the `ZERO` constant.
///
/// # Safety
///
/// Because the implementation of `ZERO` is manual, it does not make
/// any assumption on the safety of `zeroed()`.  However, other users of the
/// trait could use it that way.  Do not add this trait to a type unless
/// all-zeroes is a valid value for the type.  In particular, remember that
/// raw pointers can be zero, but references and `NonNull<T>` cannot
pub unsafe trait Zeroable: Default {
    const ZERO: Self;
}

unsafe impl Zeroable for crate::bindings::Property__bindgen_ty_1 {
    const ZERO: Self = Self { i: 0 };
}

unsafe impl Zeroable for crate::bindings::Property {
    const ZERO: Self = Self {
        name: ptr::null(),
        info: ptr::null(),
        offset: 0,
        bitnr: 0,
        bitmask: 0,
        set_default: false,
        defval: Zeroable::ZERO,
        arrayoffset: 0,
        arrayinfo: ptr::null(),
        arrayfieldsize: 0,
        link_type: ptr::null(),
    };
}

// bindgen does not derive Default here
#[allow(clippy::derivable_impls)]
impl Default for crate::bindings::VMStateFlags {
    fn default() -> Self {
        Self(0)
    }
}

unsafe impl Zeroable for crate::bindings::VMStateFlags {
    const ZERO: Self = Self(0);
}

unsafe impl Zeroable for crate::bindings::VMStateField {
    const ZERO: Self = Self {
        name: ptr::null(),
        err_hint: ptr::null(),
        offset: 0,
        size: 0,
        start: 0,
        num: 0,
        num_offset: 0,
        size_offset: 0,
        info: ptr::null(),
        flags: Zeroable::ZERO,
        vmsd: ptr::null(),
        version_id: 0,
        struct_version_id: 0,
        field_exists: None,
    };
}

unsafe impl Zeroable for crate::bindings::VMStateDescription {
    const ZERO: Self = Self {
        name: ptr::null(),
        unmigratable: false,
        early_setup: false,
        version_id: 0,
        minimum_version_id: 0,
        priority: crate::bindings::MigrationPriority::MIG_PRI_DEFAULT,
        pre_load: None,
        post_load: None,
        pre_save: None,
        post_save: None,
        needed: None,
        dev_unplug_pending: None,
        fields: ptr::null(),
        subsections: ptr::null(),
    };
}

unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_1 {
    const ZERO: Self = Self {
        min_access_size: 0,
        max_access_size: 0,
        unaligned: false,
        accepts: None,
    };
}

unsafe impl Zeroable for crate::bindings::MemoryRegionOps__bindgen_ty_2 {
    const ZERO: Self = Self {
        min_access_size: 0,
        max_access_size: 0,
        unaligned: false,
    };
}