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 8150767339e1..249148951768 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 17476a4e2b1e..597d26d3239a 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 27f85666d719..d2b7837d98fa 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1740,10 +1740,13 @@ pub fn into_boxed_slice(mut self) -> Box<[T], A> { } } - /// Converts the Vec into a boxed array. This conversion will discard any spare capacity, if there is any, see [`Vec::shrink_to_fit`]. + /// Converts the Vec into a boxed array. This conversion will discard any spare capacity, + /// if there is any, see [`Vec::shrink_to_fit`]. /// If you merely wish for a reference to an array, use [`as_array`](https://doc.rust-lang.org/stable/std/primitive.slice.html#method.as_array). /// - /// If `N` is not exactly equal to [`Vec::len`], then this method returns `None`. + /// # Errors + /// + /// Returns the original `Vec` in the `Err` variant if [`Vec::len`] does not equal `N`. /// /// # Examples /// @@ -1758,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