Return result instead of option by Arc/Rc/Box::into_array

This commit is contained in:
Bennet Blischke
2026-05-21 18:21:37 +02:00
parent d9ab6d6e2d
commit 8b382d62df
4 changed files with 23 additions and 13 deletions
+6 -4
View File
@@ -1142,7 +1142,9 @@ pub fn try_new_zeroed_slice_in(
///
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
/// # Errors
///
/// Returns the original `Box<[T]>` in the `Err` variant if `self.len()` does not equal `N`.
///
/// # Examples
///
@@ -1155,16 +1157,16 @@ pub fn try_new_zeroed_slice_in(
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
#[inline]
#[must_use]
pub fn into_array<const N: usize>(self) -> Option<Box<[T; N], A>> {
pub fn into_array<const N: usize>(self) -> Result<Box<[T; N], A>, Self> {
if self.len() == N {
let (ptr, alloc) = Self::into_raw_with_allocator(self);
let ptr = ptr as *mut [T; N];
// SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
let me = unsafe { Box::from_raw_in(ptr, alloc) };
Some(me)
Ok(me)
} else {
None
Err(self)
}
}
}
+6 -4
View File
@@ -1245,7 +1245,9 @@ pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Rc<[mem::MaybeUninit<T>], A>
///
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
/// # Errors
///
/// Returns the original `Rc<[T]>` in the `Err` variant if `self.len()` does not equal `N`.
///
/// # Examples
///
@@ -1260,16 +1262,16 @@ pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Rc<[mem::MaybeUninit<T>], A>
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
#[inline]
#[must_use]
pub fn into_array<const N: usize>(self) -> Option<Rc<[T; N], A>> {
pub fn into_array<const N: usize>(self) -> Result<Rc<[T; N], A>, Self> {
if self.len() == N {
let (ptr, alloc) = Self::into_raw_with_allocator(self);
let ptr = ptr as *const [T; N];
// SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
let me = unsafe { Rc::from_raw_in(ptr, alloc) };
Some(me)
Ok(me)
} else {
None
Err(self)
}
}
}
+6 -4
View File
@@ -1392,7 +1392,9 @@ pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Arc<[mem::MaybeUninit<T>], A
///
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
/// # Errors
///
/// Returns the original `Arc<[T]>` in the `Err` variant if `self.len()` does not equal `N`.
///
/// # Examples
///
@@ -1407,16 +1409,16 @@ pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Arc<[mem::MaybeUninit<T>], A
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
#[inline]
#[must_use]
pub fn into_array<const N: usize>(self) -> Option<Arc<[T; N], A>> {
pub fn into_array<const N: usize>(self) -> Result<Arc<[T; N], A>, Self> {
if self.len() == N {
let (ptr, alloc) = Self::into_raw_with_allocator(self);
let ptr = ptr as *const [T; N];
// SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
let me = unsafe { Arc::from_raw_in(ptr, alloc) };
Some(me)
Ok(me)
} else {
None
Err(self)
}
}
}
+5 -1
View File
@@ -1761,7 +1761,11 @@ pub fn into_boxed_slice(mut self) -> Box<[T], A> {
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
#[must_use]
pub fn into_array<const N: usize>(self) -> Result<Box<[T; N], A>, Self> {
if self.len() == N { Ok(self.into_boxed_slice().into_array().unwrap()) } else { Err(self) }
if self.len() == N {
Ok(self.into_boxed_slice().into_array().ok().unwrap())
} else {
Err(self)
}
}
/// Shortens the vector, keeping the first `len` elements and dropping