From dc9b3ff1b30c10aaf60d40fd9845d9bf69ae2c2e Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Tue, 2 Jul 2013 18:40:46 -0700 Subject: [PATCH] Change signature of Iterator.size_hint Remove the Option wrapper around the lower bound. None is semantically the same as Size(0), so there's no point in having a distinction. --- src/librustc/util/enum_set.rs | 6 +++--- src/libstd/iterator.rs | 21 ++++++++------------- src/libstd/vec.rs | 28 ++++++++++++++-------------- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/librustc/util/enum_set.rs b/src/librustc/util/enum_set.rs index f9bd7a3508e..3ce645e012b 100644 --- a/src/librustc/util/enum_set.rs +++ b/src/librustc/util/enum_set.rs @@ -125,9 +125,9 @@ impl Iterator for EnumSetIterator { Some(elem) } - fn size_hint(&self) -> (Option, Option) { - let exact = Some(self.bits.population_count()); - (exact, exact) + fn size_hint(&self) -> (uint, Option) { + let exact = self.bits.population_count(); + (exact, Some(exact)) } } diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index 77befbf19aa..46d449e4dfb 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -43,7 +43,7 @@ pub trait Iterator { /// Return a lower bound and upper bound on the remaining length of the iterator. /// /// The common use case for the estimate is pre-allocating space to store the results. - fn size_hint(&self) -> (Option, Option) { (None, None) } + fn size_hint(&self) -> (uint, Option) { (0, None) } } /// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also @@ -684,16 +684,11 @@ impl, U: Iterator> Iterator for ChainIterator { } #[inline] - fn size_hint(&self) -> (Option, Option) { + fn size_hint(&self) -> (uint, Option) { let (a_lower, a_upper) = self.a.size_hint(); let (b_lower, b_upper) = self.b.size_hint(); - let lower = match (a_lower, b_lower) { - (Some(x), Some(y)) => Some(x + y), - (Some(x), None) => Some(x), - (None, Some(y)) => Some(y), - (None, None) => None - }; + let lower = a_lower + b_lower; let upper = match (a_upper, b_upper) { (Some(x), Some(y)) => Some(x + y), @@ -737,7 +732,7 @@ impl<'self, A, B, T: Iterator> Iterator for MapIterator<'self, A, B, T> { } #[inline] - fn size_hint(&self) -> (Option, Option) { + fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } } @@ -762,9 +757,9 @@ impl<'self, A, T: Iterator> Iterator for FilterIterator<'self, A, T> { } #[inline] - fn size_hint(&self) -> (Option, Option) { + fn size_hint(&self) -> (uint, Option) { let (_, upper) = self.iter.size_hint(); - (None, upper) // can't know a lower bound, due to the predicate + (0, upper) // can't know a lower bound, due to the predicate } } @@ -787,9 +782,9 @@ impl<'self, A, B, T: Iterator> Iterator for FilterMapIterator<'self, A, B, } #[inline] - fn size_hint(&self) -> (Option, Option) { + fn size_hint(&self) -> (uint, Option) { let (_, upper) = self.iter.size_hint(); - (None, upper) // can't know a lower bound, due to the predicate + (0, upper) // can't know a lower bound, due to the predicate } } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 1014ff48b1d..7244a9a7aac 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -2024,14 +2024,14 @@ macro_rules! iterator { } #[inline] - fn size_hint(&self) -> (Option, Option) { + fn size_hint(&self) -> (uint, Option) { let diff = if $step > 0 { (self.end as uint) - (self.ptr as uint) } else { (self.ptr as uint) - (self.end as uint) }; - let exact = Some(diff / size_of::<$elem>()); - (exact, exact) + let exact = diff / size_of::<$elem>(); + (exact, Some(exact)) } } } @@ -2132,7 +2132,7 @@ impl> FromIterator for ~[A] { impl> FromIterator for ~[A] { pub fn from_iterator(iterator: &mut T) -> ~[A] { let (lower, _) = iterator.size_hint(); - let mut xs = with_capacity(lower.get_or_zero()); + let mut xs = with_capacity(lower); for iterator.advance |x| { xs.push(x); } @@ -2968,17 +2968,17 @@ mod tests { use iterator::*; let xs = [1, 2, 5, 10, 11]; let mut it = xs.iter(); - assert_eq!(it.size_hint(), (Some(5), Some(5))); + assert_eq!(it.size_hint(), (5, Some(5))); assert_eq!(it.next().unwrap(), &1); - assert_eq!(it.size_hint(), (Some(4), Some(4))); + assert_eq!(it.size_hint(), (4, Some(4))); assert_eq!(it.next().unwrap(), &2); - assert_eq!(it.size_hint(), (Some(3), Some(3))); + assert_eq!(it.size_hint(), (3, Some(3))); assert_eq!(it.next().unwrap(), &5); - assert_eq!(it.size_hint(), (Some(2), Some(2))); + assert_eq!(it.size_hint(), (2, Some(2))); assert_eq!(it.next().unwrap(), &10); - assert_eq!(it.size_hint(), (Some(1), Some(1))); + assert_eq!(it.size_hint(), (1, Some(1))); assert_eq!(it.next().unwrap(), &11); - assert_eq!(it.size_hint(), (Some(0), Some(0))); + assert_eq!(it.size_hint(), (0, Some(0))); assert!(it.next().is_none()); } @@ -2986,10 +2986,10 @@ mod tests { fn test_iter_size_hints() { use iterator::*; let mut xs = [1, 2, 5, 10, 11]; - assert_eq!(xs.iter().size_hint(), (Some(5), Some(5))); - assert_eq!(xs.rev_iter().size_hint(), (Some(5), Some(5))); - assert_eq!(xs.mut_iter().size_hint(), (Some(5), Some(5))); - assert_eq!(xs.mut_rev_iter().size_hint(), (Some(5), Some(5))); + assert_eq!(xs.iter().size_hint(), (5, Some(5))); + assert_eq!(xs.rev_iter().size_hint(), (5, Some(5))); + assert_eq!(xs.mut_iter().size_hint(), (5, Some(5))); + assert_eq!(xs.mut_rev_iter().size_hint(), (5, Some(5))); } #[test]