From 265dce66b6b1181f4b955032b742c702c753f6c8 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Tue, 30 May 2017 09:15:25 -0700 Subject: [PATCH] RangeFrom should have an infinite size_hint This makes the size_hint from things like `take` more precise. --- src/libcore/iter/iterator.rs | 3 ++- src/libcore/iter/range.rs | 5 +++++ src/libcore/tests/iter.rs | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs index 77cbdb98c8304..85149a0f57078 100644 --- a/src/libcore/iter/iterator.rs +++ b/src/libcore/iter/iterator.rs @@ -130,9 +130,10 @@ pub trait Iterator { /// /// ``` /// // an infinite iterator has no upper bound + /// // and the maximum possible lower bound /// let iter = 0..; /// - /// assert_eq!((0, None), iter.size_hint()); + /// assert_eq!((usize::max_value(), None), iter.size_hint()); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/iter/range.rs b/src/libcore/iter/range.rs index e02823fd81280..c0313333ea9ee 100644 --- a/src/libcore/iter/range.rs +++ b/src/libcore/iter/range.rs @@ -543,6 +543,11 @@ impl Iterator for ops::RangeFrom where mem::swap(&mut n, &mut self.start); Some(n) } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + (usize::MAX, None) + } } #[unstable(feature = "fused", issue = "35602")] diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs index 4030eaf2b2333..44d5936c63edd 100644 --- a/src/libcore/tests/iter.rs +++ b/src/libcore/tests/iter.rs @@ -764,6 +764,7 @@ fn test_iterator_size_hint() { let v2 = &[10, 11, 12]; let vi = v.iter(); + assert_eq!((0..).size_hint(), (usize::MAX, None)); assert_eq!(c.size_hint(), (usize::MAX, None)); assert_eq!(vi.clone().size_hint(), (10, Some(10)));