Skip to content

Commit c3b602a

Browse files
authoredJul 6, 2024
Add missing try_new_uninit_slice_in and try_new_zeroed_slice_in
The methods for fallible slice allocation in a given allocator were missing, which was an oversight according to rust-lang/wg-allocators#130 This PR adds them as `try_new_uninit_slice_in` and `try_new_zeroed_slice_in`. Also adds missing punctuation to the doc comments of ` try_new_uninit_slice` and `try_new_zeroed_slice`
1 parent ad4fde6 commit c3b602a

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed
 

‎alloc/src/boxed.rs

+75-2
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ impl<T> Box<[T]> {
704704
}
705705

706706
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
707-
/// the allocation fails
707+
/// the allocation fails.
708708
///
709709
/// # Examples
710710
///
@@ -739,7 +739,7 @@ impl<T> Box<[T]> {
739739
}
740740

741741
/// Constructs a new boxed slice with uninitialized contents, with the memory
742-
/// being filled with `0` bytes. Returns an error if the allocation fails
742+
/// being filled with `0` bytes. Returns an error if the allocation fails.
743743
///
744744
/// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
745745
/// of this method.
@@ -831,6 +831,79 @@ impl<T, A: Allocator> Box<[T], A> {
831831
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
832832
unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) }
833833
}
834+
835+
/// Constructs a new boxed slice with uninitialized contents in the provided allocator. Returns an error if
836+
/// the allocation fails.
837+
///
838+
/// # Examples
839+
///
840+
/// ```
841+
/// #![feature(allocator_api, new_uninit)]
842+
///
843+
/// use std::alloc::System;
844+
///
845+
/// let mut values = Box::<[u32]>::try_new_uninit_slice(3, System)?;
846+
/// let values = unsafe {
847+
/// // Deferred initialization:
848+
/// values[0].as_mut_ptr().write(1);
849+
/// values[1].as_mut_ptr().write(2);
850+
/// values[2].as_mut_ptr().write(3);
851+
/// values.assume_init()
852+
/// };
853+
///
854+
/// assert_eq!(*values, [1, 2, 3]);
855+
/// # Ok::<(), std::alloc::AllocError>(())
856+
/// ```
857+
#[unstable(feature = "allocator_api", issue = "32838")]
858+
#[inline]
859+
pub fn try_new_uninit_slice_in(len: usize, alloc: A) -> Result<Box<[mem::MaybeUninit<T>], A>, AllocError> {
860+
let ptr = if T::IS_ZST || len == 0 {
861+
NonNull::dangling()
862+
} else {
863+
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
864+
Ok(l) => l,
865+
Err(_) => return Err(AllocError),
866+
};
867+
Global.allocate(layout)?.cast()
868+
};
869+
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, alloc).into_box(len)) }
870+
}
871+
872+
/// Constructs a new boxed slice with uninitialized contents in the provided allocator, with the memory
873+
/// being filled with `0` bytes. Returns an error if the allocation fails.
874+
///
875+
/// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
876+
/// of this method.
877+
///
878+
/// # Examples
879+
///
880+
/// ```
881+
/// #![feature(allocator_api, new_uninit)]
882+
///
883+
/// use std::alloc::System;
884+
///
885+
/// let values = Box::<[u32]>::try_new_zeroed_slice(3, System)?;
886+
/// let values = unsafe { values.assume_init() };
887+
///
888+
/// assert_eq!(*values, [0, 0, 0]);
889+
/// # Ok::<(), std::alloc::AllocError>(())
890+
/// ```
891+
///
892+
/// [zeroed]: mem::MaybeUninit::zeroed
893+
#[unstable(feature = "allocator_api", issue = "32838")]
894+
#[inline]
895+
pub fn try_new_zeroed_slice_in(len: usize, alloc: A) -> Result<Box<[mem::MaybeUninit<T>], A>, AllocError> {
896+
let ptr = if T::IS_ZST || len == 0 {
897+
NonNull::dangling()
898+
} else {
899+
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
900+
Ok(l) => l,
901+
Err(_) => return Err(AllocError),
902+
};
903+
Global.allocate_zeroed(layout)?.cast()
904+
};
905+
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, alloc).into_box(len)) }
906+
}
834907
}
835908

836909
impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {

0 commit comments

Comments
 (0)
Please sign in to comment.