Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement advance_by, advance_back_by for slice::{Iter, IterMut} #77633

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
timvermeulen committed Oct 6, 2020
commit 3f22d0d9281acb05dac2b74eb44244ddf3e73c47
4 changes: 2 additions & 2 deletions library/core/src/slice/iter/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 165,7 @@ macro_rules! iterator {
#[inline]
fn advance_by(&mut self, n: usize) -> Result<(), usize>{
timvermeulen marked this conversation as resolved.
Show resolved Hide resolved
let advance = cmp::min(n, len!(self));
// SAFETY: `advance` does not exceed `self.len()` by construction
// SAFETY: By construction, `advance` does not exceed `self.len()`.
unsafe { self.post_inc_start(advance as isize) };
if advance == n { Ok(()) } else { Err(advance) }
}
Expand Down Expand Up @@ -346,7 346,7 @@ macro_rules! iterator {
#[inline]
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
let advance = cmp::min(n, len!(self));
// SAFETY: `advance` does not exceed `self.len()` by construction
// SAFETY: By construction, `advance` does not exceed `self.len()`.
unsafe { self.pre_dec_end(advance as isize) };
if advance == n { Ok(()) } else { Err(advance) }
}
Expand Down
42 changes: 42 additions & 0 deletions library/core/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 122,48 @@ fn test_partition_point() {
assert_eq!(b.partition_point(|&x| x < 8), 5);
}

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

for i in 0..=v.len() {
let mut iter = v.iter();
iter.advance_by(i).unwrap();
assert_eq!(iter.as_slice(), &v[i..]);
}

let mut iter = v.iter();
assert_eq!(iter.advance_by(v.len() 1), Err(v.len()));
assert_eq!(iter.as_slice(), &[]);

let mut iter = v.iter();
iter.advance_by(3).unwrap();
assert_eq!(iter.as_slice(), &v[3..]);
iter.advance_by(2).unwrap();
assert_eq!(iter.as_slice(), &[]);
}

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

for i in 0..=v.len() {
let mut iter = v.iter();
iter.advance_back_by(i).unwrap();
assert_eq!(iter.as_slice(), &v[..v.len() - i]);
}

let mut iter = v.iter();
assert_eq!(iter.advance_back_by(v.len() 1), Err(v.len()));
assert_eq!(iter.as_slice(), &[]);

let mut iter = v.iter();
iter.advance_back_by(3).unwrap();
assert_eq!(iter.as_slice(), &v[..v.len() - 3]);
iter.advance_back_by(2).unwrap();
assert_eq!(iter.as_slice(), &[]);
}

#[test]
fn test_iterator_nth() {
let v: &[_] = &[0, 1, 2, 3, 4];
Expand Down