Skip to content

Commit

Permalink
Add slice::rchunks(), rchunks_mut(), rchunks_exact() and rchunks_exac…
Browse files Browse the repository at this point in the history
…t_mut()

These work exactly like the normal chunks iterators but start creating
chunks from the end of the slice.

See rust-lang#55177 for the tracking issue
  • Loading branch information
sdroege committed Oct 18, 2018
1 parent 567557f commit 80a8e5c
Show file tree
Hide file tree
Showing 7 changed files with 1,058 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
#![feature(const_vec_new)]
#![feature(slice_partition_dedup)]
#![feature(maybe_uninit)]
#![feature(rchunks)]

// Allow testing this library

Expand Down
2 changes: 2 additions & 0 deletions src/liballoc/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub use core::slice::{from_ref, from_mut};
pub use core::slice::SliceIndex;
#[unstable(feature = "chunks_exact", issue = "47115")]
pub use core::slice::{ChunksExact, ChunksExactMut};
#[unstable(feature = "rchunks", issue = "55177")]
pub use core::slice::{RChunks, RChunksMut, RChunksExact, RChunksExactMut};

////////////////////////////////////////////////////////////////////////////////
// Basic slice extension methods
Expand Down
1 change: 1 addition & 0 deletions src/liballoc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#![feature(try_reserve)]
#![feature(unboxed_closures)]
#![feature(chunks_exact)]
#![feature(rchunks)]
#![feature(repeat_generic_slice)]

extern crate alloc_system;
Expand Down
116 changes: 114 additions & 2 deletions src/liballoc/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,54 @@ fn test_chunks_exactator_0() {
let _it = v.chunks_exact(0);
}

#[test]
fn test_rchunksator() {
let v = &[1, 2, 3, 4, 5];

assert_eq!(v.rchunks(2).len(), 3);

let chunks: &[&[_]] = &[&[4, 5], &[2, 3], &[1]];
assert_eq!(v.rchunks(2).collect::<Vec<_>>(), chunks);
let chunks: &[&[_]] = &[&[3, 4, 5], &[1, 2]];
assert_eq!(v.rchunks(3).collect::<Vec<_>>(), chunks);
let chunks: &[&[_]] = &[&[1, 2, 3, 4, 5]];
assert_eq!(v.rchunks(6).collect::<Vec<_>>(), chunks);

let chunks: &[&[_]] = &[&[1], &[2, 3], &[4, 5]];
assert_eq!(v.rchunks(2).rev().collect::<Vec<_>>(), chunks);
}

#[test]
#[should_panic]
fn test_rchunksator_0() {
let v = &[1, 2, 3, 4];
let _it = v.rchunks(0);
}

#[test]
fn test_rchunks_exactator() {
let v = &[1, 2, 3, 4, 5];

assert_eq!(v.rchunks_exact(2).len(), 2);

let chunks: &[&[_]] = &[&[4, 5], &[2, 3]];
assert_eq!(v.rchunks_exact(2).collect::<Vec<_>>(), chunks);
let chunks: &[&[_]] = &[&[3, 4, 5]];
assert_eq!(v.rchunks_exact(3).collect::<Vec<_>>(), chunks);
let chunks: &[&[_]] = &[];
assert_eq!(v.rchunks_exact(6).collect::<Vec<_>>(), chunks);

let chunks: &[&[_]] = &[&[2, 3], &[4, 5]];
assert_eq!(v.rchunks_exact(2).rev().collect::<Vec<_>>(), chunks);
}

#[test]
#[should_panic]
fn test_rchunks_exactator_0() {
let v = &[1, 2, 3, 4];
let _it = v.rchunks_exact(0);
}

#[test]
fn test_reverse_part() {
let mut values = [1, 2, 3, 4, 5];
Expand Down Expand Up @@ -1205,7 +1253,7 @@ fn test_get_mut() {
#[test]
fn test_mut_chunks() {
let mut v = [0, 1, 2, 3, 4, 5, 6];
assert_eq!(v.chunks_mut(2).len(), 4);
assert_eq!(v.chunks_mut(3).len(), 3);
for (i, chunk) in v.chunks_mut(3).enumerate() {
for x in chunk {
*x = i as u8;
Expand Down Expand Up @@ -1237,7 +1285,7 @@ fn test_mut_chunks_0() {
#[test]
fn test_mut_chunks_exact() {
let mut v = [0, 1, 2, 3, 4, 5, 6];
assert_eq!(v.chunks_exact_mut(2).len(), 3);
assert_eq!(v.chunks_exact_mut(3).len(), 2);
for (i, chunk) in v.chunks_exact_mut(3).enumerate() {
for x in chunk {
*x = i as u8;
Expand Down Expand Up @@ -1266,6 +1314,70 @@ fn test_mut_chunks_exact_0() {
let _it = v.chunks_exact_mut(0);
}

#[test]
fn test_mut_rchunks() {
let mut v = [0, 1, 2, 3, 4, 5, 6];
assert_eq!(v.rchunks_mut(3).len(), 3);
for (i, chunk) in v.rchunks_mut(3).enumerate() {
for x in chunk {
*x = i as u8;
}
}
let result = [2, 1, 1, 1, 0, 0, 0];
assert_eq!(v, result);
}

#[test]
fn test_mut_rchunks_rev() {
let mut v = [0, 1, 2, 3, 4, 5, 6];
for (i, chunk) in v.rchunks_mut(3).rev().enumerate() {
for x in chunk {
*x = i as u8;
}
}
let result = [0, 1, 1, 1, 2, 2, 2];
assert_eq!(v, result);
}

#[test]
#[should_panic]
fn test_mut_rchunks_0() {
let mut v = [1, 2, 3, 4];
let _it = v.rchunks_mut(0);
}

#[test]
fn test_mut_rchunks_exact() {
let mut v = [0, 1, 2, 3, 4, 5, 6];
assert_eq!(v.rchunks_exact_mut(3).len(), 2);
for (i, chunk) in v.rchunks_exact_mut(3).enumerate() {
for x in chunk {
*x = i as u8;
}
}
let result = [0, 1, 1, 1, 0, 0, 0];
assert_eq!(v, result);
}

#[test]
fn test_mut_rchunks_exact_rev() {
let mut v = [0, 1, 2, 3, 4, 5, 6];
for (i, chunk) in v.rchunks_exact_mut(3).rev().enumerate() {
for x in chunk {
*x = i as u8;
}
}
let result = [0, 0, 0, 0, 1, 1, 1];
assert_eq!(v, result);
}

#[test]
#[should_panic]
fn test_mut_rchunks_exact_0() {
let mut v = [1, 2, 3, 4];
let _it = v.rchunks_exact_mut(0);
}

#[test]
fn test_mut_last() {
let mut x = [1, 2, 3, 4, 5];
Expand Down
Loading

0 comments on commit 80a8e5c

Please sign in to comment.