Skip to content

Commit a81c3ab

Browse files
committed
Use wrapper structs for BTreeSet's iterators.
Using a type alias for iterator implementations is fragile since this exposes the implementation to users of the iterator, and any changes could break existing code. This commit changes the iterators of `BTreeSet` to use proper new types, rather than type aliases. However, since it is fair-game to treat a type-alias as the aliased type, this is a: [breaking-change].
1 parent 765806e commit a81c3ab

File tree

1 file changed

+29
-7
lines changed
  • src/libcollections/btree

1 file changed

+29
-7
lines changed

src/libcollections/btree/set.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use btree_map::{BTreeMap, Keys, MoveEntries};
1717
use std::hash::Hash;
1818
use core::borrow::BorrowFrom;
1919
use core::default::Default;
20-
use core::{iter, fmt};
21-
use core::iter::Peekable;
20+
use core::fmt;
21+
use core::iter::{Peekable, Map};
2222
use core::fmt::Show;
2323

2424
// FIXME(conventions): implement bounded iterators
@@ -33,11 +33,14 @@ pub struct BTreeSet<T>{
3333
}
3434

3535
/// An iterator over a BTreeSet's items.
36-
pub type Items<'a, T> = Keys<'a, T, ()>;
36+
pub struct Items<'a, T: 'a> {
37+
iter: Keys<'a, T, ()>
38+
}
3739

3840
/// An owning iterator over a BTreeSet's items.
39-
pub type MoveItems<T> =
40-
iter::Map<(T, ()), T, MoveEntries<T, ()>, fn((T, ())) -> T>;
41+
pub struct MoveItems<T> {
42+
iter: Map<(T, ()), T, MoveEntries<T, ()>, fn((T, ())) -> T>
43+
}
4144

4245
/// A lazy iterator producing elements in the set difference (in-order).
4346
pub struct DifferenceItems<'a, T:'a> {
@@ -105,7 +108,7 @@ impl<T> BTreeSet<T> {
105108
/// ```
106109
#[unstable = "matches collection reform specification, waiting for dust to settle"]
107110
pub fn iter<'a>(&'a self) -> Items<'a, T> {
108-
self.map.keys()
111+
Items { iter: self.map.keys() }
109112
}
110113

111114
/// Gets an iterator for moving out the BtreeSet's contents.
@@ -124,7 +127,7 @@ impl<T> BTreeSet<T> {
124127
pub fn into_iter(self) -> MoveItems<T> {
125128
fn first<A, B>((a, _): (A, B)) -> A { a }
126129

127-
self.map.into_iter().map(first)
130+
MoveItems { iter: self.map.into_iter().map(first) }
128131
}
129132
}
130133

@@ -635,6 +638,25 @@ impl<T: Show> Show for BTreeSet<T> {
635638
}
636639
}
637640

641+
impl<'a, T> Iterator<&'a T> for Items<'a, T> {
642+
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
643+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
644+
}
645+
impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
646+
fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
647+
}
648+
impl<'a, T> ExactSizeIterator<&'a T> for Items<'a, T> {}
649+
650+
651+
impl<T> Iterator<T> for MoveItems<T> {
652+
fn next(&mut self) -> Option<T> { self.iter.next() }
653+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
654+
}
655+
impl<T> DoubleEndedIterator<T> for MoveItems<T> {
656+
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
657+
}
658+
impl<T> ExactSizeIterator<T> for MoveItems<T> {}
659+
638660
/// Compare `x` and `y`, but return `short` if x is None and `long` if y is None
639661
fn cmp_opt<T: Ord>(x: Option<&T>, y: Option<&T>,
640662
short: Ordering, long: Ordering) -> Ordering {

0 commit comments

Comments
 (0)