qemu-cr16/rust/common/src/uninit.rs
Zhao Liu 35d7735f76 rust/common/uninit: Fix Clippy's complaints about lifetime
Clippy complains about the following cases and following its suggestion
to fix these warnings.

warning: the following explicit lifetimes could be elided: 'a
  --> common/src/uninit.rs:38:6
   |
38 | impl<'a, T, U> Deref for MaybeUninitField<'a, T, U> {
   |      ^^                                   ^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
   = note: `#[warn(clippy::needless_lifetimes)]` on by default
help: elide the lifetimes
   |
38 - impl<'a, T, U> Deref for MaybeUninitField<'a, T, U> {
38 + impl<T, U> Deref for MaybeUninitField<'_, T, U> {
   |

warning: the following explicit lifetimes could be elided: 'a
  --> common/src/uninit.rs:49:6
   |
49 | impl<'a, T, U> DerefMut for MaybeUninitField<'a, T, U> {
   |      ^^                                      ^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
help: elide the lifetimes
   |
49 - impl<'a, T, U> DerefMut for MaybeUninitField<'a, T, U> {
49 + impl<T, U> DerefMut for MaybeUninitField<'_, T, U> {
   |

warning: `common` (lib) generated 2 warnings (run `cargo clippy --fix --lib -p common` to apply 2 suggestions)

Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Link: https://lore.kernel.org/r/20250920160520.3699591-5-zhao1.liu@intel.com
2025-09-22 17:17:18 +02:00

85 lines
2.7 KiB
Rust

//! 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 const 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 const 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 const fn parent_mut(f: &mut Self) -> *mut T {
f.parent.as_mut_ptr()
}
}
impl<T, U> Deref for MaybeUninitField<'_, 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<T, U> DerefMut for MaybeUninitField<'_, 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 common::{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__)
}};
}