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

Add iterator method specialisations to Range* #47180

Merged
merged 9 commits into from
Jan 11, 2018
27 changes: 27 additions & 0 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ impl<A: Step> Iterator for ops::Range<A> {
self.start = self.end.clone();
None
}

#[inline]
fn max(self) -> Option<A> {
if self.start != self.end {
Some(self.end.sub_one())
} else { None }
}
}

// These macros generate `ExactSizeIterator` impls for various range types.
Expand Down Expand Up @@ -305,6 +312,11 @@ impl<A: Step> Iterator for ops::RangeFrom<A> {
self.start = plus_n.add_one();
Some(plus_n)
}

#[inline]
fn min(self) -> Option<A> {
Some(self.start)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently min for RangeFrom will either panic with overflow or loop forever so this is a change of behaviour. As RangeFrom is fundamentally broken right now (#25708) I don't think it's a good idea to specialise methods for it just yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was considering not adding this yet. I'll remove it, as it would be better placed in a separate change.

}
}

#[unstable(feature = "fused", issue = "35602")]
Expand Down Expand Up @@ -367,6 +379,21 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
self.end.replace_zero();
None
}

#[inline]
fn last(self) -> Option<A> {
Some(self.end)
}

#[inline]
fn min(self) -> Option<A> {
Some(self.start)
}

#[inline]
fn max(self) -> Option<A> {
Some(self.end)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this and last return None once the iterator is exhausted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it definitely should.

}
}

#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
Expand Down
32 changes: 32 additions & 0 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,38 @@ fn test_range_step() {
assert_eq!((isize::MIN..isize::MAX).step_by(1).size_hint(), (usize::MAX, Some(usize::MAX)));
}

#[test]
fn test_range_max() {
assert_eq!((0..100).max(), Some(99));
assert_eq!((-20..-10).max(), Some(-11));
assert_eq!((1..1).max(), None);
}

#[test]
fn test_range_from_min() {
assert_eq!((0..).min(), Some(0));
assert_eq!((-20..).min(), Some(-20));
assert_eq!((20..).min(), Some(20));
}

#[test]
fn test_range_inc_last_max() {
assert_eq!((0..=20).last(), Some(20));
assert_eq!((-20..=0).last(), Some(0));
assert_eq!((5..=5).last(), Some(5));

assert_eq!((0..=20).max(), Some(20));
assert_eq!((-20..=0).max(), Some(0));
assert_eq!((5..=5).max(), Some(5));
}

#[test]
fn test_range_inc_min() {
assert_eq!((0..=20).min(), Some(0));
assert_eq!((-20..=0).min(), Some(-20));
assert_eq!((5..=5).min(), Some(5));
}

#[test]
fn test_repeat() {
let mut it = repeat(42);
Expand Down