From ec8bfdd63c18cc986cedf8b891773e844a438586 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 17 Nov 2012 08:41:47 -0800 Subject: [PATCH] Made Map.contains_key, contains_key_ref, and get pure. --- src/libcore/mutable.rs | 2 +- src/libstd/map.rs | 18 +++++++++--------- src/libstd/smallintmap.rs | 8 ++++---- .../class-impl-very-parameterized-trait.rs | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libcore/mutable.rs b/src/libcore/mutable.rs index 56a6df2c4ddf..eafdf58e67e9 100644 --- a/src/libcore/mutable.rs +++ b/src/libcore/mutable.rs @@ -48,7 +48,7 @@ impl Data { } } - fn borrow_const(op: &fn(t: &const T) -> R) -> R { + pure fn borrow_const(op: &fn(t: &const T) -> R) -> R { op(&const self.value) } diff --git a/src/libstd/map.rs b/src/libstd/map.rs index 0ee7cb6fcf96..915202143a1e 100644 --- a/src/libstd/map.rs +++ b/src/libstd/map.rs @@ -30,17 +30,17 @@ pub trait Map { fn insert(v: K, v: V) -> bool; /// Returns true if the map contains a value for the specified key - fn contains_key(key: K) -> bool; + pure fn contains_key(key: K) -> bool; /// Returns true if the map contains a value for the specified /// key, taking the key by reference. - fn contains_key_ref(key: &K) -> bool; + pure fn contains_key_ref(key: &K) -> bool; /** * Get the value for the specified key. Fails if the key does not exist in * the map. */ - fn get(key: K) -> V; + pure fn get(key: K) -> V; /** * Get the value for the specified key. If the key does not exist in @@ -200,11 +200,11 @@ pub mod chained { impl T: Map { pure fn size() -> uint { self.count } - fn contains_key(k: K) -> bool { + pure fn contains_key(k: K) -> bool { self.contains_key_ref(&k) } - fn contains_key_ref(k: &K) -> bool { + pure fn contains_key_ref(k: &K) -> bool { let hash = k.hash_keyed(0,0) as uint; match self.search_tbl(k, hash) { NotFound => false, @@ -264,7 +264,7 @@ pub mod chained { } } - fn get(k: K) -> V { + pure fn get(k: K) -> V { let opt_v = self.find(k); if opt_v.is_none() { fail fmt!("Key not found in table: %?", k); @@ -421,19 +421,19 @@ impl @Mut>: } } - fn contains_key(key: K) -> bool { + pure fn contains_key(key: K) -> bool { do self.borrow_const |p| { p.contains_key(&key) } } - fn contains_key_ref(key: &K) -> bool { + pure fn contains_key_ref(key: &K) -> bool { do self.borrow_const |p| { p.contains_key(key) } } - fn get(key: K) -> V { + pure fn get(key: K) -> V { do self.borrow_const |p| { p.get(&key) } diff --git a/src/libstd/smallintmap.rs b/src/libstd/smallintmap.rs index 1582d90ce2d6..9dc216a21557 100644 --- a/src/libstd/smallintmap.rs +++ b/src/libstd/smallintmap.rs @@ -60,7 +60,7 @@ pub pure fn get(self: SmallIntMap, key: uint) -> T { } /// Returns true if the map contains a value for the specified key -pub fn contains_key(self: SmallIntMap, key: uint) -> bool { +pub pure fn contains_key(self: SmallIntMap, key: uint) -> bool { return !find(self, key).is_none(); } @@ -93,13 +93,13 @@ impl SmallIntMap: map::Map { fn clear() { self.v.set(~[]); } - fn contains_key(key: uint) -> bool { + pure fn contains_key(key: uint) -> bool { contains_key(self, key) } - fn contains_key_ref(key: &uint) -> bool { + pure fn contains_key_ref(key: &uint) -> bool { contains_key(self, *key) } - fn get(key: uint) -> V { get(self, key) } + pure fn get(key: uint) -> V { get(self, key) } pure fn find(key: uint) -> Option { find(self, key) } fn rehash() { fail } 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 8c25d94db3be..4cfcbf67cc9a 100644 --- a/src/test/run-pass/class-impl-very-parameterized-trait.rs +++ b/src/test/run-pass/class-impl-very-parameterized-trait.rs @@ -47,10 +47,10 @@ impl cat : Map { self.meows += k; true } - fn contains_key(+k: int) -> bool { k <= self.meows } - fn contains_key_ref(k: &int) -> bool { self.contains_key(*k) } + pure fn contains_key(+k: int) -> bool { k <= self.meows } + pure fn contains_key_ref(k: &int) -> bool { self.contains_key(*k) } - fn get(+k:int) -> T { match self.find(k) { + pure fn get(+k:int) -> T { match self.find(k) { Some(v) => { v } None => { fail ~"epic fail"; } }