Rollup merge of #136976 - jedbrown:jed/doc-boxed-deferred-init, r=tgross35

alloc boxed: docs: use MaybeUninit::write instead of as_mut_ptr

In the deferred initialization pattern, the docs were needlessly going through `as_mut_ptr().write()` to initialize, which is unnecessary use of a pointer, needs to be inside an `unsafe` block, and may weaken alias analysis.
This commit is contained in:
Matthias Krüger 2025-02-14 16:23:33 +01:00 committed by GitHub
commit 145e35a6e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -280,13 +280,9 @@ impl<T> Box<T> {
/// ///
/// ``` /// ```
/// let mut five = Box::<u32>::new_uninit(); /// let mut five = Box::<u32>::new_uninit();
/// /// // Deferred initialization:
/// let five = unsafe { /// five.write(5);
/// // Deferred initialization: /// let five = unsafe { five.assume_init() };
/// five.as_mut_ptr().write(5);
///
/// five.assume_init()
/// };
/// ///
/// assert_eq!(*five, 5) /// assert_eq!(*five, 5)
/// ``` /// ```
@ -367,13 +363,9 @@ impl<T> Box<T> {
/// #![feature(allocator_api)] /// #![feature(allocator_api)]
/// ///
/// let mut five = Box::<u32>::try_new_uninit()?; /// let mut five = Box::<u32>::try_new_uninit()?;
/// /// // Deferred initialization:
/// let five = unsafe { /// five.write(5);
/// // Deferred initialization: /// let five = unsafe { five.assume_init() };
/// five.as_mut_ptr().write(5);
///
/// five.assume_init()
/// };
/// ///
/// assert_eq!(*five, 5); /// assert_eq!(*five, 5);
/// # Ok::<(), std::alloc::AllocError>(()) /// # Ok::<(), std::alloc::AllocError>(())
@ -435,10 +427,8 @@ impl<T, A: Allocator> Box<T, A> {
A: Allocator, A: Allocator,
{ {
let mut boxed = Self::new_uninit_in(alloc); let mut boxed = Self::new_uninit_in(alloc);
unsafe { boxed.write(x);
boxed.as_mut_ptr().write(x); unsafe { boxed.assume_init() }
boxed.assume_init()
}
} }
/// Allocates memory in the given allocator then places `x` into it, /// Allocates memory in the given allocator then places `x` into it,
@ -463,10 +453,8 @@ impl<T, A: Allocator> Box<T, A> {
A: Allocator, A: Allocator,
{ {
let mut boxed = Self::try_new_uninit_in(alloc)?; let mut boxed = Self::try_new_uninit_in(alloc)?;
unsafe { boxed.write(x);
boxed.as_mut_ptr().write(x); unsafe { Ok(boxed.assume_init()) }
Ok(boxed.assume_init())
}
} }
/// Constructs a new box with uninitialized contents in the provided allocator. /// Constructs a new box with uninitialized contents in the provided allocator.
@ -479,13 +467,9 @@ impl<T, A: Allocator> Box<T, A> {
/// use std::alloc::System; /// use std::alloc::System;
/// ///
/// let mut five = Box::<u32, _>::new_uninit_in(System); /// let mut five = Box::<u32, _>::new_uninit_in(System);
/// /// // Deferred initialization:
/// let five = unsafe { /// five.write(5);
/// // Deferred initialization: /// let five = unsafe { five.assume_init() };
/// five.as_mut_ptr().write(5);
///
/// five.assume_init()
/// };
/// ///
/// assert_eq!(*five, 5) /// assert_eq!(*five, 5)
/// ``` /// ```
@ -517,13 +501,9 @@ impl<T, A: Allocator> Box<T, A> {
/// use std::alloc::System; /// use std::alloc::System;
/// ///
/// let mut five = Box::<u32, _>::try_new_uninit_in(System)?; /// let mut five = Box::<u32, _>::try_new_uninit_in(System)?;
/// /// // Deferred initialization:
/// let five = unsafe { /// five.write(5);
/// // Deferred initialization: /// let five = unsafe { five.assume_init() };
/// five.as_mut_ptr().write(5);
///
/// five.assume_init()
/// };
/// ///
/// assert_eq!(*five, 5); /// assert_eq!(*five, 5);
/// # Ok::<(), std::alloc::AllocError>(()) /// # Ok::<(), std::alloc::AllocError>(())
@ -669,15 +649,11 @@ impl<T> Box<[T]> {
/// ///
/// ``` /// ```
/// let mut values = Box::<[u32]>::new_uninit_slice(3); /// let mut values = Box::<[u32]>::new_uninit_slice(3);
/// /// // Deferred initialization:
/// let values = unsafe { /// values[0].write(1);
/// // Deferred initialization: /// values[1].write(2);
/// values[0].as_mut_ptr().write(1); /// values[2].write(3);
/// values[1].as_mut_ptr().write(2); /// let values = unsafe {values.assume_init() };
/// values[2].as_mut_ptr().write(3);
///
/// values.assume_init()
/// };
/// ///
/// assert_eq!(*values, [1, 2, 3]) /// assert_eq!(*values, [1, 2, 3])
/// ``` /// ```
@ -722,13 +698,11 @@ impl<T> Box<[T]> {
/// #![feature(allocator_api)] /// #![feature(allocator_api)]
/// ///
/// let mut values = Box::<[u32]>::try_new_uninit_slice(3)?; /// let mut values = Box::<[u32]>::try_new_uninit_slice(3)?;
/// let values = unsafe { /// // Deferred initialization:
/// // Deferred initialization: /// values[0].write(1);
/// values[0].as_mut_ptr().write(1); /// values[1].write(2);
/// values[1].as_mut_ptr().write(2); /// values[2].write(3);
/// values[2].as_mut_ptr().write(3); /// let values = unsafe { values.assume_init() };
/// values.assume_init()
/// };
/// ///
/// assert_eq!(*values, [1, 2, 3]); /// assert_eq!(*values, [1, 2, 3]);
/// # Ok::<(), std::alloc::AllocError>(()) /// # Ok::<(), std::alloc::AllocError>(())
@ -814,15 +788,11 @@ impl<T, A: Allocator> Box<[T], A> {
/// use std::alloc::System; /// use std::alloc::System;
/// ///
/// let mut values = Box::<[u32], _>::new_uninit_slice_in(3, System); /// let mut values = Box::<[u32], _>::new_uninit_slice_in(3, System);
/// /// // Deferred initialization:
/// let values = unsafe { /// values[0].write(1);
/// // Deferred initialization: /// values[1].write(2);
/// values[0].as_mut_ptr().write(1); /// values[2].write(3);
/// values[1].as_mut_ptr().write(2); /// let values = unsafe { values.assume_init() };
/// values[2].as_mut_ptr().write(3);
///
/// values.assume_init()
/// };
/// ///
/// assert_eq!(*values, [1, 2, 3]) /// assert_eq!(*values, [1, 2, 3])
/// ``` /// ```
@ -873,13 +843,11 @@ impl<T, A: Allocator> Box<[T], A> {
/// use std::alloc::System; /// use std::alloc::System;
/// ///
/// let mut values = Box::<[u32], _>::try_new_uninit_slice_in(3, System)?; /// let mut values = Box::<[u32], _>::try_new_uninit_slice_in(3, System)?;
/// let values = unsafe { /// // Deferred initialization:
/// // Deferred initialization: /// values[0].write(1);
/// values[0].as_mut_ptr().write(1); /// values[1].write(2);
/// values[1].as_mut_ptr().write(2); /// values[2].write(3);
/// values[2].as_mut_ptr().write(3); /// let values = unsafe { values.assume_init() };
/// values.assume_init()
/// };
/// ///
/// assert_eq!(*values, [1, 2, 3]); /// assert_eq!(*values, [1, 2, 3]);
/// # Ok::<(), std::alloc::AllocError>(()) /// # Ok::<(), std::alloc::AllocError>(())
@ -959,13 +927,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
/// ///
/// ``` /// ```
/// let mut five = Box::<u32>::new_uninit(); /// let mut five = Box::<u32>::new_uninit();
/// /// // Deferred initialization:
/// let five: Box<u32> = unsafe { /// five.write(5);
/// // Deferred initialization: /// let five: Box<u32> = unsafe { five.assume_init() };
/// five.as_mut_ptr().write(5);
///
/// five.assume_init()
/// };
/// ///
/// assert_eq!(*five, 5) /// assert_eq!(*five, 5)
/// ``` /// ```
@ -1030,15 +994,11 @@ impl<T, A: Allocator> Box<[mem::MaybeUninit<T>], A> {
/// ///
/// ``` /// ```
/// let mut values = Box::<[u32]>::new_uninit_slice(3); /// let mut values = Box::<[u32]>::new_uninit_slice(3);
/// /// // Deferred initialization:
/// let values = unsafe { /// values[0].write(1);
/// // Deferred initialization: /// values[1].write(2);
/// values[0].as_mut_ptr().write(1); /// values[2].write(3);
/// values[1].as_mut_ptr().write(2); /// let values = unsafe { values.assume_init() };
/// values[2].as_mut_ptr().write(3);
///
/// values.assume_init()
/// };
/// ///
/// assert_eq!(*values, [1, 2, 3]) /// assert_eq!(*values, [1, 2, 3])
/// ``` /// ```