Skip to content

Commit 2e22610

Browse files
committed
Use wrapper structs for BTreeMap'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 keys and values iterators of `BTreeMap` 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 444fa1b commit 2e22610

File tree

1 file changed

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

1 file changed

+28
-6
lines changed

src/libcollections/btree/map.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::hash::{Writer, Hash};
2626
use core::default::Default;
2727
use core::{iter, fmt, mem};
2828
use core::fmt::Show;
29+
use core::iter::Map;
2930

3031
use ring_buf::RingBuf;
3132

@@ -107,12 +108,14 @@ pub struct MoveEntries<K, V> {
107108
}
108109

109110
/// An iterator over a BTreeMap's keys.
110-
pub type Keys<'a, K, V> =
111-
iter::Map<(&'a K, &'a V), &'a K, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>;
111+
pub struct Keys<'a, K: 'a, V: 'a> {
112+
inner: Map<(&'a K, &'a V), &'a K, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
113+
}
112114

113115
/// An iterator over a BTreeMap's values.
114-
pub type Values<'a, K, V> =
115-
iter::Map<(&'a K, &'a V), &'a V, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>;
116+
pub struct Values<'a, K: 'a, V: 'a> {
117+
inner: Map<(&'a K, &'a V), &'a V, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
118+
}
116119

117120
/// A view into a single entry in a map, which may either be vacant or occupied.
118121
pub enum Entry<'a, K:'a, V:'a> {
@@ -1061,6 +1064,25 @@ impl<K, V> DoubleEndedIterator<(K, V)> for MoveEntries<K, V> {
10611064
impl<K, V> ExactSizeIterator<(K, V)> for MoveEntries<K, V> {}
10621065

10631066

1067+
impl<'a, K, V> Iterator<&'a K> for Keys<'a, K, V> {
1068+
fn next(&mut self) -> Option<(&'a K)> { self.inner.next() }
1069+
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1070+
}
1071+
impl<'a, K, V> DoubleEndedIterator<&'a K> for Keys<'a, K, V> {
1072+
fn next_back(&mut self) -> Option<(&'a K)> { self.inner.next_back() }
1073+
}
1074+
impl<'a, K, V> ExactSizeIterator<&'a K> for Keys<'a, K, V> {}
1075+
1076+
1077+
impl<'a, K, V> Iterator<&'a V> for Values<'a, K, V> {
1078+
fn next(&mut self) -> Option<(&'a V)> { self.inner.next() }
1079+
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
1080+
}
1081+
impl<'a, K, V> DoubleEndedIterator<&'a V> for Values<'a, K, V> {
1082+
fn next_back(&mut self) -> Option<(&'a V)> { self.inner.next_back() }
1083+
}
1084+
impl<'a, K, V> ExactSizeIterator<&'a V> for Values<'a, K, V> {}
1085+
10641086

10651087
impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
10661088
/// Sets the value of the entry with the VacantEntry's key,
@@ -1211,7 +1233,7 @@ impl<K, V> BTreeMap<K, V> {
12111233
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
12121234
fn first<A, B>((a, _): (A, B)) -> A { a }
12131235

1214-
self.iter().map(first)
1236+
Keys { inner: self.iter().map(first) }
12151237
}
12161238

12171239
/// Gets an iterator over the values of the map.
@@ -1232,7 +1254,7 @@ impl<K, V> BTreeMap<K, V> {
12321254
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
12331255
fn second<A, B>((_, b): (A, B)) -> B { b }
12341256

1235-
self.iter().map(second)
1257+
Values { inner: self.iter().map(second) }
12361258
}
12371259

12381260
/// Return the number of elements in the map.

0 commit comments

Comments
 (0)