mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-27 04:13:53 -06:00
rust: qemu_api_macros: add Wrapper derive macro
Add a derive macro that makes it easy to peel off all the layers of specialness (UnsafeCell, MaybeUninit, etc.) and just get a pointer to the wrapped type; and likewise add them back starting from a *mut. Reviewed-by: Zhao Liu <zhao1.liu@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
0b9d05e3c9
commit
f07a5674cf
4 changed files with 141 additions and 9 deletions
|
@ -1030,3 +1030,48 @@ impl<T: Default> Opaque<T> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Annotates [`Self`] as a transparent wrapper for another type.
|
||||
///
|
||||
/// Usually defined via the [`qemu_api_macros::Wrapper`] derive macro.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use std::mem::ManuallyDrop;
|
||||
/// # use qemu_api::cell::Wrapper;
|
||||
/// #[repr(transparent)]
|
||||
/// pub struct Example {
|
||||
/// inner: ManuallyDrop<String>,
|
||||
/// }
|
||||
///
|
||||
/// unsafe impl Wrapper for Example {
|
||||
/// type Wrapped = String;
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// `Self` must be a `#[repr(transparent)]` wrapper for the `Wrapped` type,
|
||||
/// whether directly or indirectly.
|
||||
///
|
||||
/// # Methods
|
||||
///
|
||||
/// By convention, types that implement Wrapper also implement the following
|
||||
/// methods:
|
||||
///
|
||||
/// ```ignore
|
||||
/// pub const unsafe fn from_raw<'a>(value: *mut Self::Wrapped) -> &'a Self;
|
||||
/// pub const unsafe fn as_mut_ptr(&self) -> *mut Self::Wrapped;
|
||||
/// pub const unsafe fn as_ptr(&self) -> *const Self::Wrapped;
|
||||
/// pub const unsafe fn raw_get(slot: *mut Self) -> *const Self::Wrapped;
|
||||
/// ```
|
||||
///
|
||||
/// They are not defined here to allow them to be `const`.
|
||||
pub unsafe trait Wrapper {
|
||||
type Wrapped;
|
||||
}
|
||||
|
||||
unsafe impl<T> Wrapper for Opaque<T> {
|
||||
type Wrapped = T;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue