Skip to content

Commit ce4e1e4

Browse files
committed
stylo: Add appropriate traits to hashglobe::fake
1 parent 5cd296a commit ce4e1e4

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/hashglobe/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ readme = "README.md"
1111

1212
[dependencies]
1313
libc = "0.2"
14+
heapsize = "0.4"
1415

1516
[dev-dependencies]
1617
rand = "0.3"

components/hashglobe/src/fake.rs

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ use std::hash::{BuildHasher, Hash};
88
use std::collections::HashMap as StdMap;
99
use std::collections::HashSet as StdSet;
1010
use std::ops::{Deref, DerefMut};
11+
use std::fmt;
1112

12-
pub use std::collections::hash_map::{Entry, RandomState};
13+
use heapsize::HeapSizeOf;
1314

15+
pub use std::collections::hash_map::{Entry, RandomState, Iter as MapIter, IterMut as MapIterMut};
16+
pub use std::collections::hash_set::{Iter as SetIter, IntoIter as SetIntoIter};
17+
18+
#[derive(Clone)]
1419
pub struct HashMap<K, V, S = RandomState>(StdMap<K, V, S>);
1520

1621

@@ -86,6 +91,7 @@ impl<K, V, S> HashMap<K, V, S>
8691
}
8792
}
8893

94+
#[derive(Clone)]
8995
pub struct HashSet<T, S = RandomState>(StdSet<T, S>);
9096

9197

@@ -147,3 +153,133 @@ impl<T, S> HashSet<T, S>
147153
Ok(self.insert(value))
148154
}
149155
}
156+
157+
// Pass through trait impls
158+
// We can't derive these since the bounds are not obvious to the derive macro
159+
160+
161+
impl<K: HeapSizeOf + Hash + Eq, V: HeapSizeOf, S: BuildHasher> HeapSizeOf for HashMap<K, V, S> {
162+
fn heap_size_of_children(&self) -> usize {
163+
self.0.heap_size_of_children()
164+
}
165+
}
166+
167+
impl<K: Hash + Eq, V, S: BuildHasher + Default> Default for HashMap<K, V, S> {
168+
fn default() -> Self {
169+
HashMap(Default::default())
170+
}
171+
}
172+
173+
impl<K, V, S> fmt::Debug for HashMap<K, V, S>
174+
where K: Eq + Hash + fmt::Debug,
175+
V: fmt::Debug,
176+
S: BuildHasher {
177+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
178+
self.0.fmt(f)
179+
}
180+
}
181+
182+
impl<K, V, S> PartialEq for HashMap<K, V, S>
183+
where K: Eq + Hash,
184+
V: PartialEq,
185+
S: BuildHasher
186+
{
187+
fn eq(&self, other: &HashMap<K, V, S>) -> bool {
188+
self.0.eq(&other.0)
189+
}
190+
}
191+
192+
impl<K, V, S> Eq for HashMap<K, V, S>
193+
where K: Eq + Hash,
194+
V: Eq,
195+
S: BuildHasher
196+
{
197+
}
198+
199+
impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>
200+
where K: Eq + Hash,
201+
S: BuildHasher
202+
{
203+
type Item = (&'a K, &'a V);
204+
type IntoIter = MapIter<'a, K, V>;
205+
206+
fn into_iter(self) -> MapIter<'a, K, V> {
207+
self.0.iter()
208+
}
209+
}
210+
211+
impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S>
212+
where K: Eq + Hash,
213+
S: BuildHasher
214+
{
215+
type Item = (&'a K, &'a mut V);
216+
type IntoIter = MapIterMut<'a, K, V>;
217+
218+
fn into_iter(self) -> MapIterMut<'a, K, V> {
219+
self.0.iter_mut()
220+
}
221+
}
222+
223+
224+
impl<T: HeapSizeOf + Eq + Hash, S: BuildHasher> HeapSizeOf for HashSet<T, S> {
225+
fn heap_size_of_children(&self) -> usize {
226+
self.0.heap_size_of_children()
227+
}
228+
}
229+
230+
impl<T: Eq + Hash, S: BuildHasher + Default> Default for HashSet<T, S> {
231+
fn default() -> Self {
232+
HashSet(Default::default())
233+
}
234+
}
235+
236+
impl<T, S> fmt::Debug for HashSet<T, S>
237+
where T: Eq + Hash + fmt::Debug,
238+
S: BuildHasher
239+
{
240+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
241+
self.0.fmt(f)
242+
}
243+
}
244+
245+
impl<T, S> PartialEq for HashSet<T, S>
246+
where T: Eq + Hash,
247+
S: BuildHasher
248+
{
249+
fn eq(&self, other: &HashSet<T, S>) -> bool {
250+
self.0.eq(&other.0)
251+
}
252+
}
253+
254+
impl<T, S> Eq for HashSet<T, S>
255+
where T: Eq + Hash,
256+
S: BuildHasher
257+
{
258+
}
259+
260+
impl<'a, T, S> IntoIterator for &'a HashSet<T, S>
261+
where T: Eq + Hash,
262+
S: BuildHasher
263+
{
264+
type Item = &'a T;
265+
type IntoIter = SetIter<'a, T>;
266+
267+
fn into_iter(self) -> SetIter<'a, T> {
268+
self.0.iter()
269+
}
270+
}
271+
272+
impl<T, S> IntoIterator for HashSet<T, S>
273+
where T: Eq + Hash,
274+
S: BuildHasher
275+
{
276+
type Item = T;
277+
type IntoIter = SetIntoIter<T>;
278+
279+
280+
fn into_iter(self) -> SetIntoIter<T> {
281+
self.0.into_iter()
282+
}
283+
}
284+
285+

components/hashglobe/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub use std::*;
22

3+
extern crate heapsize;
4+
35
mod table;
46
mod shim;
57
mod alloc;

0 commit comments

Comments
 (0)