Skip to content

Commit c49ddc0

Browse files
committed
Improve rustdocs on slice_as_chunks methods
Also mention them from `as_flattened(_mut)`.
1 parent 6cfdd53 commit c49ddc0

File tree

1 file changed

+94
-8
lines changed

1 file changed

+94
-8
lines changed

library/core/src/slice/mod.rs

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,18 @@ impl<T> [T] {
12621262
/// Splits the slice into a slice of `N`-element arrays,
12631263
/// assuming that there's no remainder.
12641264
///
1265+
/// This is the inverse operation to [`as_flattened`].
1266+
///
1267+
/// [`as_flattened`]: slice::as_flattened
1268+
///
1269+
/// As this is `unsafe`, consider whether you could use [`as_chunks`] or
1270+
/// [`as_rchunks`] instead, perhaps via something like
1271+
/// `if let (chunks, []) = slice.as_chunks()` or
1272+
/// `let (chunks, []) = slice.as_chunks() else { unreachable!() };`.
1273+
///
1274+
/// [`as_chunks`]: slice::as_chunks
1275+
/// [`as_rchunks`]: slice::as_rchunks
1276+
///
12651277
/// # Safety
12661278
///
12671279
/// This may only be called when
@@ -1306,10 +1318,23 @@ impl<T> [T] {
13061318
/// starting at the beginning of the slice,
13071319
/// and a remainder slice with length strictly less than `N`.
13081320
///
1321+
/// The remainder is meaningful in the division sense. Given
1322+
/// `let (chunks, remainder) = slice.as_chunks()`, then:
1323+
/// - `chunks.len()` equals `slice.len() / N`,
1324+
/// - `remainder.len()` equals `slice.len() % N`, and
1325+
/// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1326+
///
1327+
/// You can flatten the chunks back into a slice-of-`T` with [`as_flattened`].
1328+
///
1329+
/// [`as_flattened`]: slice::as_flattened
1330+
///
13091331
/// # Panics
13101332
///
1311-
/// Panics if `N` is zero. This check will most probably get changed to a compile time
1312-
/// error before this method gets stabilized.
1333+
/// Panics if `N` is zero.
1334+
///
1335+
/// Note that this check is against a const generic parameter, not a runtime
1336+
/// value, and thus a particular monomorphization will either always panic
1337+
/// or it will never panic.
13131338
///
13141339
/// # Examples
13151340
///
@@ -1350,10 +1375,23 @@ impl<T> [T] {
13501375
/// starting at the end of the slice,
13511376
/// and a remainder slice with length strictly less than `N`.
13521377
///
1378+
/// The remainder is meaningful in the division sense. Given
1379+
/// `let (remainder, chunks) = slice.as_rchunks()`, then:
1380+
/// - `remainder.len()` equals `slice.len() % N`,
1381+
/// - `chunks.len()` equals `slice.len() / N`, and
1382+
/// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1383+
///
1384+
/// You can flatten the chunks back into a slice-of-`T` with [`as_flattened`].
1385+
///
1386+
/// [`as_flattened`]: slice::as_flattened
1387+
///
13531388
/// # Panics
13541389
///
1355-
/// Panics if `N` is zero. This check will most probably get changed to a compile time
1356-
/// error before this method gets stabilized.
1390+
/// Panics if `N` is zero.
1391+
///
1392+
/// Note that this check is against a const generic parameter, not a runtime
1393+
/// value, and thus a particular monomorphization will either always panic
1394+
/// or it will never panic.
13571395
///
13581396
/// # Examples
13591397
///
@@ -1417,6 +1455,18 @@ impl<T> [T] {
14171455
/// Splits the slice into a slice of `N`-element arrays,
14181456
/// assuming that there's no remainder.
14191457
///
1458+
/// This is the inverse operation to [`as_flattened_mut`].
1459+
///
1460+
/// [`as_flattened_mut`]: slice::as_flattened_mut
1461+
///
1462+
/// As this is `unsafe`, consider whether you could use [`as_chunks_mut`] or
1463+
/// [`as_rchunks_mut`] instead, perhaps via something like
1464+
/// `if let (chunks, []) = slice.as_chunks_mut()` or
1465+
/// `let (chunks, []) = slice.as_chunks_mut() else { unreachable!() };`.
1466+
///
1467+
/// [`as_chunks_mut`]: slice::as_chunks_mut
1468+
/// [`as_rchunks_mut`]: slice::as_rchunks_mut
1469+
///
14201470
/// # Safety
14211471
///
14221472
/// This may only be called when
@@ -1463,10 +1513,23 @@ impl<T> [T] {
14631513
/// starting at the beginning of the slice,
14641514
/// and a remainder slice with length strictly less than `N`.
14651515
///
1516+
/// The remainder is meaningful in the division sense. Given
1517+
/// `let (chunks, remainder) = slice.as_chunks_mut()`, then:
1518+
/// - `chunks.len()` equals `slice.len() / N`,
1519+
/// - `remainder.len()` equals `slice.len() % N`, and
1520+
/// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1521+
///
1522+
/// You can flatten the chunks back into a slice-of-`T` with [`as_flattened_mut`].
1523+
///
1524+
/// [`as_flattened_mut`]: slice::as_flattened_mut
1525+
///
14661526
/// # Panics
14671527
///
1468-
/// Panics if `N` is zero. This check will most probably get changed to a compile time
1469-
/// error before this method gets stabilized.
1528+
/// Panics if `N` is zero.
1529+
///
1530+
/// Note that this check is against a const generic parameter, not a runtime
1531+
/// value, and thus a particular monomorphization will either always panic
1532+
/// or it will never panic.
14701533
///
14711534
/// # Examples
14721535
///
@@ -1503,10 +1566,23 @@ impl<T> [T] {
15031566
/// starting at the end of the slice,
15041567
/// and a remainder slice with length strictly less than `N`.
15051568
///
1569+
/// The remainder is meaningful in the division sense. Given
1570+
/// `let (remainder, chunks) = slice.as_rchunks_mut()`, then:
1571+
/// - `remainder.len()` equals `slice.len() % N`,
1572+
/// - `chunks.len()` equals `slice.len() / N`, and
1573+
/// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1574+
///
1575+
/// You can flatten the chunks back into a slice-of-`T` with [`as_flattened_mut`].
1576+
///
1577+
/// [`as_flattened_mut`]: slice::as_flattened_mut
1578+
///
15061579
/// # Panics
15071580
///
1508-
/// Panics if `N` is zero. This check will most probably get changed to a compile time
1509-
/// error before this method gets stabilized.
1581+
/// Panics if `N` is zero.
1582+
///
1583+
/// Note that this check is against a const generic parameter, not a runtime
1584+
/// value, and thus a particular monomorphization will either always panic
1585+
/// or it will never panic.
15101586
///
15111587
/// # Examples
15121588
///
@@ -4809,6 +4885,11 @@ impl<T> [MaybeUninit<T>] {
48094885
impl<T, const N: usize> [[T; N]] {
48104886
/// Takes a `&[[T; N]]`, and flattens it to a `&[T]`.
48114887
///
4888+
/// For the opposite operation, see [`as_chunks`] and [`as_rchunks`].
4889+
///
4890+
/// [`as_chunks`]: slice::as_chunks
4891+
/// [`as_rchunks`]: slice::as_rchunks
4892+
///
48124893
/// # Panics
48134894
///
48144895
/// This panics if the length of the resulting slice would overflow a `usize`.
@@ -4849,6 +4930,11 @@ impl<T, const N: usize> [[T; N]] {
48494930

48504931
/// Takes a `&mut [[T; N]]`, and flattens it to a `&mut [T]`.
48514932
///
4933+
/// For the opposite operation, see [`as_chunks_mut`] and [`as_rchunks_mut`].
4934+
///
4935+
/// [`as_chunks_mut`]: slice::as_chunks_mut
4936+
/// [`as_rchunks_mut`]: slice::as_rchunks_mut
4937+
///
48524938
/// # Panics
48534939
///
48544940
/// This panics if the length of the resulting slice would overflow a `usize`.

0 commit comments

Comments
 (0)