Skip to content
Open
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
7 changes: 6 additions & 1 deletion library/core/src/iter/adapters/step_by.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::intrinsics;
use crate::iter::{TrustedLen, TrustedRandomAccess, from_fn};
use crate::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, from_fn};
use crate::num::NonZero;
use crate::ops::{Range, Try};

Expand Down Expand Up @@ -135,6 +135,11 @@ where
#[stable(feature = "iterator_step_by", since = "1.28.0")]
impl<I> ExactSizeIterator for StepBy<I> where I: ExactSizeIterator {}

// StepBy can be marked as a `FusedIterator` if the underlying iterator
// is fused
#[stable(feature = "fuse_step_by", since = "CURRENT_RUSTC_VERSION")]
impl<I> FusedIterator for StepBy<I> where I: FusedIterator {}

// SAFETY: This adapter is shortening. TrustedLen requires the upper bound to be calculated correctly.
// These requirements can only be satisfied when the upper bound of the inner iterator's upper
// bound is never `None`. I: TrustedRandomAccess happens to provide this guarantee while
Expand Down
23 changes: 23 additions & 0 deletions library/coretests/tests/iter/adapters/step_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,26 @@ fn test_step_by_fold_range_specialization() {
assert_eq!(r.sum::<usize>(), usize::MAX - 1);
});
}

#[test]
fn test_step_by_fused_iterator() {
struct TestFusedIter(usize);
impl Iterator for TestFusedIter {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
if self.0 > 5 {
let ret = self.0;
self.0 -= 1;
return Some(ret);
}
None
}
}
impl FusedIterator for TestFusedIter {}

let mut it = TestFusedIter(15).step_by(5);
assert_eq!(it.next(), Some(15));
assert_eq!(it.next(), Some(10));
assert_eq!(it.next(), None);
assert_eq!(it.next(), None);
}
Loading