Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 44 additions & 84 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,9 @@ impl<T> Box<T> {
///
/// ```
/// let mut five = Box::<u32>::new_uninit();
///
/// let five = unsafe {
/// // Deferred initialization:
/// five.as_mut_ptr().write(5);
///
/// five.assume_init()
/// };
/// // Deferred initialization:
/// five.write(5);
/// let five = unsafe { five.assume_init() };
///
/// assert_eq!(*five, 5)
/// ```
Expand Down Expand Up @@ -367,13 +363,9 @@ impl<T> Box<T> {
/// #![feature(allocator_api)]
///
/// let mut five = Box::<u32>::try_new_uninit()?;
///
/// let five = unsafe {
/// // Deferred initialization:
/// five.as_mut_ptr().write(5);
///
/// five.assume_init()
/// };
/// // Deferred initialization:
/// five.write(5);
/// let five = unsafe { five.assume_init() };
///
/// assert_eq!(*five, 5);
/// # Ok::<(), std::alloc::AllocError>(())
Expand Down Expand Up @@ -435,10 +427,8 @@ impl<T, A: Allocator> Box<T, A> {
A: Allocator,
{
let mut boxed = Self::new_uninit_in(alloc);
unsafe {
boxed.as_mut_ptr().write(x);
boxed.assume_init()
}
boxed.write(x);
unsafe { boxed.assume_init() }
}

/// Allocates memory in the given allocator then places `x` into it,
Expand All @@ -463,10 +453,8 @@ impl<T, A: Allocator> Box<T, A> {
A: Allocator,
{
let mut boxed = Self::try_new_uninit_in(alloc)?;
unsafe {
boxed.as_mut_ptr().write(x);
Ok(boxed.assume_init())
}
boxed.write(x);
unsafe { Ok(boxed.assume_init()) }
}

/// Constructs a new box with uninitialized contents in the provided allocator.
Expand All @@ -479,13 +467,9 @@ impl<T, A: Allocator> Box<T, A> {
/// use std::alloc::System;
///
/// let mut five = Box::<u32, _>::new_uninit_in(System);
///
/// let five = unsafe {
/// // Deferred initialization:
/// five.as_mut_ptr().write(5);
///
/// five.assume_init()
/// };
/// // Deferred initialization:
/// five.write(5);
/// let five = unsafe { five.assume_init() };
///
/// assert_eq!(*five, 5)
/// ```
Expand Down Expand Up @@ -517,13 +501,9 @@ impl<T, A: Allocator> Box<T, A> {
/// use std::alloc::System;
///
/// let mut five = Box::<u32, _>::try_new_uninit_in(System)?;
///
/// let five = unsafe {
/// // Deferred initialization:
/// five.as_mut_ptr().write(5);
///
/// five.assume_init()
/// };
/// // Deferred initialization:
/// five.write(5);
/// let five = unsafe { five.assume_init() };
///
/// assert_eq!(*five, 5);
/// # Ok::<(), std::alloc::AllocError>(())
Expand Down Expand Up @@ -669,15 +649,11 @@ impl<T> Box<[T]> {
///
/// ```
/// let mut values = Box::<[u32]>::new_uninit_slice(3);
///
/// let values = unsafe {
/// // Deferred initialization:
/// values[0].as_mut_ptr().write(1);
/// values[1].as_mut_ptr().write(2);
/// values[2].as_mut_ptr().write(3);
///
/// values.assume_init()
/// };
/// // Deferred initialization:
/// values[0].write(1);
/// values[1].write(2);
/// values[2].write(3);
/// let values = unsafe {values.assume_init() };
///
/// assert_eq!(*values, [1, 2, 3])
/// ```
Expand Down Expand Up @@ -722,13 +698,11 @@ impl<T> Box<[T]> {
/// #![feature(allocator_api)]
///
/// let mut values = Box::<[u32]>::try_new_uninit_slice(3)?;
/// let values = unsafe {
/// // Deferred initialization:
/// values[0].as_mut_ptr().write(1);
/// values[1].as_mut_ptr().write(2);
/// values[2].as_mut_ptr().write(3);
/// values.assume_init()
/// };
/// // Deferred initialization:
/// values[0].write(1);
/// values[1].write(2);
/// values[2].write(3);
/// let values = unsafe { values.assume_init() };
///
/// assert_eq!(*values, [1, 2, 3]);
/// # Ok::<(), std::alloc::AllocError>(())
Expand Down Expand Up @@ -814,15 +788,11 @@ impl<T, A: Allocator> Box<[T], A> {
/// use std::alloc::System;
///
/// let mut values = Box::<[u32], _>::new_uninit_slice_in(3, System);
///
/// let values = unsafe {
/// // Deferred initialization:
/// values[0].as_mut_ptr().write(1);
/// values[1].as_mut_ptr().write(2);
/// values[2].as_mut_ptr().write(3);
///
/// values.assume_init()
/// };
/// // Deferred initialization:
/// values[0].write(1);
/// values[1].write(2);
/// values[2].write(3);
/// let values = unsafe { values.assume_init() };
///
/// assert_eq!(*values, [1, 2, 3])
/// ```
Expand Down Expand Up @@ -873,13 +843,11 @@ impl<T, A: Allocator> Box<[T], A> {
/// use std::alloc::System;
///
/// let mut values = Box::<[u32], _>::try_new_uninit_slice_in(3, System)?;
/// let values = unsafe {
/// // Deferred initialization:
/// values[0].as_mut_ptr().write(1);
/// values[1].as_mut_ptr().write(2);
/// values[2].as_mut_ptr().write(3);
/// values.assume_init()
/// };
/// // Deferred initialization:
/// values[0].write(1);
/// values[1].write(2);
/// values[2].write(3);
/// let values = unsafe { values.assume_init() };
///
/// assert_eq!(*values, [1, 2, 3]);
/// # Ok::<(), std::alloc::AllocError>(())
Expand Down Expand Up @@ -959,13 +927,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
///
/// ```
/// let mut five = Box::<u32>::new_uninit();
///
/// let five: Box<u32> = unsafe {
/// // Deferred initialization:
/// five.as_mut_ptr().write(5);
///
/// five.assume_init()
/// };
/// // Deferred initialization:
/// five.write(5);
/// let five: Box<u32> = unsafe { five.assume_init() };
///
/// assert_eq!(*five, 5)
/// ```
Expand Down Expand Up @@ -1030,15 +994,11 @@ impl<T, A: Allocator> Box<[mem::MaybeUninit<T>], A> {
///
/// ```
/// let mut values = Box::<[u32]>::new_uninit_slice(3);
///
/// let values = unsafe {
/// // Deferred initialization:
/// values[0].as_mut_ptr().write(1);
/// values[1].as_mut_ptr().write(2);
/// values[2].as_mut_ptr().write(3);
///
/// values.assume_init()
/// };
/// // Deferred initialization:
/// values[0].write(1);
/// values[1].write(2);
/// values[2].write(3);
/// let values = unsafe { values.assume_init() };
///
/// assert_eq!(*values, [1, 2, 3])
/// ```
Expand Down
Loading