diff --git a/Cargo.lock b/Cargo.lock index bce891473bd..d8adfd4f5a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1246,6 +1246,7 @@ dependencies = [ name = "hashglobe" version = "0.1.0" dependencies = [ + "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/components/hashglobe/Cargo.toml b/components/hashglobe/Cargo.toml index ce9513cdcf7..e92b7fc7794 100644 --- a/components/hashglobe/Cargo.toml +++ b/components/hashglobe/Cargo.toml @@ -11,6 +11,7 @@ readme = "README.md" [dependencies] libc = "0.2" +heapsize = "0.4" [dev-dependencies] rand = "0.3" \ No newline at end of file diff --git a/components/hashglobe/src/fake.rs b/components/hashglobe/src/fake.rs index 5b635226474..baf06ab0470 100644 --- a/components/hashglobe/src/fake.rs +++ b/components/hashglobe/src/fake.rs @@ -8,9 +8,14 @@ use std::hash::{BuildHasher, Hash}; use std::collections::HashMap as StdMap; use std::collections::HashSet as StdSet; use std::ops::{Deref, DerefMut}; +use std::fmt; -pub use std::collections::hash_map::{Entry, RandomState}; +use heapsize::HeapSizeOf; +pub use std::collections::hash_map::{Entry, RandomState, Iter as MapIter, IterMut as MapIterMut}; +pub use std::collections::hash_set::{Iter as SetIter, IntoIter as SetIntoIter}; + +#[derive(Clone)] pub struct HashMap(StdMap); @@ -86,6 +91,7 @@ impl HashMap } } +#[derive(Clone)] pub struct HashSet(StdSet); @@ -147,3 +153,133 @@ impl HashSet Ok(self.insert(value)) } } + +// Pass through trait impls +// We can't derive these since the bounds are not obvious to the derive macro + + +impl HeapSizeOf for HashMap { + fn heap_size_of_children(&self) -> usize { + self.0.heap_size_of_children() + } +} + +impl Default for HashMap { + fn default() -> Self { + HashMap(Default::default()) + } +} + +impl fmt::Debug for HashMap + where K: Eq + Hash + fmt::Debug, + V: fmt::Debug, + S: BuildHasher { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } +} + +impl PartialEq for HashMap + where K: Eq + Hash, + V: PartialEq, + S: BuildHasher +{ + fn eq(&self, other: &HashMap) -> bool { + self.0.eq(&other.0) + } +} + +impl Eq for HashMap + where K: Eq + Hash, + V: Eq, + S: BuildHasher +{ +} + +impl<'a, K, V, S> IntoIterator for &'a HashMap + where K: Eq + Hash, + S: BuildHasher +{ + type Item = (&'a K, &'a V); + type IntoIter = MapIter<'a, K, V>; + + fn into_iter(self) -> MapIter<'a, K, V> { + self.0.iter() + } +} + +impl<'a, K, V, S> IntoIterator for &'a mut HashMap + where K: Eq + Hash, + S: BuildHasher +{ + type Item = (&'a K, &'a mut V); + type IntoIter = MapIterMut<'a, K, V>; + + fn into_iter(self) -> MapIterMut<'a, K, V> { + self.0.iter_mut() + } +} + + +impl HeapSizeOf for HashSet { + fn heap_size_of_children(&self) -> usize { + self.0.heap_size_of_children() + } +} + +impl Default for HashSet { + fn default() -> Self { + HashSet(Default::default()) + } +} + +impl fmt::Debug for HashSet + where T: Eq + Hash + fmt::Debug, + S: BuildHasher +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } +} + +impl PartialEq for HashSet + where T: Eq + Hash, + S: BuildHasher +{ + fn eq(&self, other: &HashSet) -> bool { + self.0.eq(&other.0) + } +} + +impl Eq for HashSet + where T: Eq + Hash, + S: BuildHasher +{ +} + +impl<'a, T, S> IntoIterator for &'a HashSet + where T: Eq + Hash, + S: BuildHasher +{ + type Item = &'a T; + type IntoIter = SetIter<'a, T>; + + fn into_iter(self) -> SetIter<'a, T> { + self.0.iter() + } +} + +impl IntoIterator for HashSet + where T: Eq + Hash, + S: BuildHasher +{ + type Item = T; + type IntoIter = SetIntoIter; + + + fn into_iter(self) -> SetIntoIter { + self.0.into_iter() + } +} + + diff --git a/components/hashglobe/src/lib.rs b/components/hashglobe/src/lib.rs index a29f5c1b001..4bd32e59e96 100644 --- a/components/hashglobe/src/lib.rs +++ b/components/hashglobe/src/lib.rs @@ -1,5 +1,7 @@ pub use std::*; +extern crate heapsize; + mod table; mod shim; mod alloc;