Implement slice::{Iter, IterMut}::{advance_by, advance_back_by}
Co-authored-by: The8472 <git@infinite-source.de>
This commit is contained in:
parent
e742158ef5
commit
2b89914962
1 changed files with 16 additions and 0 deletions
|
|
@ -185,6 +185,14 @@ macro_rules! iterator {
|
|||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn advance_by(&mut self, n: usize) -> Result<(), usize>{
|
||||
let advance = cmp::min(n, len!(self));
|
||||
// SAFETY: `advance` does not exceed `self.len()` by construction
|
||||
unsafe { self.post_inc_start(advance as isize) };
|
||||
if advance == n { Ok(()) } else { Err(advance) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn last(mut self) -> Option<$elem> {
|
||||
self.next_back()
|
||||
|
|
@ -371,6 +379,14 @@ macro_rules! iterator {
|
|||
Some(next_back_unchecked!(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[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
|
||||
unsafe { self.pre_dec_end(advance as isize) };
|
||||
if advance == n { Ok(()) } else { Err(advance) }
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "fused", since = "1.26.0")]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue