Skip to content

Commit 121320d

Browse files
committed
Auto merge of #54580 - sdroege:rchunks, r=SimonSapin
Add slice::rchunks(), rchunks_mut(), rchunks_exact() and rchunks_exact_mut() These work exactly like the normal chunks iterators but start creating chunks from the end of the slice. ---- The new iterators were motivated by a [comment](#47115 (comment)) by @DutchGhost. ~~~This currently includes the commits from #54537 to not have to rename things twice or have merge conflicts. I'll force-push a new version of the branch ones those are in master.~~~ Also the stabilization tracking issue is just some number right now. I'll create the corresponding issue once this is reviewed and otherwise mergeable. cc @DutchGhost
2 parents 40123a1 + 80a8e5c commit 121320d

File tree

7 files changed

+1058
-35
lines changed

7 files changed

+1058
-35
lines changed

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
#![feature(const_vec_new)]
121121
#![feature(slice_partition_dedup)]
122122
#![feature(maybe_uninit)]
123+
#![feature(rchunks)]
123124

124125
// Allow testing this library
125126

src/liballoc/slice.rs

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ pub use core::slice::{from_ref, from_mut};
125125
pub use core::slice::SliceIndex;
126126
#[unstable(feature = "chunks_exact", issue = "47115")]
127127
pub use core::slice::{ChunksExact, ChunksExactMut};
128+
#[unstable(feature = "rchunks", issue = "55177")]
129+
pub use core::slice::{RChunks, RChunksMut, RChunksExact, RChunksExactMut};
128130

129131
////////////////////////////////////////////////////////////////////////////////
130132
// Basic slice extension methods

src/liballoc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![feature(try_reserve)]
2121
#![feature(unboxed_closures)]
2222
#![feature(chunks_exact)]
23+
#![feature(rchunks)]
2324
#![feature(repeat_generic_slice)]
2425

2526
extern crate alloc_system;

src/liballoc/tests/slice.rs

+114-2
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,54 @@ fn test_chunks_exactator_0() {
998998
let _it = v.chunks_exact(0);
999999
}
10001000

1001+
#[test]
1002+
fn test_rchunksator() {
1003+
let v = &[1, 2, 3, 4, 5];
1004+
1005+
assert_eq!(v.rchunks(2).len(), 3);
1006+
1007+
let chunks: &[&[_]] = &[&[4, 5], &[2, 3], &[1]];
1008+
assert_eq!(v.rchunks(2).collect::<Vec<_>>(), chunks);
1009+
let chunks: &[&[_]] = &[&[3, 4, 5], &[1, 2]];
1010+
assert_eq!(v.rchunks(3).collect::<Vec<_>>(), chunks);
1011+
let chunks: &[&[_]] = &[&[1, 2, 3, 4, 5]];
1012+
assert_eq!(v.rchunks(6).collect::<Vec<_>>(), chunks);
1013+
1014+
let chunks: &[&[_]] = &[&[1], &[2, 3], &[4, 5]];
1015+
assert_eq!(v.rchunks(2).rev().collect::<Vec<_>>(), chunks);
1016+
}
1017+
1018+
#[test]
1019+
#[should_panic]
1020+
fn test_rchunksator_0() {
1021+
let v = &[1, 2, 3, 4];
1022+
let _it = v.rchunks(0);
1023+
}
1024+
1025+
#[test]
1026+
fn test_rchunks_exactator() {
1027+
let v = &[1, 2, 3, 4, 5];
1028+
1029+
assert_eq!(v.rchunks_exact(2).len(), 2);
1030+
1031+
let chunks: &[&[_]] = &[&[4, 5], &[2, 3]];
1032+
assert_eq!(v.rchunks_exact(2).collect::<Vec<_>>(), chunks);
1033+
let chunks: &[&[_]] = &[&[3, 4, 5]];
1034+
assert_eq!(v.rchunks_exact(3).collect::<Vec<_>>(), chunks);
1035+
let chunks: &[&[_]] = &[];
1036+
assert_eq!(v.rchunks_exact(6).collect::<Vec<_>>(), chunks);
1037+
1038+
let chunks: &[&[_]] = &[&[2, 3], &[4, 5]];
1039+
assert_eq!(v.rchunks_exact(2).rev().collect::<Vec<_>>(), chunks);
1040+
}
1041+
1042+
#[test]
1043+
#[should_panic]
1044+
fn test_rchunks_exactator_0() {
1045+
let v = &[1, 2, 3, 4];
1046+
let _it = v.rchunks_exact(0);
1047+
}
1048+
10011049
#[test]
10021050
fn test_reverse_part() {
10031051
let mut values = [1, 2, 3, 4, 5];
@@ -1205,7 +1253,7 @@ fn test_get_mut() {
12051253
#[test]
12061254
fn test_mut_chunks() {
12071255
let mut v = [0, 1, 2, 3, 4, 5, 6];
1208-
assert_eq!(v.chunks_mut(2).len(), 4);
1256+
assert_eq!(v.chunks_mut(3).len(), 3);
12091257
for (i, chunk) in v.chunks_mut(3).enumerate() {
12101258
for x in chunk {
12111259
*x = i as u8;
@@ -1237,7 +1285,7 @@ fn test_mut_chunks_0() {
12371285
#[test]
12381286
fn test_mut_chunks_exact() {
12391287
let mut v = [0, 1, 2, 3, 4, 5, 6];
1240-
assert_eq!(v.chunks_exact_mut(2).len(), 3);
1288+
assert_eq!(v.chunks_exact_mut(3).len(), 2);
12411289
for (i, chunk) in v.chunks_exact_mut(3).enumerate() {
12421290
for x in chunk {
12431291
*x = i as u8;
@@ -1266,6 +1314,70 @@ fn test_mut_chunks_exact_0() {
12661314
let _it = v.chunks_exact_mut(0);
12671315
}
12681316

1317+
#[test]
1318+
fn test_mut_rchunks() {
1319+
let mut v = [0, 1, 2, 3, 4, 5, 6];
1320+
assert_eq!(v.rchunks_mut(3).len(), 3);
1321+
for (i, chunk) in v.rchunks_mut(3).enumerate() {
1322+
for x in chunk {
1323+
*x = i as u8;
1324+
}
1325+
}
1326+
let result = [2, 1, 1, 1, 0, 0, 0];
1327+
assert_eq!(v, result);
1328+
}
1329+
1330+
#[test]
1331+
fn test_mut_rchunks_rev() {
1332+
let mut v = [0, 1, 2, 3, 4, 5, 6];
1333+
for (i, chunk) in v.rchunks_mut(3).rev().enumerate() {
1334+
for x in chunk {
1335+
*x = i as u8;
1336+
}
1337+
}
1338+
let result = [0, 1, 1, 1, 2, 2, 2];
1339+
assert_eq!(v, result);
1340+
}
1341+
1342+
#[test]
1343+
#[should_panic]
1344+
fn test_mut_rchunks_0() {
1345+
let mut v = [1, 2, 3, 4];
1346+
let _it = v.rchunks_mut(0);
1347+
}
1348+
1349+
#[test]
1350+
fn test_mut_rchunks_exact() {
1351+
let mut v = [0, 1, 2, 3, 4, 5, 6];
1352+
assert_eq!(v.rchunks_exact_mut(3).len(), 2);
1353+
for (i, chunk) in v.rchunks_exact_mut(3).enumerate() {
1354+
for x in chunk {
1355+
*x = i as u8;
1356+
}
1357+
}
1358+
let result = [0, 1, 1, 1, 0, 0, 0];
1359+
assert_eq!(v, result);
1360+
}
1361+
1362+
#[test]
1363+
fn test_mut_rchunks_exact_rev() {
1364+
let mut v = [0, 1, 2, 3, 4, 5, 6];
1365+
for (i, chunk) in v.rchunks_exact_mut(3).rev().enumerate() {
1366+
for x in chunk {
1367+
*x = i as u8;
1368+
}
1369+
}
1370+
let result = [0, 0, 0, 0, 1, 1, 1];
1371+
assert_eq!(v, result);
1372+
}
1373+
1374+
#[test]
1375+
#[should_panic]
1376+
fn test_mut_rchunks_exact_0() {
1377+
let mut v = [1, 2, 3, 4];
1378+
let _it = v.rchunks_exact_mut(0);
1379+
}
1380+
12691381
#[test]
12701382
fn test_mut_last() {
12711383
let mut x = [1, 2, 3, 4, 5];

0 commit comments

Comments
 (0)