Skip to content

Commit

Permalink
(fix) Remove upstream collection traits.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Nov 1, 2014
1 parent 260ea05 commit 10587f0
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![license = "MIT"]
#![deny(missing_doc)]
#![deny(missing_docs)]
#![deny(warnings)]

//! A type-based key value store where one value type is allowed for each key.
Expand Down Expand Up @@ -66,7 +66,7 @@ impl TypeMap {
self.data.remove(&TypeId::of::<K>())
}

/// Gets the given key's corresponding entry in the map for in-place manipulation.
/// Get the given key's corresponding entry in the map for in-place manipulation.
pub fn entry<'a, K: Assoc<V>, V: 'static>(&'a mut self) -> Entry<'a, K, V> {
match self.data.entry(TypeId::of::<K>()) {
hashmap::Occupied(e) => Occupied(OccupiedEntry { data: e }),
Expand All @@ -79,22 +79,37 @@ impl TypeMap {

/// Get a mutable reference to the underlying HashMap
pub unsafe fn data_mut(&mut self) -> &mut HashMap<TypeId, Box<Any + 'static>> { &mut self.data }

/// Get the number of values stored in the map.
pub fn len(&self) -> uint {
self.data.len()
}

/// Return true if the map contains no values.
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}

/// Remove all entries from the map.
pub fn clear(&mut self) {
self.data.clear()
}
}

/// A view onto an entry in the map.
/// A view onto an entry in a TypeMap.
pub enum Entry<'a, K, V> {
/// A view onto an occupied entry in the map.
/// A view onto an occupied entry in a TypeMap.
Occupied(OccupiedEntry<'a, K, V>),
/// A view onto an unoccupied entry in the map.
/// A view onto an unoccupied entry in a TypeMap.
Vacant(VacantEntry<'a, K, V>)
}

/// A view onto an occupied entry in the map.
/// A view onto an occupied entry in a TypeMap.
pub struct OccupiedEntry<'a, K, V> {
data: hashmap::OccupiedEntry<'a, TypeId, Box<Any + 'static>>
}

/// A view onto an unoccupied entry in the map.
/// A view onto an unoccupied entry in a TypeMap.
pub struct VacantEntry<'a, K, V> {
data: hashmap::VacantEntry<'a, TypeId, Box<Any + 'static>>
}
Expand Down Expand Up @@ -145,18 +160,6 @@ impl<'a, K, V: 'static> VacantEntry<'a, K, V> {
}
}

impl Collection for TypeMap {
fn len(&self) -> uint {
self.data.len()
}
}

impl Mutable for TypeMap {
fn clear(&mut self) {
self.data.clear()
}
}

#[cfg(test)]
mod test {
use super::{TypeMap, Assoc, Occupied, Vacant};
Expand Down Expand Up @@ -192,14 +195,14 @@ mod test {
assert_eq!(e.get(), &Value);
assert_eq!(e.take(), Value);
},
_ => fail!("Unable to locate inserted item.")
_ => panic!("Unable to locate inserted item.")
}
assert!(!map.contains::<Key, Value>());
match map.entry::<Key, Value>() {
Vacant(e) => {
e.set(Value);
},
_ => fail!("Found non-existant entry.")
_ => panic!("Found non-existant entry.")
}
assert!(map.contains::<Key, Value>());
}
Expand Down

0 comments on commit 10587f0

Please sign in to comment.