#![cfg(feature = "hashbrown")]
use std::mem;
use hashbrown::HashTable;
use crate::Allocative;
use crate::Key;
use crate::Visitor;
const CAPACITY_NAME: Key = Key::new("capacity");
impl<T: Allocative> Allocative for HashTable<T> {
fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>) {
use crate::impls::common::DATA_NAME;
use crate::impls::hashbrown_util;
let mut visitor = visitor.enter_self_sized::<Self>();
{
let mut visitor = visitor.enter_unique(DATA_NAME, mem::size_of::<*const T>());
{
let mut visitor = visitor.enter(
CAPACITY_NAME,
hashbrown_util::raw_table_alloc_size_for_len::<T>(self.capacity()),
);
visitor.visit_iter::<T, _>(self.iter());
visitor.exit();
}
visitor.exit();
}
visitor.exit();
}
}
#[cfg(test)]
mod tests {
use std::collections::hash_map::DefaultHasher;
use std::hash::Hash;
use std::hash::Hasher;
use hashbrown::HashTable;
use crate::golden::golden_test;
fn hash<H: Hash>(value: &H) -> u64 {
let mut hasher = DefaultHasher::new();
value.hash(&mut hasher);
hasher.finish()
}
#[test]
fn test_hash_table() {
let mut table = HashTable::with_capacity(100);
for i in 0..100 {
let mut s = i.to_string();
s.shrink_to_fit(); table.insert_unique(hash(&s), s, hash);
}
golden_test!(&table);
}
}