Skip to content

Commit c9a894c

Browse files
committed
rollup merge of rust-lang#19720: csouth3/vecmap-newtypes
As mentioned in rust-lang#19663, it is more desirable long-term that iterators be implemented as wrapper structs instead of typedefs. This pull request converts `VecMap` to the preferred solution.
2 parents 0dda9e2 + 69d113f commit c9a894c

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

src/libcollections/vec_map.rs

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core::prelude::*;
1818
use core::default::Default;
1919
use core::fmt;
2020
use core::iter;
21-
use core::iter::{Enumerate, FilterMap};
21+
use core::iter::{Enumerate, FilterMap, Map};
2222
use core::mem::replace;
2323

2424
use hash::{Hash, Writer};
@@ -141,14 +141,14 @@ impl<V> VecMap<V> {
141141
/// The iterator's element type is `uint`.
142142
#[unstable = "matches collection reform specification, waiting for dust to settle"]
143143
pub fn keys<'r>(&'r self) -> Keys<'r, V> {
144-
self.iter().map(|(k, _v)| k)
144+
Keys { iter: self.iter().map(|(k, _v)| k) }
145145
}
146146

147147
/// Returns an iterator visiting all values in ascending order by the keys.
148148
/// The iterator's element type is `&'r V`.
149149
#[unstable = "matches collection reform specification, waiting for dust to settle"]
150150
pub fn values<'r>(&'r self) -> Values<'r, V> {
151-
self.iter().map(|(_k, v)| v)
151+
Values { iter: self.iter().map(|(_k, v)| v) }
152152
}
153153

154154
/// Returns an iterator visiting all key-value pairs in ascending order by the keys.
@@ -231,9 +231,11 @@ impl<V> VecMap<V> {
231231
#[unstable = "matches collection reform specification, waiting for dust to settle"]
232232
pub fn into_iter(&mut self) -> MoveItems<V> {
233233
let values = replace(&mut self.v, vec!());
234-
values.into_iter().enumerate().filter_map(|(i, v)| {
235-
v.map(|v| (i, v))
236-
})
234+
MoveItems {
235+
iter: values.into_iter().enumerate().filter_map(|(i, v)| {
236+
v.map(|v| (i, v))
237+
})
238+
}
237239
}
238240

239241
/// Return the number of elements in the map.
@@ -598,7 +600,7 @@ macro_rules! double_ended_iterator {
598600
}
599601
}
600602

601-
/// Forward iterator over a map.
603+
/// An iterator over the key-value pairs of a map.
602604
pub struct Entries<'a, V:'a> {
603605
front: uint,
604606
back: uint,
@@ -608,7 +610,7 @@ pub struct Entries<'a, V:'a> {
608610
iterator!(impl Entries -> (uint, &'a V), as_ref)
609611
double_ended_iterator!(impl Entries -> (uint, &'a V), as_ref)
610612

611-
/// Forward iterator over the key-value pairs of a map, with the
613+
/// An iterator over the key-value pairs of a map, with the
612614
/// values being mutable.
613615
pub struct MutEntries<'a, V:'a> {
614616
front: uint,
@@ -619,17 +621,47 @@ pub struct MutEntries<'a, V:'a> {
619621
iterator!(impl MutEntries -> (uint, &'a mut V), as_mut)
620622
double_ended_iterator!(impl MutEntries -> (uint, &'a mut V), as_mut)
621623

622-
/// Forward iterator over the keys of a map
623-
pub type Keys<'a, V> =
624-
iter::Map<'static, (uint, &'a V), uint, Entries<'a, V>>;
624+
/// An iterator over the keys of a map.
625+
pub struct Keys<'a, V: 'a> {
626+
iter: Map<'static, (uint, &'a V), uint, Entries<'a, V>>
627+
}
628+
629+
impl<'a, V> Iterator<uint> for Keys<'a, V> {
630+
fn next(&mut self) -> Option<uint> { self.iter.next() }
631+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
632+
}
633+
634+
impl<'a, V> DoubleEndedIterator<uint> for Keys<'a, V> {
635+
fn next_back(&mut self) -> Option<uint> { self.iter.next_back() }
636+
}
637+
638+
/// An iterator over the values of a map.
639+
pub struct Values<'a, V: 'a> {
640+
iter: Map<'static, (uint, &'a V), &'a V, Entries<'a, V>>
641+
}
642+
643+
impl<'a, V> Iterator<&'a V> for Values<'a, V> {
644+
fn next(&mut self) -> Option<(&'a V)> { self.iter.next() }
645+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
646+
}
647+
648+
impl<'a, V> DoubleEndedIterator<&'a V> for Values<'a, V> {
649+
fn next_back(&mut self) -> Option<(&'a V)> { self.iter.next_back() }
650+
}
651+
652+
/// A consuming iterator over the key-value pairs of a map.
653+
pub struct MoveItems<V> {
654+
iter: FilterMap<'static, (uint, Option<V>), (uint, V), Enumerate<vec::MoveItems<Option<V>>>>
655+
}
625656

626-
/// Forward iterator over the values of a map
627-
pub type Values<'a, V> =
628-
iter::Map<'static, (uint, &'a V), &'a V, Entries<'a, V>>;
657+
impl<V> Iterator<(uint, V)> for MoveItems<V> {
658+
fn next(&mut self) -> Option<(uint, V)> { self.iter.next() }
659+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
660+
}
629661

630-
/// Iterator over the key-value pairs of a map, the iterator consumes the map
631-
pub type MoveItems<V> =
632-
FilterMap<'static, (uint, Option<V>), (uint, V), Enumerate<vec::MoveItems<Option<V>>>>;
662+
impl<V> DoubleEndedIterator<(uint, V)> for MoveItems<V> {
663+
fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() }
664+
}
633665

634666
#[cfg(test)]
635667
mod test_map {

0 commit comments

Comments
 (0)