@@ -1268,6 +1268,18 @@ impl<T> [T] {
1268
1268
/// Splits the slice into a slice of `N`-element arrays,
1269
1269
/// assuming that there's no remainder.
1270
1270
///
1271
+ /// This is the inverse operation to [`as_flattened`].
1272
+ ///
1273
+ /// [`as_flattened`]: slice::as_flattened
1274
+ ///
1275
+ /// As this is `unsafe`, consider whether you could use [`as_chunks`] or
1276
+ /// [`as_rchunks`] instead, perhaps via something like
1277
+ /// `if let (chunks, []) = slice.as_chunks()` or
1278
+ /// `let (chunks, []) = slice.as_chunks() else { unreachable!() };`.
1279
+ ///
1280
+ /// [`as_chunks`]: slice::as_chunks
1281
+ /// [`as_rchunks`]: slice::as_rchunks
1282
+ ///
1271
1283
/// # Safety
1272
1284
///
1273
1285
/// This may only be called when
@@ -1277,7 +1289,6 @@ impl<T> [T] {
1277
1289
/// # Examples
1278
1290
///
1279
1291
/// ```
1280
- /// #![feature(slice_as_chunks)]
1281
1292
/// let slice: &[char] = &['l', 'o', 'r', 'e', 'm', '!'];
1282
1293
/// let chunks: &[[char; 1]] =
1283
1294
/// // SAFETY: 1-element chunks never have remainder
@@ -1292,7 +1303,8 @@ impl<T> [T] {
1292
1303
/// // let chunks: &[[_; 5]] = slice.as_chunks_unchecked() // The slice length is not a multiple of 5
1293
1304
/// // let chunks: &[[_; 0]] = slice.as_chunks_unchecked() // Zero-length chunks are never allowed
1294
1305
/// ```
1295
- #[ unstable( feature = "slice_as_chunks" , issue = "74985" ) ]
1306
+ #[ stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1307
+ #[ rustc_const_stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1296
1308
#[ inline]
1297
1309
#[ must_use]
1298
1310
pub const unsafe fn as_chunks_unchecked < const N : usize > ( & self ) -> & [ [ T ; N ] ] {
@@ -1312,15 +1324,27 @@ impl<T> [T] {
1312
1324
/// starting at the beginning of the slice,
1313
1325
/// and a remainder slice with length strictly less than `N`.
1314
1326
///
1327
+ /// The remainder is meaningful in the division sense. Given
1328
+ /// `let (chunks, remainder) = slice.as_chunks()`, then:
1329
+ /// - `chunks.len()` equals `slice.len() / N`,
1330
+ /// - `remainder.len()` equals `slice.len() % N`, and
1331
+ /// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1332
+ ///
1333
+ /// You can flatten the chunks back into a slice-of-`T` with [`as_flattened`].
1334
+ ///
1335
+ /// [`as_flattened`]: slice::as_flattened
1336
+ ///
1315
1337
/// # Panics
1316
1338
///
1317
- /// Panics if `N` is zero. This check will most probably get changed to a compile time
1318
- /// error before this method gets stabilized.
1339
+ /// Panics if `N` is zero.
1340
+ ///
1341
+ /// Note that this check is against a const generic parameter, not a runtime
1342
+ /// value, and thus a particular monomorphization will either always panic
1343
+ /// or it will never panic.
1319
1344
///
1320
1345
/// # Examples
1321
1346
///
1322
1347
/// ```
1323
- /// #![feature(slice_as_chunks)]
1324
1348
/// let slice = ['l', 'o', 'r', 'e', 'm'];
1325
1349
/// let (chunks, remainder) = slice.as_chunks();
1326
1350
/// assert_eq!(chunks, &[['l', 'o'], ['r', 'e']]);
@@ -1330,14 +1354,14 @@ impl<T> [T] {
1330
1354
/// If you expect the slice to be an exact multiple, you can combine
1331
1355
/// `let`-`else` with an empty slice pattern:
1332
1356
/// ```
1333
- /// #![feature(slice_as_chunks)]
1334
1357
/// let slice = ['R', 'u', 's', 't'];
1335
1358
/// let (chunks, []) = slice.as_chunks::<2>() else {
1336
1359
/// panic!("slice didn't have even length")
1337
1360
/// };
1338
1361
/// assert_eq!(chunks, &[['R', 'u'], ['s', 't']]);
1339
1362
/// ```
1340
- #[ unstable( feature = "slice_as_chunks" , issue = "74985" ) ]
1363
+ #[ stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1364
+ #[ rustc_const_stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1341
1365
#[ inline]
1342
1366
#[ track_caller]
1343
1367
#[ must_use]
@@ -1357,21 +1381,34 @@ impl<T> [T] {
1357
1381
/// starting at the end of the slice,
1358
1382
/// and a remainder slice with length strictly less than `N`.
1359
1383
///
1384
+ /// The remainder is meaningful in the division sense. Given
1385
+ /// `let (remainder, chunks) = slice.as_rchunks()`, then:
1386
+ /// - `remainder.len()` equals `slice.len() % N`,
1387
+ /// - `chunks.len()` equals `slice.len() / N`, and
1388
+ /// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1389
+ ///
1390
+ /// You can flatten the chunks back into a slice-of-`T` with [`as_flattened`].
1391
+ ///
1392
+ /// [`as_flattened`]: slice::as_flattened
1393
+ ///
1360
1394
/// # Panics
1361
1395
///
1362
- /// Panics if `N` is zero. This check will most probably get changed to a compile time
1363
- /// error before this method gets stabilized.
1396
+ /// Panics if `N` is zero.
1397
+ ///
1398
+ /// Note that this check is against a const generic parameter, not a runtime
1399
+ /// value, and thus a particular monomorphization will either always panic
1400
+ /// or it will never panic.
1364
1401
///
1365
1402
/// # Examples
1366
1403
///
1367
1404
/// ```
1368
- /// #![feature(slice_as_chunks)]
1369
1405
/// let slice = ['l', 'o', 'r', 'e', 'm'];
1370
1406
/// let (remainder, chunks) = slice.as_rchunks();
1371
1407
/// assert_eq!(remainder, &['l']);
1372
1408
/// assert_eq!(chunks, &[['o', 'r'], ['e', 'm']]);
1373
1409
/// ```
1374
- #[ unstable( feature = "slice_as_chunks" , issue = "74985" ) ]
1410
+ #[ stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1411
+ #[ rustc_const_stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1375
1412
#[ inline]
1376
1413
#[ track_caller]
1377
1414
#[ must_use]
@@ -1424,6 +1461,18 @@ impl<T> [T] {
1424
1461
/// Splits the slice into a slice of `N`-element arrays,
1425
1462
/// assuming that there's no remainder.
1426
1463
///
1464
+ /// This is the inverse operation to [`as_flattened_mut`].
1465
+ ///
1466
+ /// [`as_flattened_mut`]: slice::as_flattened_mut
1467
+ ///
1468
+ /// As this is `unsafe`, consider whether you could use [`as_chunks_mut`] or
1469
+ /// [`as_rchunks_mut`] instead, perhaps via something like
1470
+ /// `if let (chunks, []) = slice.as_chunks_mut()` or
1471
+ /// `let (chunks, []) = slice.as_chunks_mut() else { unreachable!() };`.
1472
+ ///
1473
+ /// [`as_chunks_mut`]: slice::as_chunks_mut
1474
+ /// [`as_rchunks_mut`]: slice::as_rchunks_mut
1475
+ ///
1427
1476
/// # Safety
1428
1477
///
1429
1478
/// This may only be called when
@@ -1433,7 +1482,6 @@ impl<T> [T] {
1433
1482
/// # Examples
1434
1483
///
1435
1484
/// ```
1436
- /// #![feature(slice_as_chunks)]
1437
1485
/// let slice: &mut [char] = &mut ['l', 'o', 'r', 'e', 'm', '!'];
1438
1486
/// let chunks: &mut [[char; 1]] =
1439
1487
/// // SAFETY: 1-element chunks never have remainder
@@ -1450,7 +1498,8 @@ impl<T> [T] {
1450
1498
/// // let chunks: &[[_; 5]] = slice.as_chunks_unchecked_mut() // The slice length is not a multiple of 5
1451
1499
/// // let chunks: &[[_; 0]] = slice.as_chunks_unchecked_mut() // Zero-length chunks are never allowed
1452
1500
/// ```
1453
- #[ unstable( feature = "slice_as_chunks" , issue = "74985" ) ]
1501
+ #[ stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1502
+ #[ rustc_const_stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1454
1503
#[ inline]
1455
1504
#[ must_use]
1456
1505
pub const unsafe fn as_chunks_unchecked_mut < const N : usize > ( & mut self ) -> & mut [ [ T ; N ] ] {
@@ -1470,15 +1519,27 @@ impl<T> [T] {
1470
1519
/// starting at the beginning of the slice,
1471
1520
/// and a remainder slice with length strictly less than `N`.
1472
1521
///
1522
+ /// The remainder is meaningful in the division sense. Given
1523
+ /// `let (chunks, remainder) = slice.as_chunks_mut()`, then:
1524
+ /// - `chunks.len()` equals `slice.len() / N`,
1525
+ /// - `remainder.len()` equals `slice.len() % N`, and
1526
+ /// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1527
+ ///
1528
+ /// You can flatten the chunks back into a slice-of-`T` with [`as_flattened_mut`].
1529
+ ///
1530
+ /// [`as_flattened_mut`]: slice::as_flattened_mut
1531
+ ///
1473
1532
/// # Panics
1474
1533
///
1475
- /// Panics if `N` is zero. This check will most probably get changed to a compile time
1476
- /// error before this method gets stabilized.
1534
+ /// Panics if `N` is zero.
1535
+ ///
1536
+ /// Note that this check is against a const generic parameter, not a runtime
1537
+ /// value, and thus a particular monomorphization will either always panic
1538
+ /// or it will never panic.
1477
1539
///
1478
1540
/// # Examples
1479
1541
///
1480
1542
/// ```
1481
- /// #![feature(slice_as_chunks)]
1482
1543
/// let v = &mut [0, 0, 0, 0, 0];
1483
1544
/// let mut count = 1;
1484
1545
///
@@ -1490,7 +1551,8 @@ impl<T> [T] {
1490
1551
/// }
1491
1552
/// assert_eq!(v, &[1, 1, 2, 2, 9]);
1492
1553
/// ```
1493
- #[ unstable( feature = "slice_as_chunks" , issue = "74985" ) ]
1554
+ #[ stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1555
+ #[ rustc_const_stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1494
1556
#[ inline]
1495
1557
#[ track_caller]
1496
1558
#[ must_use]
@@ -1510,15 +1572,27 @@ impl<T> [T] {
1510
1572
/// starting at the end of the slice,
1511
1573
/// and a remainder slice with length strictly less than `N`.
1512
1574
///
1575
+ /// The remainder is meaningful in the division sense. Given
1576
+ /// `let (remainder, chunks) = slice.as_rchunks_mut()`, then:
1577
+ /// - `remainder.len()` equals `slice.len() % N`,
1578
+ /// - `chunks.len()` equals `slice.len() / N`, and
1579
+ /// - `slice.len()` equals `chunks.len() * N + remainder.len()`.
1580
+ ///
1581
+ /// You can flatten the chunks back into a slice-of-`T` with [`as_flattened_mut`].
1582
+ ///
1583
+ /// [`as_flattened_mut`]: slice::as_flattened_mut
1584
+ ///
1513
1585
/// # Panics
1514
1586
///
1515
- /// Panics if `N` is zero. This check will most probably get changed to a compile time
1516
- /// error before this method gets stabilized.
1587
+ /// Panics if `N` is zero.
1588
+ ///
1589
+ /// Note that this check is against a const generic parameter, not a runtime
1590
+ /// value, and thus a particular monomorphization will either always panic
1591
+ /// or it will never panic.
1517
1592
///
1518
1593
/// # Examples
1519
1594
///
1520
1595
/// ```
1521
- /// #![feature(slice_as_chunks)]
1522
1596
/// let v = &mut [0, 0, 0, 0, 0];
1523
1597
/// let mut count = 1;
1524
1598
///
@@ -1530,7 +1604,8 @@ impl<T> [T] {
1530
1604
/// }
1531
1605
/// assert_eq!(v, &[9, 1, 1, 2, 2]);
1532
1606
/// ```
1533
- #[ unstable( feature = "slice_as_chunks" , issue = "74985" ) ]
1607
+ #[ stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1608
+ #[ rustc_const_stable( feature = "slice_as_chunks" , since = "CURRENT_RUSTC_VERSION" ) ]
1534
1609
#[ inline]
1535
1610
#[ track_caller]
1536
1611
#[ must_use]
@@ -4816,6 +4891,11 @@ impl<T> [MaybeUninit<T>] {
4816
4891
impl < T , const N : usize > [ [ T ; N ] ] {
4817
4892
/// Takes a `&[[T; N]]`, and flattens it to a `&[T]`.
4818
4893
///
4894
+ /// For the opposite operation, see [`as_chunks`] and [`as_rchunks`].
4895
+ ///
4896
+ /// [`as_chunks`]: slice::as_chunks
4897
+ /// [`as_rchunks`]: slice::as_rchunks
4898
+ ///
4819
4899
/// # Panics
4820
4900
///
4821
4901
/// This panics if the length of the resulting slice would overflow a `usize`.
@@ -4856,6 +4936,11 @@ impl<T, const N: usize> [[T; N]] {
4856
4936
4857
4937
/// Takes a `&mut [[T; N]]`, and flattens it to a `&mut [T]`.
4858
4938
///
4939
+ /// For the opposite operation, see [`as_chunks_mut`] and [`as_rchunks_mut`].
4940
+ ///
4941
+ /// [`as_chunks_mut`]: slice::as_chunks_mut
4942
+ /// [`as_rchunks_mut`]: slice::as_rchunks_mut
4943
+ ///
4859
4944
/// # Panics
4860
4945
///
4861
4946
/// This panics if the length of the resulting slice would overflow a `usize`.
0 commit comments