diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 80ce7a699efd..bd3f10a16dd9 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -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(self) -> Option> { + pub fn into_array(self) -> Result, 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) } } } diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 2905170d22a7..3f223ad1c9d2 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -1245,7 +1245,9 @@ pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Rc<[mem::MaybeUninit], 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], A> #[unstable(feature = "alloc_slice_into_array", issue = "148082")] #[inline] #[must_use] - pub fn into_array(self) -> Option> { + pub fn into_array(self) -> Result, 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) } } } diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 229fcd2b429c..c0e93bd61c21 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -1392,7 +1392,9 @@ pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Arc<[mem::MaybeUninit], 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], A #[unstable(feature = "alloc_slice_into_array", issue = "148082")] #[inline] #[must_use] - pub fn into_array(self) -> Option> { + pub fn into_array(self) -> Result, 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) } } } diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index de04eaf97226..d2b7837d98fa 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -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(self) -> Result, 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