Skip to content

Commit dc9b3ff

Browse files
committed
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.
1 parent e89dcb8 commit dc9b3ff

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

src/librustc/util/enum_set.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ impl<E:CLike> Iterator<E> for EnumSetIterator<E> {
125125
Some(elem)
126126
}
127127

128-
fn size_hint(&self) -> (Option<uint>, Option<uint>) {
129-
let exact = Some(self.bits.population_count());
130-
(exact, exact)
128+
fn size_hint(&self) -> (uint, Option<uint>) {
129+
let exact = self.bits.population_count();
130+
(exact, Some(exact))
131131
}
132132
}
133133

src/libstd/iterator.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub trait Iterator<A> {
4343
/// Return a lower bound and upper bound on the remaining length of the iterator.
4444
///
4545
/// The common use case for the estimate is pre-allocating space to store the results.
46-
fn size_hint(&self) -> (Option<uint>, Option<uint>) { (None, None) }
46+
fn size_hint(&self) -> (uint, Option<uint>) { (0, None) }
4747
}
4848

4949
/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
@@ -684,16 +684,11 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<A, T, U> {
684684
}
685685

686686
#[inline]
687-
fn size_hint(&self) -> (Option<uint>, Option<uint>) {
687+
fn size_hint(&self) -> (uint, Option<uint>) {
688688
let (a_lower, a_upper) = self.a.size_hint();
689689
let (b_lower, b_upper) = self.b.size_hint();
690690

691-
let lower = match (a_lower, b_lower) {
692-
(Some(x), Some(y)) => Some(x + y),
693-
(Some(x), None) => Some(x),
694-
(None, Some(y)) => Some(y),
695-
(None, None) => None
696-
};
691+
let lower = a_lower + b_lower;
697692

698693
let upper = match (a_upper, b_upper) {
699694
(Some(x), Some(y)) => Some(x + y),
@@ -737,7 +732,7 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
737732
}
738733

739734
#[inline]
740-
fn size_hint(&self) -> (Option<uint>, Option<uint>) {
735+
fn size_hint(&self) -> (uint, Option<uint>) {
741736
self.iter.size_hint()
742737
}
743738
}
@@ -762,9 +757,9 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
762757
}
763758

764759
#[inline]
765-
fn size_hint(&self) -> (Option<uint>, Option<uint>) {
760+
fn size_hint(&self) -> (uint, Option<uint>) {
766761
let (_, upper) = self.iter.size_hint();
767-
(None, upper) // can't know a lower bound, due to the predicate
762+
(0, upper) // can't know a lower bound, due to the predicate
768763
}
769764
}
770765

@@ -787,9 +782,9 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B,
787782
}
788783

789784
#[inline]
790-
fn size_hint(&self) -> (Option<uint>, Option<uint>) {
785+
fn size_hint(&self) -> (uint, Option<uint>) {
791786
let (_, upper) = self.iter.size_hint();
792-
(None, upper) // can't know a lower bound, due to the predicate
787+
(0, upper) // can't know a lower bound, due to the predicate
793788
}
794789
}
795790

src/libstd/vec.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,14 +2024,14 @@ macro_rules! iterator {
20242024
}
20252025

20262026
#[inline]
2027-
fn size_hint(&self) -> (Option<uint>, Option<uint>) {
2027+
fn size_hint(&self) -> (uint, Option<uint>) {
20282028
let diff = if $step > 0 {
20292029
(self.end as uint) - (self.ptr as uint)
20302030
} else {
20312031
(self.ptr as uint) - (self.end as uint)
20322032
};
2033-
let exact = Some(diff / size_of::<$elem>());
2034-
(exact, exact)
2033+
let exact = diff / size_of::<$elem>();
2034+
(exact, Some(exact))
20352035
}
20362036
}
20372037
}
@@ -2132,7 +2132,7 @@ impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {
21322132
impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {
21332133
pub fn from_iterator(iterator: &mut T) -> ~[A] {
21342134
let (lower, _) = iterator.size_hint();
2135-
let mut xs = with_capacity(lower.get_or_zero());
2135+
let mut xs = with_capacity(lower);
21362136
for iterator.advance |x| {
21372137
xs.push(x);
21382138
}
@@ -2968,28 +2968,28 @@ mod tests {
29682968
use iterator::*;
29692969
let xs = [1, 2, 5, 10, 11];
29702970
let mut it = xs.iter();
2971-
assert_eq!(it.size_hint(), (Some(5), Some(5)));
2971+
assert_eq!(it.size_hint(), (5, Some(5)));
29722972
assert_eq!(it.next().unwrap(), &1);
2973-
assert_eq!(it.size_hint(), (Some(4), Some(4)));
2973+
assert_eq!(it.size_hint(), (4, Some(4)));
29742974
assert_eq!(it.next().unwrap(), &2);
2975-
assert_eq!(it.size_hint(), (Some(3), Some(3)));
2975+
assert_eq!(it.size_hint(), (3, Some(3)));
29762976
assert_eq!(it.next().unwrap(), &5);
2977-
assert_eq!(it.size_hint(), (Some(2), Some(2)));
2977+
assert_eq!(it.size_hint(), (2, Some(2)));
29782978
assert_eq!(it.next().unwrap(), &10);
2979-
assert_eq!(it.size_hint(), (Some(1), Some(1)));
2979+
assert_eq!(it.size_hint(), (1, Some(1)));
29802980
assert_eq!(it.next().unwrap(), &11);
2981-
assert_eq!(it.size_hint(), (Some(0), Some(0)));
2981+
assert_eq!(it.size_hint(), (0, Some(0)));
29822982
assert!(it.next().is_none());
29832983
}
29842984

29852985
#[test]
29862986
fn test_iter_size_hints() {
29872987
use iterator::*;
29882988
let mut xs = [1, 2, 5, 10, 11];
2989-
assert_eq!(xs.iter().size_hint(), (Some(5), Some(5)));
2990-
assert_eq!(xs.rev_iter().size_hint(), (Some(5), Some(5)));
2991-
assert_eq!(xs.mut_iter().size_hint(), (Some(5), Some(5)));
2992-
assert_eq!(xs.mut_rev_iter().size_hint(), (Some(5), Some(5)));
2989+
assert_eq!(xs.iter().size_hint(), (5, Some(5)));
2990+
assert_eq!(xs.rev_iter().size_hint(), (5, Some(5)));
2991+
assert_eq!(xs.mut_iter().size_hint(), (5, Some(5)));
2992+
assert_eq!(xs.mut_rev_iter().size_hint(), (5, Some(5)));
29932993
}
29942994

29952995
#[test]

0 commit comments

Comments
 (0)