use std::hash::{BuildHasher, Hash};
use dashmap::DashMap;
use crate::registry;
use crate::IntoInner;
pub struct TrackedDashMap<K, V, S = crate::CapHasher>
where
K: Eq + Hash,
S: BuildHasher + Clone,
{
inner: DashMap<K, V, S>,
#[allow(dead_code)]
name: &'static str,
file: &'static str,
line: u32,
column: u32,
}
impl<K: Eq + Hash, V, S: BuildHasher + Clone + Default> TrackedDashMap<K, V, 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: DashMap::with_capacity_and_hasher(cap, S::default()),
name,
file,
line,
column,
}
}
}
impl<K: Eq + Hash, V, S: BuildHasher + Clone> TrackedDashMap<K, V, 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: DashMap::with_capacity_and_hasher(cap, hasher),
name,
file,
line,
column,
}
}
#[inline]
pub fn wrap_from(
inner: DashMap<K, V, 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<K: Eq + Hash, V, S: BuildHasher + Clone> std::ops::Deref for TrackedDashMap<K, V, S> {
type Target = DashMap<K, V, S>;
fn deref(&self) -> &DashMap<K, V, S> {
&self.inner
}
}
impl<K: Eq + Hash, V, S: BuildHasher + Clone> std::ops::DerefMut for TrackedDashMap<K, V, S> {
fn deref_mut(&mut self) -> &mut DashMap<K, V, S> {
&mut self.inner
}
}
impl<K: Eq + Hash, V, S: BuildHasher + Clone> Drop for TrackedDashMap<K, V, 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<K: Eq + Hash, V, S: BuildHasher + Clone> From<TrackedDashMap<K, V, S>> for DashMap<K, V, S> {
fn from(tracked: TrackedDashMap<K, V, S>) -> DashMap<K, V, 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<K: Eq + Hash, V, S: BuildHasher + Clone> IntoInner for TrackedDashMap<K, V, S> {
type Inner = DashMap<K, V, S>;
#[inline]
fn into_inner(self) -> DashMap<K, V, S> {
DashMap::from(self)
}
}