#![allow(clippy::disallowed_types)]
use std::collections::HashSet;
use std::hash::{BuildHasher, Hash};
use crate::registry;
use crate::IntoInner;
pub struct TrackedHashSet<T, S = crate::CapHasher> {
inner: HashSet<T, S>,
#[allow(dead_code)]
name: &'static str,
file: &'static str,
line: u32,
column: u32,
}
impl<T: Eq + Hash, S: BuildHasher + Default> TrackedHashSet<T, S> {
pub fn with_capacity_named(
cap: usize,
name: &'static str,
file: &'static str,
line: u32,
column: u32,
) -> Self {
registry::record_creation(name, file, line, column);
Self {
inner: HashSet::with_capacity_and_hasher(cap, S::default()),
name,
file,
line,
column,
}
}
}
impl<T: Eq + Hash, S: BuildHasher> TrackedHashSet<T, S> {
pub fn with_capacity_and_hasher_named(
cap: usize,
hasher: S,
name: &'static str,
file: &'static str,
line: u32,
column: u32,
) -> Self {
registry::record_creation(name, file, line, column);
Self {
inner: HashSet::with_capacity_and_hasher(cap, hasher),
name,
file,
line,
column,
}
}
#[inline]
pub fn wrap_from(
inner: HashSet<T, S>,
name: &'static str,
file: &'static str,
line: u32,
column: u32,
) -> Self {
registry::record_creation(name, file, line, column);
Self {
inner,
name,
file,
line,
column,
}
}
}
impl<T, S> std::ops::Deref for TrackedHashSet<T, S> {
type Target = HashSet<T, S>;
fn deref(&self) -> &HashSet<T, S> {
&self.inner
}
}
impl<T, S> std::ops::DerefMut for TrackedHashSet<T, S> {
fn deref_mut(&mut self) -> &mut HashSet<T, S> {
&mut self.inner
}
}
impl<T, S> Drop for TrackedHashSet<T, S> {
fn drop(&mut self) {
registry::record_sample(self.file, self.line, self.column, self.inner.capacity());
}
}
impl<T, S> From<TrackedHashSet<T, S>> for HashSet<T, S> {
fn from(tracked: TrackedHashSet<T, S>) -> HashSet<T, S> {
registry::record_sample(
tracked.file,
tracked.line,
tracked.column,
tracked.inner.capacity(),
);
let inner = unsafe { std::ptr::read(&tracked.inner) };
std::mem::forget(tracked);
inner
}
}
impl<T, S> IntoInner for TrackedHashSet<T, S> {
type Inner = HashSet<T, S>;
#[inline]
fn into_inner(self) -> HashSet<T, S> {
HashSet::from(self)
}
}
impl<T: Eq + Hash, S: BuildHasher> IntoIterator for TrackedHashSet<T, S> {
type Item = T;
type IntoIter = std::collections::hash_set::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
registry::record_sample(self.file, self.line, self.column, self.inner.capacity());
let inner = unsafe { std::ptr::read(&self.inner) };
std::mem::forget(self);
inner.into_iter()
}
}