]> git.lizzy.rs Git - rust.git/blobdiff - src/libcollections/lru_cache.rs
use TotalEq for HashMap
[rust.git] / src / libcollections / lru_cache.rs
index ec387df7215d35efdab74ac8b27a05cf95bf53fd..e328b41cc0fff83cc06b093fa2caf7fe0e711141 100644 (file)
 //! assert!(cache.get(&2).is_none());
 //! ```
 
+use std::cast;
 use std::container::Container;
-use std::hash::{Hash, sip};
+use std::hash::Hash;
+use std::fmt;
 use std::ptr;
-use std::cast;
 
 use HashMap;
 
@@ -61,9 +62,9 @@ pub struct LruCache<K, V> {
     priv tail: *mut LruEntry<K, V>,
 }
 
-impl<K: Hash> Hash for KeyRef<K> {
-    fn hash(&self, s: &mut sip::SipState) {
-        unsafe {(*self.k).hash(s)}
+impl<S, K: Hash<S>> Hash<S> for KeyRef<K> {
+    fn hash(&self, state: &mut S) {
+        unsafe { (*self.k).hash(state) }
     }
 }
 
@@ -73,6 +74,8 @@ fn eq(&self, other: &KeyRef<K>) -> bool {
     }
 }
 
+impl<K: TotalEq> TotalEq for KeyRef<K> {}
+
 impl<K, V> LruEntry<K, V> {
     fn new() -> LruEntry<K, V> {
         LruEntry {
@@ -93,7 +96,7 @@ fn with_key_value(k: K, v: V) -> LruEntry<K, V> {
     }
 }
 
-impl<K: Hash + Eq, V> LruCache<K, V> {
+impl<K: Hash + TotalEq, V> LruCache<K, V> {
     /// Create an LRU Cache that holds at most `capacity` items.
     pub fn new(capacity: uint) -> LruCache<K, V> {
         let cache = LruCache {
@@ -217,47 +220,43 @@ fn attach(&mut self, node: *mut LruEntry<K, V>) {
     }
 }
 
-impl<A: ToStr + Hash + Eq, B: ToStr> ToStr for LruCache<A, B> {
+impl<A: fmt::Show + Hash + TotalEq, B: fmt::Show> fmt::Show for LruCache<A, B> {
     /// Return a string that lists the key-value pairs from most-recently
     /// used to least-recently used.
-    #[inline]
-    fn to_str(&self) -> ~str {
-        let mut acc = ~"{";
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        try!(write!(f.buf, r"\{"));
         let mut cur = self.head;
         for i in range(0, self.len()) {
-            if i > 0 {
-                acc.push_str(", ");
-            }
+            if i > 0 { try!(write!(f.buf, ", ")) }
             unsafe {
                 cur = (*cur).next;
                 match (*cur).key {
                     // should never print nil
-                    None => acc.push_str("nil"),
-                    Some(ref k) => acc.push_str(k.to_str())
+                    None => try!(write!(f.buf, "nil")),
+                    Some(ref k) => try!(write!(f.buf, "{}", *k)),
                 }
             }
-            acc.push_str(": ");
+            try!(write!(f.buf, ": "));
             unsafe {
                 match (*cur).value {
                     // should never print nil
-                    None => acc.push_str("nil"),
-                    Some(ref value) => acc.push_str(value.to_str())
+                    None => try!(write!(f.buf, "nil")),
+                    Some(ref value) => try!(write!(f.buf, "{}", *value)),
                 }
             }
         }
-        acc.push_char('}');
-        acc
+        write!(f.buf, r"\}")
     }
 }
 
-impl<K: Hash + Eq, V> Container for LruCache<K, V> {
+impl<K: Hash + TotalEq, V> Container for LruCache<K, V> {
     /// Return the number of key-value pairs in the cache.
     fn len(&self) -> uint {
         self.map.len()
     }
 }
 
-impl<K: Hash + Eq, V> Mutable for LruCache<K, V> {
+impl<K: Hash + TotalEq, V> Mutable for LruCache<K, V> {
     /// Clear the cache of all key-value pairs.
     fn clear(&mut self) {
         self.map.clear();
@@ -280,7 +279,7 @@ mod tests {
 
     fn assert_opt_eq<V: Eq>(opt: Option<&V>, v: V) {
         assert!(opt.is_some());
-        assert_eq!(opt.unwrap(), &v);
+        assert!(opt.unwrap() == &v);
     }
 
     #[test]