Implement advance_by, advance_back_by for iter::Chain by timvermeulen · Pull Request #77594 · rust-lang/rust (original) (raw)
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code here all looks right to me, but I've become paranoid about early-exits and fusing in &mut self
iterator methods.
Could you please add some tests for the edge cases of non-fused iterators? You're self.a = None;
ing in the right places, AFAICT, but I don't think the tests right now would catch it if you weren't (since slice::Iter
is always fused anyway).
If you need a non-fused iterator, you could use something like
pub fn make_silly_iterator(mut x: u8) -> impl Iterator<Item = u8> { std::iter::from_fn(move || { let i = x; x = x.wrapping_sub(1); if i == 0 { None } else { Some(i) } }) }