diff --git a/src/librustc/middle/typeck/coherence.rs b/src/librustc/middle/typeck/coherence.rs index 376af7e9d3d..50459cd176b 100644 --- a/src/librustc/middle/typeck/coherence.rs +++ b/src/librustc/middle/typeck/coherence.rs @@ -426,10 +426,9 @@ pub fn add_trait_impl(&self, pub fn check_implementation_coherence(&self) { let trait_impls = self.crate_context.tcx.trait_impls.borrow(); - trait_impls.get().each_key(|&trait_id| { + for &trait_id in trait_impls.get().keys() { self.check_implementation_coherence_of(trait_id); - true - }); + } } pub fn check_implementation_coherence_of(&self, trait_def_id: DefId) { diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index 6ef327ab617..5c3b18caa06 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -57,6 +57,7 @@ use cmp::{Eq, Equiv}; use default::Default; use hash::Hash; +use iter; use iter::{Iterator, FromIterator, Extendable}; use iter::{FilterMap, Chain, Repeat, Zip}; use num; @@ -525,14 +526,16 @@ pub fn find_equiv<'a, Q:Hash + Equiv>(&'a self, k: &Q) } } - /// Visit all keys - pub fn each_key(&self, blk: |k: &K| -> bool) -> bool { - self.iter().advance(|(k, _)| blk(k)) + /// An iterator visiting all keys in arbitrary order. + /// Iterator element type is &'a K. + pub fn keys<'a>(&'a self) -> HashMapKeyIterator<'a, K, V> { + self.iter().map(|(k, _v)| k) } - /// Visit all values - pub fn each_value<'a>(&'a self, blk: |v: &'a V| -> bool) -> bool { - self.iter().advance(|(_, v)| blk(v)) + /// An iterator visiting all values in arbitrary order. + /// Iterator element type is &'a V. + pub fn values<'a>(&'a self) -> HashMapValueIterator<'a, K, V> { + self.iter().map(|(_k, v)| v) } /// An iterator visiting all key-value pairs in arbitrary order. @@ -609,6 +612,14 @@ pub struct HashMapMoveIterator { priv iter: vec::MoveIterator>>, } +/// HashMap keys iterator +pub type HashMapKeyIterator<'a, K, V> = + iter::Map<'static, (&'a K, &'a V), &'a K, HashMapIterator<'a, K, V>>; + +/// HashMap values iterator +pub type HashMapValueIterator<'a, K, V> = + iter::Map<'static, (&'a K, &'a V), &'a V, HashMapIterator<'a, K, V>>; + /// HashSet iterator #[deriving(Clone)] pub struct HashSetIterator<'a, K> { @@ -1015,6 +1026,28 @@ fn test_iterate() { assert_eq!(observed, 0xFFFF_FFFF); } + #[test] + fn test_keys() { + let vec = ~[(1, 'a'), (2, 'b'), (3, 'c')]; + let map = vec.move_iter().collect::>(); + let keys = map.keys().map(|&k| k).collect::<~[int]>(); + assert_eq!(keys.len(), 3); + assert!(keys.contains(&1)); + assert!(keys.contains(&2)); + assert!(keys.contains(&3)); + } + + #[test] + fn test_values() { + let vec = ~[(1, 'a'), (2, 'b'), (3, 'c')]; + let map = vec.move_iter().collect::>(); + let values = map.values().map(|&v| v).collect::<~[char]>(); + assert_eq!(values.len(), 3); + assert!(values.contains(&'a')); + assert!(values.contains(&'b')); + assert!(values.contains(&'c')); + } + #[test] fn test_find() { let mut m = HashMap::new();