Skip to content

Commit 9c63e94

Browse files
authored
Rollup merge of #156810 - Teufelchen1:fix_doc_typo, r=SimonSapin
Fix doc typo in Vec::into_array and convert Arc/Box/Rc::into_arry to -> Result Hello 🪼, the first commit fixes a documentation mistake added in the recent PR #156234 The second commit addresses three function signature changes as defined by the tracking issue #148082 It converts all `.into_array() -> Option<>` into `.into_array() -> Result<>` The third commit is a fixup for the second. The change is needed as calling `unwrap()` on `Err(E)` requires `E` to implement `Debug`. I thought `Vec<T, A> where T: Debug` is not an acceptable solution, so I tried to come up with my own. I am *NOT* confident in my `unsafe {}` code due to lack of expierence, please check extra carefully. Appreciating feedback & greetings from the RustWeek, Thanks!
2 parents 81eb844 + 8b382d6 commit 9c63e94

4 files changed

Lines changed: 28 additions & 15 deletions

File tree

library/alloc/src/boxed.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,9 @@ impl<T, A: Allocator> Box<[T], A> {
11421142
///
11431143
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
11441144
///
1145-
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
1145+
/// # Errors
1146+
///
1147+
/// Returns the original `Box<[T]>` in the `Err` variant if `self.len()` does not equal `N`.
11461148
///
11471149
/// # Examples
11481150
///
@@ -1155,16 +1157,16 @@ impl<T, A: Allocator> Box<[T], A> {
11551157
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
11561158
#[inline]
11571159
#[must_use]
1158-
pub fn into_array<const N: usize>(self) -> Option<Box<[T; N], A>> {
1160+
pub fn into_array<const N: usize>(self) -> Result<Box<[T; N], A>, Self> {
11591161
if self.len() == N {
11601162
let (ptr, alloc) = Self::into_raw_with_allocator(self);
11611163
let ptr = ptr as *mut [T; N];
11621164

11631165
// 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.
11641166
let me = unsafe { Box::from_raw_in(ptr, alloc) };
1165-
Some(me)
1167+
Ok(me)
11661168
} else {
1167-
None
1169+
Err(self)
11681170
}
11691171
}
11701172
}

library/alloc/src/rc.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,9 @@ impl<T, A: Allocator> Rc<[T], A> {
12451245
///
12461246
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
12471247
///
1248-
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
1248+
/// # Errors
1249+
///
1250+
/// Returns the original `Rc<[T]>` in the `Err` variant if `self.len()` does not equal `N`.
12491251
///
12501252
/// # Examples
12511253
///
@@ -1260,16 +1262,16 @@ impl<T, A: Allocator> Rc<[T], A> {
12601262
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
12611263
#[inline]
12621264
#[must_use]
1263-
pub fn into_array<const N: usize>(self) -> Option<Rc<[T; N], A>> {
1265+
pub fn into_array<const N: usize>(self) -> Result<Rc<[T; N], A>, Self> {
12641266
if self.len() == N {
12651267
let (ptr, alloc) = Self::into_raw_with_allocator(self);
12661268
let ptr = ptr as *const [T; N];
12671269

12681270
// 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.
12691271
let me = unsafe { Rc::from_raw_in(ptr, alloc) };
1270-
Some(me)
1272+
Ok(me)
12711273
} else {
1272-
None
1274+
Err(self)
12731275
}
12741276
}
12751277
}

library/alloc/src/sync.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,9 @@ impl<T, A: Allocator> Arc<[T], A> {
13921392
///
13931393
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
13941394
///
1395-
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
1395+
/// # Errors
1396+
///
1397+
/// Returns the original `Arc<[T]>` in the `Err` variant if `self.len()` does not equal `N`.
13961398
///
13971399
/// # Examples
13981400
///
@@ -1407,16 +1409,16 @@ impl<T, A: Allocator> Arc<[T], A> {
14071409
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
14081410
#[inline]
14091411
#[must_use]
1410-
pub fn into_array<const N: usize>(self) -> Option<Arc<[T; N], A>> {
1412+
pub fn into_array<const N: usize>(self) -> Result<Arc<[T; N], A>, Self> {
14111413
if self.len() == N {
14121414
let (ptr, alloc) = Self::into_raw_with_allocator(self);
14131415
let ptr = ptr as *const [T; N];
14141416

14151417
// 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.
14161418
let me = unsafe { Arc::from_raw_in(ptr, alloc) };
1417-
Some(me)
1419+
Ok(me)
14181420
} else {
1419-
None
1421+
Err(self)
14201422
}
14211423
}
14221424
}

library/alloc/src/vec/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,10 +1740,13 @@ impl<T, A: Allocator> Vec<T, A> {
17401740
}
17411741
}
17421742

1743-
/// Converts the Vec into a boxed array. This conversion will discard any spare capacity, if there is any, see [`Vec::shrink_to_fit`].
1743+
/// Converts the Vec into a boxed array. This conversion will discard any spare capacity,
1744+
/// if there is any, see [`Vec::shrink_to_fit`].
17441745
/// 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).
17451746
///
1746-
/// If `N` is not exactly equal to [`Vec::len`], then this method returns `None`.
1747+
/// # Errors
1748+
///
1749+
/// Returns the original `Vec<T>` in the `Err` variant if [`Vec::len`] does not equal `N`.
17471750
///
17481751
/// # Examples
17491752
///
@@ -1758,7 +1761,11 @@ impl<T, A: Allocator> Vec<T, A> {
17581761
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
17591762
#[must_use]
17601763
pub fn into_array<const N: usize>(self) -> Result<Box<[T; N], A>, Self> {
1761-
if self.len() == N { Ok(self.into_boxed_slice().into_array().unwrap()) } else { Err(self) }
1764+
if self.len() == N {
1765+
Ok(self.into_boxed_slice().into_array().ok().unwrap())
1766+
} else {
1767+
Err(self)
1768+
}
17621769
}
17631770

17641771
/// Shortens the vector, keeping the first `len` elements and dropping

0 commit comments

Comments
 (0)