Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove upstream collection traits. #8

Merged
merged 1 commit into from
Nov 1, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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