use crate::{PulseKey, PulseValue, TypedPulseMap};
#[cfg(not(feature = "std"))]
use alloc::format;
impl<K: PulseKey + core::fmt::Debug, V: PulseValue + core::fmt::Debug> core::fmt::Debug
for TypedPulseMap<K, V>
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("TypedPulseMap")
.field("len", &self.len())
.field("capacity", &self.capacity())
.field(
"load_factor",
&format!("{:.1}%", self.load_factor() * 100.0),
)
.field("evictions", &self.eviction_count())
.finish()
}
}
impl<K: PulseKey, V: PulseValue> core::fmt::Display for TypedPulseMap<K, V> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"PulseMap({}/{} entries, {:.1}% load, {} evictions)",
self.len(),
self.capacity(),
self.load_factor() * 100.0,
self.eviction_count()
)
}
}
impl<K: PulseKey, V: PulseValue> Extend<(K, V)> for TypedPulseMap<K, V> {
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
for (k, v) in iter {
self.insert(k, v);
}
}
}
#[cfg(feature = "std")]
impl<K: PulseKey + std::hash::Hash + Eq, V: PulseValue> From<std::collections::HashMap<K, V>>
for TypedPulseMap<K, V>
{
fn from(map: std::collections::HashMap<K, V>) -> Self {
let num_buckets = (map.len() / 3).max(16);
let mut pulse = TypedPulseMap::new(num_buckets);
for (k, v) in map {
pulse.insert(k, v);
}
pulse
}
}