aboutsummaryrefslogtreecommitdiff
path: root/rust/qemu-api/src/uninit.rs
blob: 04123b4ae99d19c9bec13645d92acd851b45c3c0 (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
//! Access fields of a [`MaybeUninit`]

use std::{
    mem::MaybeUninit,
    ops::{Deref, DerefMut},
};

pub struct MaybeUninitField<'a, T, U> {
    parent: &'a mut MaybeUninit<T>,
    child: *mut U,
}

impl<'a, T, U> MaybeUninitField<'a, T, U> {
    #[doc(hidden)]
    pub fn new(parent: &'a mut MaybeUninit<T>, child: *mut U) -> Self {
        MaybeUninitField { parent, child }
    }

    /// Return a constant pointer to the containing object of the field.
    ///
    /// Because the `MaybeUninitField` remembers the containing object,
    /// it is possible to use it in foreign APIs that initialize the
    /// child.
    pub fn parent(f: &Self) -> *const T {
        f.parent.as_ptr()
    }

    /// Return a mutable pointer to the containing object.
    ///
    /// Because the `MaybeUninitField` remembers the containing object,
    /// it is possible to use it in foreign APIs that initialize the
    /// child.
    pub fn parent_mut(f: &mut Self) -> *mut T {
        f.parent.as_mut_ptr()
    }
}

impl<'a, T, U> Deref for MaybeUninitField<'a, T, U> {
    type Target = MaybeUninit<U>;

    fn deref(&self) -> &MaybeUninit<U> {
        // SAFETY: self.child was obtained by dereferencing a valid mutable
        // reference; the content of the memory may be invalid or uninitialized
        // but MaybeUninit<_> makes no assumption on it
        unsafe { &*(self.child.cast()) }
    }
}

impl<'a, T, U> DerefMut for MaybeUninitField<'a, T, U> {
    fn deref_mut(&mut self) -> &mut MaybeUninit<U> {
        // SAFETY: self.child was obtained by dereferencing a valid mutable
        // reference; the content of the memory may be invalid or uninitialized
        // but MaybeUninit<_> makes no assumption on it
        unsafe { &mut *(self.child.cast()) }
    }
}

/// ```
/// #[derive(Debug)]
/// struct S {
///     x: u32,
///     y: u32,
/// }
///
/// # use std::mem::MaybeUninit;
/// # use qemu_api::{assert_match, uninit_field_mut};
///
/// let mut s: MaybeUninit<S> = MaybeUninit::zeroed();
/// uninit_field_mut!(s, x).write(5);
/// let s = unsafe { s.assume_init() };
/// assert_match!(s, S { x: 5, y: 0 });
/// ```
#[macro_export]
macro_rules! uninit_field_mut {
    ($container:expr, $($field:tt)+) => {{
        let container__: &mut ::std::mem::MaybeUninit<_> = &mut $container;
        let container_ptr__ = container__.as_mut_ptr();

        // SAFETY: the container is not used directly, only through a MaybeUninit<>,
        // so the safety is delegated to the caller and to final invocation of
        // assume_init()
        let target__ = unsafe { std::ptr::addr_of_mut!((*container_ptr__).$($field)+) };
        $crate::uninit::MaybeUninitField::new(container__, target__)
    }};
}