From 554fbc212ac2371397d188cd3e51df8770a583f9 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 11 Jan 2018 19:35:00 +0000 Subject: [PATCH] Add `min` specialisation for `RangeFrom` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 https://github.com/rust-lang/rust/pull/47180 because this technically changes behaviour; it’s not just an optimisation, so it’s a little different. --- src/libcore/iter/range.rs | 5 +++++ src/libcore/tests/iter.rs | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/libcore/iter/range.rs b/src/libcore/iter/range.rs index 66a76a24df45a..a3a6b313f6356 100644 --- a/src/libcore/iter/range.rs +++ b/src/libcore/iter/range.rs @@ -320,6 +320,11 @@ impl Iterator for ops::RangeFrom { self.start = plus_n.add_one(); Some(plus_n) } + + #[inline] + fn min(mut self) -> Option { + self.next() + } } #[unstable(feature = "fused", issue = "35602")] diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs index 8997cf9c6bff9..13a1b66c2e008 100644 --- a/src/libcore/tests/iter.rs +++ b/src/libcore/tests/iter.rs @@ -1397,6 +1397,12 @@ fn test_range_inclusive_min() { assert_eq!(r.min(), None); } +#[test] +fn test_range_from_min() { + assert_eq!((0..).min(), Some(0)); + assert_eq!((-20..).min(), Some(-20)); +} + #[test] fn test_repeat() { let mut it = repeat(42);