Skip to content

Commit cbd5a2f

Browse files
authored
Unrolled build for rust-lang#136976
Rollup merge of rust-lang#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.
2 parents bdc97d1 + 2f27236 commit cbd5a2f

File tree

1 file changed

+44
-84
lines changed

1 file changed

+44
-84
lines changed

library/alloc/src/boxed.rs

+44-84
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,9 @@ impl<T> Box<T> {
280280
///
281281
/// ```
282282
/// let mut five = Box::<u32>::new_uninit();
283-
///
284-
/// let five = unsafe {
285-
/// // Deferred initialization:
286-
/// five.as_mut_ptr().write(5);
287-
///
288-
/// five.assume_init()
289-
/// };
283+
/// // Deferred initialization:
284+
/// five.write(5);
285+
/// let five = unsafe { five.assume_init() };
290286
///
291287
/// assert_eq!(*five, 5)
292288
/// ```
@@ -367,13 +363,9 @@ impl<T> Box<T> {
367363
/// #![feature(allocator_api)]
368364
///
369365
/// let mut five = Box::<u32>::try_new_uninit()?;
370-
///
371-
/// let five = unsafe {
372-
/// // Deferred initialization:
373-
/// five.as_mut_ptr().write(5);
374-
///
375-
/// five.assume_init()
376-
/// };
366+
/// // Deferred initialization:
367+
/// five.write(5);
368+
/// let five = unsafe { five.assume_init() };
377369
///
378370
/// assert_eq!(*five, 5);
379371
/// # Ok::<(), std::alloc::AllocError>(())
@@ -435,10 +427,8 @@ impl<T, A: Allocator> Box<T, A> {
435427
A: Allocator,
436428
{
437429
let mut boxed = Self::new_uninit_in(alloc);
438-
unsafe {
439-
boxed.as_mut_ptr().write(x);
440-
boxed.assume_init()
441-
}
430+
boxed.write(x);
431+
unsafe { boxed.assume_init() }
442432
}
443433

444434
/// Allocates memory in the given allocator then places `x` into it,
@@ -463,10 +453,8 @@ impl<T, A: Allocator> Box<T, A> {
463453
A: Allocator,
464454
{
465455
let mut boxed = Self::try_new_uninit_in(alloc)?;
466-
unsafe {
467-
boxed.as_mut_ptr().write(x);
468-
Ok(boxed.assume_init())
469-
}
456+
boxed.write(x);
457+
unsafe { Ok(boxed.assume_init()) }
470458
}
471459

472460
/// Constructs a new box with uninitialized contents in the provided allocator.
@@ -479,13 +467,9 @@ impl<T, A: Allocator> Box<T, A> {
479467
/// use std::alloc::System;
480468
///
481469
/// let mut five = Box::<u32, _>::new_uninit_in(System);
482-
///
483-
/// let five = unsafe {
484-
/// // Deferred initialization:
485-
/// five.as_mut_ptr().write(5);
486-
///
487-
/// five.assume_init()
488-
/// };
470+
/// // Deferred initialization:
471+
/// five.write(5);
472+
/// let five = unsafe { five.assume_init() };
489473
///
490474
/// assert_eq!(*five, 5)
491475
/// ```
@@ -517,13 +501,9 @@ impl<T, A: Allocator> Box<T, A> {
517501
/// use std::alloc::System;
518502
///
519503
/// let mut five = Box::<u32, _>::try_new_uninit_in(System)?;
520-
///
521-
/// let five = unsafe {
522-
/// // Deferred initialization:
523-
/// five.as_mut_ptr().write(5);
524-
///
525-
/// five.assume_init()
526-
/// };
504+
/// // Deferred initialization:
505+
/// five.write(5);
506+
/// let five = unsafe { five.assume_init() };
527507
///
528508
/// assert_eq!(*five, 5);
529509
/// # Ok::<(), std::alloc::AllocError>(())
@@ -669,15 +649,11 @@ impl<T> Box<[T]> {
669649
///
670650
/// ```
671651
/// let mut values = Box::<[u32]>::new_uninit_slice(3);
672-
///
673-
/// let values = unsafe {
674-
/// // Deferred initialization:
675-
/// values[0].as_mut_ptr().write(1);
676-
/// values[1].as_mut_ptr().write(2);
677-
/// values[2].as_mut_ptr().write(3);
678-
///
679-
/// values.assume_init()
680-
/// };
652+
/// // Deferred initialization:
653+
/// values[0].write(1);
654+
/// values[1].write(2);
655+
/// values[2].write(3);
656+
/// let values = unsafe {values.assume_init() };
681657
///
682658
/// assert_eq!(*values, [1, 2, 3])
683659
/// ```
@@ -722,13 +698,11 @@ impl<T> Box<[T]> {
722698
/// #![feature(allocator_api)]
723699
///
724700
/// let mut values = Box::<[u32]>::try_new_uninit_slice(3)?;
725-
/// let values = unsafe {
726-
/// // Deferred initialization:
727-
/// values[0].as_mut_ptr().write(1);
728-
/// values[1].as_mut_ptr().write(2);
729-
/// values[2].as_mut_ptr().write(3);
730-
/// values.assume_init()
731-
/// };
701+
/// // Deferred initialization:
702+
/// values[0].write(1);
703+
/// values[1].write(2);
704+
/// values[2].write(3);
705+
/// let values = unsafe { values.assume_init() };
732706
///
733707
/// assert_eq!(*values, [1, 2, 3]);
734708
/// # Ok::<(), std::alloc::AllocError>(())
@@ -814,15 +788,11 @@ impl<T, A: Allocator> Box<[T], A> {
814788
/// use std::alloc::System;
815789
///
816790
/// let mut values = Box::<[u32], _>::new_uninit_slice_in(3, System);
817-
///
818-
/// let values = unsafe {
819-
/// // Deferred initialization:
820-
/// values[0].as_mut_ptr().write(1);
821-
/// values[1].as_mut_ptr().write(2);
822-
/// values[2].as_mut_ptr().write(3);
823-
///
824-
/// values.assume_init()
825-
/// };
791+
/// // Deferred initialization:
792+
/// values[0].write(1);
793+
/// values[1].write(2);
794+
/// values[2].write(3);
795+
/// let values = unsafe { values.assume_init() };
826796
///
827797
/// assert_eq!(*values, [1, 2, 3])
828798
/// ```
@@ -873,13 +843,11 @@ impl<T, A: Allocator> Box<[T], A> {
873843
/// use std::alloc::System;
874844
///
875845
/// let mut values = Box::<[u32], _>::try_new_uninit_slice_in(3, System)?;
876-
/// let values = unsafe {
877-
/// // Deferred initialization:
878-
/// values[0].as_mut_ptr().write(1);
879-
/// values[1].as_mut_ptr().write(2);
880-
/// values[2].as_mut_ptr().write(3);
881-
/// values.assume_init()
882-
/// };
846+
/// // Deferred initialization:
847+
/// values[0].write(1);
848+
/// values[1].write(2);
849+
/// values[2].write(3);
850+
/// let values = unsafe { values.assume_init() };
883851
///
884852
/// assert_eq!(*values, [1, 2, 3]);
885853
/// # Ok::<(), std::alloc::AllocError>(())
@@ -959,13 +927,9 @@ impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
959927
///
960928
/// ```
961929
/// let mut five = Box::<u32>::new_uninit();
962-
///
963-
/// let five: Box<u32> = unsafe {
964-
/// // Deferred initialization:
965-
/// five.as_mut_ptr().write(5);
966-
///
967-
/// five.assume_init()
968-
/// };
930+
/// // Deferred initialization:
931+
/// five.write(5);
932+
/// let five: Box<u32> = unsafe { five.assume_init() };
969933
///
970934
/// assert_eq!(*five, 5)
971935
/// ```
@@ -1030,15 +994,11 @@ impl<T, A: Allocator> Box<[mem::MaybeUninit<T>], A> {
1030994
///
1031995
/// ```
1032996
/// let mut values = Box::<[u32]>::new_uninit_slice(3);
1033-
///
1034-
/// let values = unsafe {
1035-
/// // Deferred initialization:
1036-
/// values[0].as_mut_ptr().write(1);
1037-
/// values[1].as_mut_ptr().write(2);
1038-
/// values[2].as_mut_ptr().write(3);
1039-
///
1040-
/// values.assume_init()
1041-
/// };
997+
/// // Deferred initialization:
998+
/// values[0].write(1);
999+
/// values[1].write(2);
1000+
/// values[2].write(3);
1001+
/// let values = unsafe { values.assume_init() };
10421002
///
10431003
/// assert_eq!(*values, [1, 2, 3])
10441004
/// ```

0 commit comments

Comments
 (0)