Skip to content

Commit

Permalink
Change VecMap's iterators to use wrapper structs instead of typedefs.
Browse files Browse the repository at this point in the history
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 `VecMap` 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].
  • Loading branch information
csouth3 committed Dec 14, 2014
1 parent 444fa1b commit 81f9a31
Showing 1 changed file with 45 additions and 14 deletions.
59 changes: 45 additions & 14 deletions src/libcollections/vec_map.rs
Expand Up @@ -18,7 +18,7 @@ use core::prelude::*;
use core::default::Default;
use core::fmt;
use core::iter;
use core::iter::{Enumerate, FilterMap};
use core::iter::{Enumerate, FilterMap, Map};
use core::mem::replace;
use core::ops::FnOnce;

Expand Down Expand Up @@ -144,7 +144,7 @@ impl<V> VecMap<V> {
pub fn keys<'r>(&'r self) -> Keys<'r, V> {
fn first<A, B>((a, _): (A, B)) -> A { a }

self.iter().map(first)
Keys { iter: self.iter().map(first) }
}

/// Returns an iterator visiting all values in ascending order by the keys.
Expand All @@ -153,7 +153,7 @@ impl<V> VecMap<V> {
pub fn values<'r>(&'r self) -> Values<'r, V> {
fn second<A, B>((_, b): (A, B)) -> B { b }

self.iter().map(second)
Values { iter: self.iter().map(second) }
}

/// Returns an iterator visiting all key-value pairs in ascending order by the keys.
Expand Down Expand Up @@ -240,7 +240,7 @@ impl<V> VecMap<V> {
}

let values = replace(&mut self.v, vec!());
values.into_iter().enumerate().filter_map(filter)
MoveItems { iter: values.into_iter().enumerate().filter_map(filter) }
}

/// Return the number of elements in the map.
Expand Down Expand Up @@ -603,7 +603,7 @@ macro_rules! double_ended_iterator {
}
}

/// Forward iterator over a map.
/// An iterator over the key-value pairs of a map.
pub struct Entries<'a, V:'a> {
front: uint,
back: uint,
Expand All @@ -613,7 +613,7 @@ pub struct Entries<'a, V:'a> {
iterator!(impl Entries -> (uint, &'a V), as_ref)
double_ended_iterator!(impl Entries -> (uint, &'a V), as_ref)

/// Forward iterator over the key-value pairs of a map, with the
/// An iterator over the key-value pairs of a map, with the
/// values being mutable.
pub struct MutEntries<'a, V:'a> {
front: uint,
Expand All @@ -624,19 +624,50 @@ pub struct MutEntries<'a, V:'a> {
iterator!(impl MutEntries -> (uint, &'a mut V), as_mut)
double_ended_iterator!(impl MutEntries -> (uint, &'a mut V), as_mut)

/// Forward iterator over the keys of a map
pub type Keys<'a, V> = iter::Map<(uint, &'a V), uint, Entries<'a, V>, fn((uint, &'a V)) -> uint>;
/// An iterator over the keys of a map.
pub struct Keys<'a, V: 'a> {
iter: Map<(uint, &'a V), uint, Entries<'a, V>, fn((uint, &'a V)) -> uint>
}

/// Forward iterator over the values of a map
pub type Values<'a, V> =
iter::Map<(uint, &'a V), &'a V, Entries<'a, V>, fn((uint, &'a V)) -> &'a V>;
/// An iterator over the values of a map.
pub struct Values<'a, V: 'a> {
iter: Map<(uint, &'a V), &'a V, Entries<'a, V>, fn((uint, &'a V)) -> &'a V>
}

/// Iterator over the key-value pairs of a map, the iterator consumes the map
pub type MoveItems<V> = FilterMap<
/// A consuming iterator over the key-value pairs of a map.
pub struct MoveItems<V> {
iter: FilterMap<
(uint, Option<V>),
(uint, V),
Enumerate<vec::MoveItems<Option<V>>>,
fn((uint, Option<V>)) -> Option<(uint, V)>>;
fn((uint, Option<V>)) -> Option<(uint, V)>>
}

impl<'a, V> Iterator<uint> for Keys<'a, V> {
fn next(&mut self) -> Option<uint> { self.iter.next() }
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
}
impl<'a, V> DoubleEndedIterator<uint> for Keys<'a, V> {
fn next_back(&mut self) -> Option<uint> { self.iter.next_back() }
}


impl<'a, V> Iterator<&'a V> for Values<'a, V> {
fn next(&mut self) -> Option<(&'a V)> { self.iter.next() }
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
}
impl<'a, V> DoubleEndedIterator<&'a V> for Values<'a, V> {
fn next_back(&mut self) -> Option<(&'a V)> { self.iter.next_back() }
}


impl<V> Iterator<(uint, V)> for MoveItems<V> {
fn next(&mut self) -> Option<(uint, V)> { self.iter.next() }
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
}
impl<V> DoubleEndedIterator<(uint, V)> for MoveItems<V> {
fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() }
}

#[cfg(test)]
mod test_map {
Expand Down

10 comments on commit 81f9a31

@bors
Copy link
Contributor

@bors bors commented on 81f9a31 Dec 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from Gankro
at csouth3@81f9a31

@bors
Copy link
Contributor

@bors bors commented on 81f9a31 Dec 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging 28 batched pull requests into batch

@bors
Copy link
Contributor

@bors bors commented on 81f9a31 Dec 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status: {"merge_sha": "b76dd4c892f50f4d2fb8d48fe7a5ba8a27999c8c", "rollup_pulls": [[19720, "81f9a319265ddbe6b7823b50c29e3aff076d82c1"], [19729, "069a2037bcc4d9b998c0d8914ac35049286a8014"], [19743, "31b240d6bcb81836fca2c2aa4076daa2aea587fe"], [19753, "e92e8ac36521b2057b5239aef552136ef4d316b2"], [19764, "577f742d7a8a593e8134056259f195a7c897eeb9"], [19766, "19eb4bf0b2d59c1d1dc78fe52fb98fe61a9a2783"], [19770, "341cf405e5be56b9b1c8f3be6d0fe9a014eeff26"], [19827, "2f7a5f49029279bd734ef87fd65ae992b2ad9f40"], [19830, "f053f29ff5469ff955221d1ea8d6154ef276ce10"], [19832, "c3778fae6f57d30381476ea0110cb445e52b407a"], [19838, "5966815abea77bd4497634fcb06c7bbdecf5e08a"], [19845, "5c29df6b28a82d1c74c9d799f82daac81f0304d8"], [19846, "a333e013fc939a933f2cdc951da0ec442abeebd9"], [19849, "1fbca8824a6b6018191b7ee998d2c97f30c481d7"], [19852, "6f4a4066266436fbe88f506aad6232b752cf0cee"], [19856, "26ce387e26c4934b265f9b126089f13330e90844"], [19857, "0a968ef199c99b9c1a3420029ca8bc5af5b249ec"], [19859, "0d38cae0b9e23f1557ce96fd7a9677f1040fab54"], [19860, "f63784f4e279facc881c51451d5cd8315336585e"], [19868, "c270390f1b7d2489644f30445e35e25a1595f256"], [19869, "8d6895a9c097a90bb00459eccacb6ba06c6437f9"], [19873, "8fcc832198e86c4f3ff2912c5cfc91dc0896098b"], [19880, "4df66cd014409042f1ddbc2f2130c81e4bdc941f"], [19881, "2e74291290c23f60428917b0449aa1d0656263eb"], [19887, "c9ea7c9a58bc2bcfe77d264cedaeca5a3296634a"], [19893, "72608eba434281361f51484c28525c85a51b9aff"], [19895, "a9dbb7908dd851dff81f613452518db3ba04887c"]]}

@bors
Copy link
Contributor

@bors bors commented on 81f9a31 Dec 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing rollup candidate = b76dd4c

Successful merges:

Failed merges:

@bors
Copy link
Contributor

@bors bors commented on 81f9a31 Dec 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from Gankro, Gankro
at csouth3@81f9a31

@bors
Copy link
Contributor

@bors bors commented on 81f9a31 Dec 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging 38 batched pull requests into batch

@bors
Copy link
Contributor

@bors bors commented on 81f9a31 Dec 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status: {"merge_sha": "86603477db11071e0e9d55dc84f5bacbf5d7ba6f", "rollup_pulls": [[19720, "81f9a319265ddbe6b7823b50c29e3aff076d82c1"], [19729, "b7ba69d4ddec0b502e091982ff22d2523d44b367"], [19743, "31b240d6bcb81836fca2c2aa4076daa2aea587fe"], [19753, "e92e8ac36521b2057b5239aef552136ef4d316b2"], [19755, "7d1fa4ebea3cb29d76d04199e3a015ab11974914"], [19764, "577f742d7a8a593e8134056259f195a7c897eeb9"], [19766, "19eb4bf0b2d59c1d1dc78fe52fb98fe61a9a2783"], [19770, "341cf405e5be56b9b1c8f3be6d0fe9a014eeff26"], [19820, "8abe7846d6c32bf06fd8bb79d76e658b6770bd7b"], [19827, "2f7a5f49029279bd734ef87fd65ae992b2ad9f40"], [19830, "f053f29ff5469ff955221d1ea8d6154ef276ce10"], [19832, "c3778fae6f57d30381476ea0110cb445e52b407a"], [19838, "5966815abea77bd4497634fcb06c7bbdecf5e08a"], [19845, "5c29df6b28a82d1c74c9d799f82daac81f0304d8"], [19846, "a333e013fc939a933f2cdc951da0ec442abeebd9"], [19849, "1fbca8824a6b6018191b7ee998d2c97f30c481d7"], [19856, "26ce387e26c4934b265f9b126089f13330e90844"], [19857, "0a968ef199c99b9c1a3420029ca8bc5af5b249ec"], [19859, "0d38cae0b9e23f1557ce96fd7a9677f1040fab54"], [19860, "f63784f4e279facc881c51451d5cd8315336585e"], [19868, "c270390f1b7d2489644f30445e35e25a1595f256"], [19869, "8d6895a9c097a90bb00459eccacb6ba06c6437f9"], [19873, "8fcc832198e86c4f3ff2912c5cfc91dc0896098b"], [19880, "4df66cd014409042f1ddbc2f2130c81e4bdc941f"], [19881, "2e74291290c23f60428917b0449aa1d0656263eb"], [19885, "df5404cfa837907405427e3aa4adf1d969e208c9"], [19887, "c9ea7c9a58bc2bcfe77d264cedaeca5a3296634a"], [19893, "72608eba434281361f51484c28525c85a51b9aff"], [19895, "a9dbb7908dd851dff81f613452518db3ba04887c"], [19902, "9021f61ef7979cb146c5786e1c54c6d928cc0483"], [19905, "4ecad89636dc7d15dd593776b342b01c7bdf2f50"], [19912, "570325dd3c6b7958ccfe008fb0c68f531fd51ed1"], [19923, "8f4e9c2357a953a7aa01570de4a00f4cac34d0b0"], [19930, "73d395e6dbde3e720544a660c95f407378b5be70"], [19935, "c42e2f604ef2f43e9b8adbf1fc7a40944bfcb668"], [19940, "c1b69c7a82b1a138dd1bb1954cd308fa97d15115"]]}

@bors
Copy link
Contributor

@bors bors commented on 81f9a31 Dec 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing rollup candidate = 8660347

Successful merges:

Failed merges:

Please sign in to comment.