Skip to content

Commit 554fbc2

Browse files
committed
Add min specialisation for RangeFrom
Calling `min` on `RangeFrom` currently causes an infinite loop. Although other methods such as `max` also result in an infinite loop, it is strictly incorrect in the case of `min`. Adding a specialisation fixes this. Separated from rust-lang#47180 because this technically changes behaviour; it’s not just an optimisation, so it’s a little different.
1 parent 73ac5d6 commit 554fbc2

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/libcore/iter/range.rs

+5
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ impl<A: Step> Iterator for ops::RangeFrom<A> {
320320
self.start = plus_n.add_one();
321321
Some(plus_n)
322322
}
323+
324+
#[inline]
325+
fn min(mut self) -> Option<A> {
326+
self.next()
327+
}
323328
}
324329

325330
#[unstable(feature = "fused", issue = "35602")]

src/libcore/tests/iter.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,12 @@ fn test_range_inclusive_min() {
13971397
assert_eq!(r.min(), None);
13981398
}
13991399

1400+
#[test]
1401+
fn test_range_from_min() {
1402+
assert_eq!((0..).min(), Some(0));
1403+
assert_eq!((-20..).min(), Some(-20));
1404+
}
1405+
14001406
#[test]
14011407
fn test_repeat() {
14021408
let mut it = repeat(42);

0 commit comments

Comments
 (0)