Skip to content

Commit b7e09d8

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 2e22610 commit b7e09d8

File tree

1 file changed

+28
-6
lines changed
  • src/libcollections/btree

1 file changed

+28
-6
lines changed

src/libcollections/btree/set.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::hash::Hash;
1818
use core::borrow::BorrowFrom;
1919
use core::default::Default;
2020
use core::{iter, fmt};
21-
use core::iter::Peekable;
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> {
@@ -82,15 +85,15 @@ impl<T> BTreeSet<T> {
8285
/// Gets an iterator over the BTreeSet's contents.
8386
#[unstable = "matches collection reform specification, waiting for dust to settle"]
8487
pub fn iter<'a>(&'a self) -> Items<'a, T> {
85-
self.map.keys()
88+
Items { iter: self.map.keys() }
8689
}
8790

8891
/// Gets an iterator for moving out the BtreeSet's contents.
8992
#[unstable = "matches collection reform specification, waiting for dust to settle"]
9093
pub fn into_iter(self) -> MoveItems<T> {
9194
fn first<A, B>((a, _): (A, B)) -> A { a }
9295

93-
self.map.into_iter().map(first)
96+
MoveItems { iter: self.map.into_iter().map(first) }
9497
}
9598
}
9699

@@ -505,6 +508,25 @@ impl<T: Show> Show for BTreeSet<T> {
505508
}
506509
}
507510

511+
impl<'a, T> Iterator<&'a T> for Items<'a, T> {
512+
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
513+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
514+
}
515+
impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
516+
fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
517+
}
518+
impl<'a, T> ExactSizeIterator<&'a T> for Items<'a, T> {}
519+
520+
521+
impl<T> Iterator<T> for MoveItems<T> {
522+
fn next(&mut self) -> Option<T> { self.iter.next() }
523+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
524+
}
525+
impl<T> DoubleEndedIterator<T> for MoveItems<T> {
526+
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
527+
}
528+
impl<T> ExactSizeIterator<T> for MoveItems<T> {}
529+
508530
/// Compare `x` and `y`, but return `short` if x is None and `long` if y is None
509531
fn cmp_opt<T: Ord>(x: Option<&T>, y: Option<&T>,
510532
short: Ordering, long: Ordering) -> Ordering {

0 commit comments

Comments
 (0)