//! 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;
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) }
}
}
}
}
+impl<K: TotalEq> TotalEq for KeyRef<K> {}
+
impl<K, V> LruEntry<K, V> {
fn new() -> LruEntry<K, V> {
LruEntry {
}
}
-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 {
}
}
-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();
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]