From 6e6b54deb143d82238dfb060dad517b86784558c Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 1 Apr 2013 18:57:52 -0400 Subject: [PATCH] add stronger region annotations to the maps --- src/libcore/container.rs | 4 ++-- src/libcore/hashmap.rs | 8 ++++---- src/libcore/trie.rs | 6 +++--- src/libstd/smallintmap.rs | 4 ++-- src/libstd/treemap.rs | 14 +++++++------- src/test/bench/core-map.rs | 6 +++--- src/test/compile-fail/map-types.rs | 2 +- .../class-impl-very-parameterized-trait.rs | 6 +++--- 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/libcore/container.rs b/src/libcore/container.rs index e20821b919b6c..b3b77c1efb7ae 100644 --- a/src/libcore/container.rs +++ b/src/libcore/container.rs @@ -25,7 +25,7 @@ pub trait Mutable: Container { fn clear(&mut self); } -pub trait Map: Mutable { +pub trait Map<'self, K, V>: Mutable { /// Return true if the map contains a value for the specified key fn contains_key(&self, key: &K) -> bool; @@ -33,7 +33,7 @@ pub trait Map: Mutable { fn each_key(&self, f: &fn(&K) -> bool); /// Visit all values - fn each_value(&self, f: &fn(&V) -> bool); + fn each_value(&self, f: &fn(&'self V) -> bool); /// Iterate over the map and mutate the contained values fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool); diff --git a/src/libcore/hashmap.rs b/src/libcore/hashmap.rs index 5dbf085f09719..7e4d32de5a9d5 100644 --- a/src/libcore/hashmap.rs +++ b/src/libcore/hashmap.rs @@ -315,7 +315,7 @@ impl Mutable for HashMap { } } -impl<'self,K:Hash + IterBytes + Eq,V> Map for HashMap { +impl<'self,K:Hash + IterBytes + Eq,V> Map<'self, K, V> for HashMap { /// Return true if the map contains a value for the specified key fn contains_key(&self, k: &K) -> bool { match self.bucket_for_key(k) { @@ -325,12 +325,12 @@ impl<'self,K:Hash + IterBytes + Eq,V> Map for HashMap { } /// Visit all keys - fn each_key(&self, blk: &fn(k: &K) -> bool) { + fn each_key(&self, blk: &fn(k: &'self K) -> bool) { self.each(|&(k, _)| blk(k)) } /// Visit all values - fn each_value(&self, blk: &fn(v: &V) -> bool) { + fn each_value(&self, blk: &fn(v: &'self V) -> bool) { self.each(|&(_, v)| blk(v)) } @@ -564,7 +564,7 @@ pub struct HashSet { impl BaseIter for HashSet { /// Visit all values in order - fn each(&self, f: &fn(&T) -> bool) { self.map.each_key(f) } + fn each(&self, f: &fn(&'self T) -> bool) { self.map.each_key(f) } fn size_hint(&self) -> Option { Some(self.len()) } } diff --git a/src/libcore/trie.rs b/src/libcore/trie.rs index 5d87e2a296d3d..e275ef0755315 100644 --- a/src/libcore/trie.rs +++ b/src/libcore/trie.rs @@ -65,7 +65,7 @@ impl Mutable for TrieMap { } } -impl Map for TrieMap { +impl<'self, T> Map<'self, uint, T> for TrieMap { /// Return true if the map contains a value for the specified key #[inline(always)] fn contains_key(&self, key: &uint) -> bool { @@ -80,7 +80,7 @@ impl Map for TrieMap { /// Visit all values in order #[inline(always)] - fn each_value(&self, f: &fn(&T) -> bool) { + fn each_value(&self, f: &fn(&'self T) -> bool) { self.each(|&(_, v)| f(v)) } @@ -156,7 +156,7 @@ pub impl TrieMap { /// Visit all values in reverse order #[inline(always)] - fn each_value_reverse(&self, f: &fn(&T) -> bool) { + fn each_value_reverse(&self, f: &fn(&'self T) -> bool) { self.each_reverse(|&(_, v)| f(v)) } } diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index b6c5ec03068cd..f84aa94659443 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -70,7 +70,7 @@ impl Mutable for SmallIntMap { fn clear(&mut self) { self.v.clear() } } -impl Map for SmallIntMap { +impl<'self, V> Map<'self, uint, V> for SmallIntMap { /// Return true if the map contains a value for the specified key fn contains_key(&self, key: &uint) -> bool { self.find(key).is_some() @@ -82,7 +82,7 @@ impl Map for SmallIntMap { } /// Visit all values in order - fn each_value(&self, blk: &fn(value: &V) -> bool) { + fn each_value(&self, blk: &fn(value: &'self V) -> bool) { self.each(|&(_, v)| blk(v)) } diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index d0868da4408d4..c4f74bb0ffd80 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -116,17 +116,17 @@ impl Mutable for TreeMap { } } -impl Map for TreeMap { +impl<'self, K: TotalOrd, V> Map<'self, K, V> for TreeMap { /// Return true if the map contains a value for the specified key fn contains_key(&self, key: &K) -> bool { self.find(key).is_some() } /// Visit all keys in order - fn each_key(&self, f: &fn(&K) -> bool) { self.each(|&(k, _)| f(k)) } + fn each_key(&self, f: &fn(&'self K) -> bool) { self.each(|&(k, _)| f(k)) } /// Visit all values in order - fn each_value(&self, f: &fn(&V) -> bool) { + fn each_value(&self, f: &fn(&'self V) -> bool) { self.each(|&(_, v)| f(v)) } @@ -181,12 +181,12 @@ pub impl TreeMap { fn new() -> TreeMap { TreeMap{root: None, length: 0} } /// Visit all keys in reverse order - fn each_key_reverse(&self, f: &fn(&K) -> bool) { + fn each_key_reverse(&self, f: &fn(&'self K) -> bool) { self.each_reverse(|&(k, _)| f(k)) } /// Visit all values in reverse order - fn each_value_reverse(&self, f: &fn(&V) -> bool) { + fn each_value_reverse(&self, f: &fn(&'self V) -> bool) { self.each_reverse(|&(_, v)| f(v)) } @@ -244,7 +244,7 @@ pub struct TreeSet { impl BaseIter for TreeSet { /// Visit all values in order #[inline(always)] - fn each(&self, f: &fn(&T) -> bool) { self.map.each_key(f) } + fn each(&self, f: &fn(&'self T) -> bool) { self.map.each_key(f) } #[inline(always)] fn size_hint(&self) -> Option { Some(self.len()) } } @@ -252,7 +252,7 @@ impl BaseIter for TreeSet { impl ReverseIter for TreeSet { /// Visit all values in reverse order #[inline(always)] - fn each_reverse(&self, f: &fn(&T) -> bool) { + fn each_reverse(&self, f: &fn(&'self T) -> bool) { self.map.each_key_reverse(f) } } diff --git a/src/test/bench/core-map.rs b/src/test/bench/core-map.rs index 8a8962fb9d637..b1f5f356d7536 100644 --- a/src/test/bench/core-map.rs +++ b/src/test/bench/core-map.rs @@ -23,7 +23,7 @@ fn timed(label: &str, f: &fn()) { io::println(fmt!(" %s: %f", label, end - start)); } -fn ascending>(map: &mut M, n_keys: uint) { +fn ascending>(map: &mut M, n_keys: uint) { io::println(" Ascending integers:"); do timed("insert") { @@ -45,7 +45,7 @@ fn ascending>(map: &mut M, n_keys: uint) { } } -fn descending>(map: &mut M, n_keys: uint) { +fn descending>(map: &mut M, n_keys: uint) { io::println(" Descending integers:"); do timed("insert") { @@ -67,7 +67,7 @@ fn descending>(map: &mut M, n_keys: uint) { } } -fn vector>(map: &mut M, n_keys: uint, dist: &[uint]) { +fn vector>(map: &mut M, n_keys: uint, dist: &[uint]) { do timed("insert") { for uint::range(0, n_keys) |i| { diff --git a/src/test/compile-fail/map-types.rs b/src/test/compile-fail/map-types.rs index ebc5b015d2752..7c8611271db77 100644 --- a/src/test/compile-fail/map-types.rs +++ b/src/test/compile-fail/map-types.rs @@ -17,5 +17,5 @@ fn main() { let x: @Map<~str, ~str> = @HashMap::new::<~str, ~str>() as @Map<~str, ~str>; let y: @Map = @x; - //~^ ERROR mismatched types: expected `@core::container::Map` + //~^ ERROR mismatched types: expected `@core::container::Map/&` } diff --git a/src/test/run-pass/class-impl-very-parameterized-trait.rs b/src/test/run-pass/class-impl-very-parameterized-trait.rs index 30ca5ea3881c9..4f39aaee4f335 100644 --- a/src/test/run-pass/class-impl-very-parameterized-trait.rs +++ b/src/test/run-pass/class-impl-very-parameterized-trait.rs @@ -70,15 +70,15 @@ impl Mutable for cat { fn clear(&mut self) {} } -impl Map for cat { +impl<'self, T> Map<'self, int, T> for cat { fn contains_key(&self, k: &int) -> bool { *k <= self.meows } fn each_key(&self, f: &fn(v: &int) -> bool) { for self.each |&(k, _)| { if !f(&k) { break; } loop;}; } - fn each_value(&self, f: &fn(v: &T) -> bool) { - for self.each |&(_, v)| { if !f(v) { break; } loop;}; + fn each_value(&self, f: &fn(v: &'self T) -> bool) { + fail!(); } fn mutate_values(&mut self, f: &fn(&int, &mut T) -> bool) {