diff --git a/examples/debug.rs b/examples/debug.rs new file mode 100644 index 0000000..3d3018f --- /dev/null +++ b/examples/debug.rs @@ -0,0 +1,17 @@ +use slotmap::SlotMap; + +slotmap::new_key_type! { struct CustomKey; } + +fn main() { + let mut x: SlotMap = SlotMap::with_key(); + let k0 = x.insert("a"); + x.insert("b"); + x.insert("c"); + dbg!(&x); + + x.remove(k0); + + x.insert("later"); + + dbg!(x); +} diff --git a/src/basic.rs b/src/basic.rs index 0666ed6..8f133e7 100644 --- a/src/basic.rs +++ b/src/basic.rs @@ -12,6 +12,7 @@ use core::marker::PhantomData; #[allow(unused_imports)] // MaybeUninit is only used on nightly at the moment. use core::mem::{ManuallyDrop, MaybeUninit}; use core::ops::{Index, IndexMut}; +use std::fmt::Debug; use crate::{DefaultKey, Key, KeyData}; @@ -109,7 +110,7 @@ impl fmt::Debug for Slot { /// Slot map, storage with stable unique keys. /// /// See [crate documentation](crate) for more details. -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct SlotMap { slots: Vec>, free_head: u32, @@ -117,6 +118,16 @@ pub struct SlotMap { _k: PhantomData K>, } +impl Debug for SlotMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut f = f.debug_map(); + for (k, v) in self.iter() { + f.entry(&k, v); + } + f.finish() + } +} + impl SlotMap { /// Constructs a new, empty [`SlotMap`]. /// diff --git a/src/dense.rs b/src/dense.rs index 926e3bc..cce454d 100644 --- a/src/dense.rs +++ b/src/dense.rs @@ -9,6 +9,7 @@ #[cfg(all(nightly, any(doc, feature = "unstable")))] use alloc::collections::TryReserveError; use alloc::vec::Vec; +use core::fmt::{self, Debug}; use core::iter::FusedIterator; #[allow(unused_imports)] // MaybeUninit is only used on nightly at the moment. use core::mem::MaybeUninit; @@ -30,7 +31,7 @@ struct Slot { /// Dense slot map, storage with stable unique keys. /// /// See [crate documentation](crate) for more details. -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct DenseSlotMap { keys: Vec, values: Vec, @@ -38,6 +39,16 @@ pub struct DenseSlotMap { free_head: u32, } +impl Debug for DenseSlotMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut f = f.debug_map(); + for (k, v) in self.iter() { + f.entry(&k, v); + } + f.finish() + } +} + impl DenseSlotMap { /// Construct a new, empty [`DenseSlotMap`]. /// diff --git a/src/hop.rs b/src/hop.rs index 57e7857..6b3baa9 100644 --- a/src/hop.rs +++ b/src/hop.rs @@ -17,7 +17,7 @@ #[cfg(all(nightly, any(doc, feature = "unstable")))] use alloc::collections::TryReserveError; use alloc::vec::Vec; -use core::fmt; +use core::fmt::{self, Debug}; use core::iter::FusedIterator; use core::marker::PhantomData; use core::mem::ManuallyDrop; @@ -129,13 +129,23 @@ impl fmt::Debug for Slot { /// Hop slot map, storage with stable unique keys. /// /// See [crate documentation](crate) for more details. -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct HopSlotMap { slots: Vec>, num_elems: u32, _k: PhantomData K>, } +impl Debug for HopSlotMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut f = f.debug_map(); + for (k, v) in self.iter() { + f.entry(&k, v); + } + f.finish() + } +} + impl HopSlotMap { /// Constructs a new, empty [`HopSlotMap`]. /// diff --git a/src/secondary.rs b/src/secondary.rs index f7dc63d..4d9537c 100644 --- a/src/secondary.rs +++ b/src/secondary.rs @@ -4,6 +4,7 @@ use super::{is_older_version, Key, KeyData}; #[cfg(all(nightly, any(doc, feature = "unstable")))] use alloc::collections::TryReserveError; use alloc::vec::Vec; +use core::fmt::{self, Debug}; use core::hint::unreachable_unchecked; use core::iter::{Enumerate, Extend, FromIterator, FusedIterator}; use core::marker::PhantomData; @@ -117,13 +118,23 @@ impl Slot { /// health[bob] -= ammo[alice] * 3; /// ammo[alice] = 0; /// ``` -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct SecondaryMap { slots: Vec>, num_elems: usize, _k: PhantomData K>, } +impl Debug for SecondaryMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut f = f.debug_map(); + for (k, v) in self.iter() { + f.entry(&k, v); + } + f.finish() + } +} + impl SecondaryMap { /// Constructs a new, empty [`SecondaryMap`]. /// @@ -1284,7 +1295,7 @@ impl<'a, K: Key, V> VacantEntry<'a, K, V> { // Despite the slot being considered Vacant for this entry, it might be occupied // with an outdated element. match replace(slot, Slot::new_occupied(self.kd.version.get(), value)) { - Occupied { .. } => {}, + Occupied { .. } => {} Vacant => self.map.num_elems += 1, } unsafe { slot.get_unchecked_mut() } diff --git a/src/sparse_secondary.rs b/src/sparse_secondary.rs index 1e93a22..d2fac47 100644 --- a/src/sparse_secondary.rs +++ b/src/sparse_secondary.rs @@ -6,6 +6,7 @@ use alloc::collections::TryReserveError; #[allow(unused_imports)] // MaybeUninit is only used on nightly at the moment. use core::mem::MaybeUninit; use std::collections::hash_map::{self, HashMap}; +use std::fmt::{self, Debug}; use std::hash; use std::iter::{Extend, FromIterator, FusedIterator}; use std::marker::PhantomData; @@ -65,12 +66,22 @@ struct Slot { /// ammo[alice] = 0; /// ``` -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct SparseSecondaryMap { slots: HashMap, S>, _k: PhantomData K>, } +impl Debug for SparseSecondaryMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut f = f.debug_map(); + for (k, v) in self.iter() { + f.entry(&k, v); + } + f.finish() + } +} + impl SparseSecondaryMap { /// Constructs a new, empty [`SparseSecondaryMap`]. ///