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 all commits
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
41 changes: 10 additions & 31 deletions library/core/src/slice/iter/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,26 163,11 @@ macro_rules! iterator {
}

#[inline]
fn nth(&mut self, n: usize) -> Option<$elem> {
if n >= len!(self) {
// This iterator is now empty.
if mem::size_of::<T>() == 0 {
// We have to do it this way as `ptr` may never be 0, but `end`
// could be (due to wrapping).
self.end = self.ptr.as_ptr();
} else {
// SAFETY: end can't be 0 if T isn't ZST because ptr isn't 0 and end >= ptr
unsafe {
self.ptr = NonNull::new_unchecked(self.end as *mut T);
}
}
return None;
}
// SAFETY: We are in bounds. `post_inc_start` does the right thing even for ZSTs.
unsafe {
self.post_inc_start(n as isize);
Some(next_unchecked!(self))
}
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
let advance = cmp::min(n, len!(self));
// SAFETY: By construction, `advance` does not exceed `self.len()`.
unsafe { self.post_inc_start(advance as isize) };
if advance == n { Ok(()) } else { Err(advance) }
}

#[inline]
Expand Down Expand Up @@ -359,17 344,11 @@ macro_rules! iterator {
}

#[inline]
fn nth_back(&mut self, n: usize) -> Option<$elem> {
if n >= len!(self) {
// This iterator is now empty.
self.end = self.ptr.as_ptr();
return None;
}
// SAFETY: We are in bounds. `pre_dec_end` does the right thing even for ZSTs.
unsafe {
self.pre_dec_end(n as isize);
Some(next_back_unchecked!(self))
}
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
let advance = cmp::min(n, len!(self));
// 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