stylo: Add appropriate traits to hashglobe::fake

This commit is contained in:
Manish Goregaokar 2017-08-31 15:55:35 -07:00
parent 5cd296a264
commit ce4e1e4194
No known key found for this signature in database
GPG key ID: 3BBF4D3E2EF79F98
4 changed files with 141 additions and 1 deletions

1
Cargo.lock generated
View file

@ -1246,6 +1246,7 @@ dependencies = [
name = "hashglobe" name = "hashglobe"
version = "0.1.0" version = "0.1.0"
dependencies = [ 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)", "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)", "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View file

@ -11,6 +11,7 @@ readme = "README.md"
[dependencies] [dependencies]
libc = "0.2" libc = "0.2"
heapsize = "0.4"
[dev-dependencies] [dev-dependencies]
rand = "0.3" rand = "0.3"

View file

@ -8,9 +8,14 @@ use std::hash::{BuildHasher, Hash};
use std::collections::HashMap as StdMap; use std::collections::HashMap as StdMap;
use std::collections::HashSet as StdSet; use std::collections::HashSet as StdSet;
use std::ops::{Deref, DerefMut}; 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<K, V, S = RandomState>(StdMap<K, V, S>); pub struct HashMap<K, V, S = RandomState>(StdMap<K, V, S>);
@ -86,6 +91,7 @@ impl<K, V, S> HashMap<K, V, S>
} }
} }
#[derive(Clone)]
pub struct HashSet<T, S = RandomState>(StdSet<T, S>); pub struct HashSet<T, S = RandomState>(StdSet<T, S>);
@ -147,3 +153,133 @@ impl<T, S> HashSet<T, S>
Ok(self.insert(value)) Ok(self.insert(value))
} }
} }
// Pass through trait impls
// We can't derive these since the bounds are not obvious to the derive macro
impl<K: HeapSizeOf + Hash + Eq, V: HeapSizeOf, S: BuildHasher> HeapSizeOf for HashMap<K, V, S> {
fn heap_size_of_children(&self) -> usize {
self.0.heap_size_of_children()
}
}
impl<K: Hash + Eq, V, S: BuildHasher + Default> Default for HashMap<K, V, S> {
fn default() -> Self {
HashMap(Default::default())
}
}
impl<K, V, S> fmt::Debug for HashMap<K, V, S>
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<K, V, S> PartialEq for HashMap<K, V, S>
where K: Eq + Hash,
V: PartialEq,
S: BuildHasher
{
fn eq(&self, other: &HashMap<K, V, S>) -> bool {
self.0.eq(&other.0)
}
}
impl<K, V, S> Eq for HashMap<K, V, S>
where K: Eq + Hash,
V: Eq,
S: BuildHasher
{
}
impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>
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<K, V, S>
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<T: HeapSizeOf + Eq + Hash, S: BuildHasher> HeapSizeOf for HashSet<T, S> {
fn heap_size_of_children(&self) -> usize {
self.0.heap_size_of_children()
}
}
impl<T: Eq + Hash, S: BuildHasher + Default> Default for HashSet<T, S> {
fn default() -> Self {
HashSet(Default::default())
}
}
impl<T, S> fmt::Debug for HashSet<T, S>
where T: Eq + Hash + fmt::Debug,
S: BuildHasher
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl<T, S> PartialEq for HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
{
fn eq(&self, other: &HashSet<T, S>) -> bool {
self.0.eq(&other.0)
}
}
impl<T, S> Eq for HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
{
}
impl<'a, T, S> IntoIterator for &'a HashSet<T, S>
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<T, S> IntoIterator for HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
{
type Item = T;
type IntoIter = SetIntoIter<T>;
fn into_iter(self) -> SetIntoIter<T> {
self.0.into_iter()
}
}

View file

@ -1,5 +1,7 @@
pub use std::*; pub use std::*;
extern crate heapsize;
mod table; mod table;
mod shim; mod shim;
mod alloc; mod alloc;