use std::hash::{BuildHasher, Hash};
use crate::registry;
use crate::IntoInner;
pub struct TrackedSccHashSet<T, S = crate::CapHasher>
where
T: Eq + Hash + 'static,
S: BuildHasher,
{
inner: scc::HashSet<T, S>,
#[allow(dead_code)]
name: &'static str,
file: &'static str,
line: u32,
column: u32,
}
impl<T: Eq + Hash + 'static, S: BuildHasher + Default> TrackedSccHashSet<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: scc::HashSet::with_capacity_and_hasher(cap, S::default()),
name,
file,
line,
column,
}
}
}
impl<T: Eq + Hash + 'static, S: BuildHasher> TrackedSccHashSet<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: scc::HashSet::with_capacity_and_hasher(cap, hasher),
name,
file,
line,
column,
}
}
#[inline]
pub fn wrap_from(
inner: scc::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: Eq + Hash + 'static, S: BuildHasher> std::ops::Deref for TrackedSccHashSet<T, S> {
type Target = scc::HashSet<T, S>;
fn deref(&self) -> &scc::HashSet<T, S> {
&self.inner
}
}
impl<T: Eq + Hash + 'static, S: BuildHasher> std::ops::DerefMut for TrackedSccHashSet<T, S> {
fn deref_mut(&mut self) -> &mut scc::HashSet<T, S> {
&mut self.inner
}
}
impl<T: Eq + Hash + 'static, S: BuildHasher> Drop for TrackedSccHashSet<T, S> {
fn drop(&mut self) {
#[allow(clippy::disallowed_methods)]
let peak = self.inner.len();
registry::record_sample(self.file, self.line, self.column, peak);
}
}
impl<T: Eq + Hash + 'static, S: BuildHasher> From<TrackedSccHashSet<T, S>> for scc::HashSet<T, S> {
fn from(tracked: TrackedSccHashSet<T, S>) -> scc::HashSet<T, S> {
#[allow(clippy::disallowed_methods)]
let peak = tracked.inner.len();
registry::record_sample(tracked.file, tracked.line, tracked.column, peak);
let inner = unsafe { std::ptr::read(&tracked.inner) };
std::mem::forget(tracked);
inner
}
}
impl<T: Eq + Hash + 'static, S: BuildHasher> IntoInner for TrackedSccHashSet<T, S> {
type Inner = scc::HashSet<T, S>;
#[inline]
fn into_inner(self) -> scc::HashSet<T, S> {
scc::HashSet::from(self)
}
}