diff --git a/src/basic.rs b/src/basic.rs index 4811f57..7f20a4f 100644 --- a/src/basic.rs +++ b/src/basic.rs @@ -13,7 +13,7 @@ use core::marker::PhantomData; use core::mem::{ManuallyDrop, MaybeUninit}; use core::ops::{Index, IndexMut}; -use crate::util::{Never, UnwrapUnchecked}; +use crate::util::{Never, UnwrapUnchecked, debug_fmt_entries}; use crate::{DefaultKey, Key, KeyData}; // Storage inside a slot or metadata for the freelist when vacant. @@ -126,7 +126,6 @@ impl fmt::Debug for Slot { /// Slot map, storage with stable unique keys. /// /// See [crate documentation](crate) for more details. -#[derive(Debug)] pub struct SlotMap { slots: Vec>, free_head: u32, @@ -938,6 +937,12 @@ impl IndexMut for SlotMap { } } +impl fmt::Debug for SlotMap { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + debug_fmt_entries(self, f) + } +} + // Iterators. /// A draining iterator for [`SlotMap`]. /// diff --git a/src/dense.rs b/src/dense.rs index 42d3b93..1cf87f3 100644 --- a/src/dense.rs +++ b/src/dense.rs @@ -9,12 +9,13 @@ #[cfg(all(nightly, any(doc, feature = "unstable")))] use alloc::collections::TryReserveError; use alloc::vec::Vec; +use core::fmt; use core::iter::FusedIterator; #[allow(unused_imports)] // MaybeUninit is only used on nightly at the moment. use core::mem::MaybeUninit; use core::ops::{Index, IndexMut}; -use crate::util::{Never, UnwrapUnchecked}; +use crate::util::{Never, UnwrapUnchecked, debug_fmt_entries}; use crate::{DefaultKey, Key, KeyData}; // A slot, which represents storage for an index and a current version. @@ -31,7 +32,6 @@ struct Slot { /// Dense slot map, storage with stable unique keys. /// /// See [crate documentation](crate) for more details. -#[derive(Debug)] pub struct DenseSlotMap { keys: Vec, values: Vec, @@ -834,6 +834,12 @@ impl IndexMut for DenseSlotMap { } } +impl fmt::Debug for DenseSlotMap { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + debug_fmt_entries(self, f) + } +} + // Iterators. /// A draining iterator for [`DenseSlotMap`]. /// diff --git a/src/hop.rs b/src/hop.rs index c7a7810..8fa42bc 100644 --- a/src/hop.rs +++ b/src/hop.rs @@ -25,7 +25,7 @@ use core::mem::ManuallyDrop; use core::mem::MaybeUninit; use core::ops::{Index, IndexMut}; -use crate::util::{Never, UnwrapUnchecked}; +use crate::util::{Never, UnwrapUnchecked, debug_fmt_entries}; use crate::{DefaultKey, Key, KeyData}; // Metadata to maintain the freelist. @@ -144,7 +144,6 @@ impl fmt::Debug for Slot { /// Hop slot map, storage with stable unique keys. /// /// See [crate documentation](crate) for more details. -#[derive(Debug)] pub struct HopSlotMap { slots: Vec>, num_elems: u32, @@ -1024,6 +1023,12 @@ impl IndexMut for HopSlotMap { } } +impl fmt::Debug for HopSlotMap { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + debug_fmt_entries(self, f) + } +} + // Iterators. /// A draining iterator for [`HopSlotMap`]. /// diff --git a/src/secondary.rs b/src/secondary.rs index aa46191..f88c560 100644 --- a/src/secondary.rs +++ b/src/secondary.rs @@ -3,6 +3,7 @@ #[cfg(all(nightly, any(doc, feature = "unstable")))] use alloc::collections::TryReserveError; use alloc::vec::Vec; +use core::fmt; use core::hint::unreachable_unchecked; use core::iter::{Enumerate, Extend, FromIterator, FusedIterator}; use core::marker::PhantomData; @@ -13,7 +14,7 @@ use core::num::NonZeroU32; use core::ops::{Index, IndexMut}; use super::{Key, KeyData}; -use crate::util::is_older_version; +use crate::util::{debug_fmt_entries, is_older_version}; // This representation works because we don't have to store the versions // of removed elements. @@ -118,7 +119,7 @@ impl Slot { /// health[bob] -= ammo[alice] * 3; /// ammo[alice] = 0; /// ``` -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct SecondaryMap { slots: Vec>, num_elems: usize, @@ -916,6 +917,12 @@ impl<'a, K: Key, V: 'a + Copy> Extend<(K, &'a V)> for SecondaryMap { } } +impl fmt::Debug for SecondaryMap { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + debug_fmt_entries(self, f) + } +} + /// A view into a occupied entry in a [`SecondaryMap`]. It is part of the /// [`Entry`] enum. #[derive(Debug)] diff --git a/src/sparse_secondary.rs b/src/sparse_secondary.rs index e10a5fe..13aa620 100644 --- a/src/sparse_secondary.rs +++ b/src/sparse_secondary.rs @@ -5,13 +5,14 @@ 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; use std::hash; use std::iter::{Extend, FromIterator, FusedIterator}; use std::marker::PhantomData; use std::ops::{Index, IndexMut}; use super::{Key, KeyData}; -use crate::util::{is_older_version, UnwrapUnchecked}; +use crate::util::{debug_fmt_entries, is_older_version, UnwrapUnchecked}; #[derive(Debug, Clone)] struct Slot { @@ -67,7 +68,7 @@ struct Slot { /// ammo[alice] = 0; /// ``` -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct SparseSecondaryMap { slots: HashMap, S>, _k: PhantomData K>, @@ -912,6 +913,17 @@ where } } +impl fmt::Debug for SparseSecondaryMap +where + K: Key, + V: fmt::Debug, + S: hash::BuildHasher +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + debug_fmt_entries(self, f) + } +} + /// A view into a occupied entry in a [`SparseSecondaryMap`]. It is part of the /// [`Entry`] enum. #[derive(Debug)] diff --git a/src/util.rs b/src/util.rs index 44f7e42..db1fcce 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,5 @@ -use core::fmt::Debug; +use crate::Key; +use core::fmt; use core::hint::unreachable_unchecked; /// Internal stable replacement for !. @@ -32,7 +33,7 @@ impl UnwrapUnchecked for Option { } } -impl UnwrapUnchecked for Result { +impl UnwrapUnchecked for Result { unsafe fn unwrap_unchecked_(self) -> T { if cfg!(debug_assertions) { self.unwrap() @@ -44,3 +45,16 @@ impl UnwrapUnchecked for Result { } } } + +/// Debug format a slot map. +/// Keys are formatted without the name of the wrapper struct +/// (`1v2` instead of `MyKey(1v2)`). +pub fn debug_fmt_entries(entries: I, f: &mut fmt::Formatter) -> fmt::Result +where + I: IntoIterator, + K: Key, + V: fmt::Debug +{ + let entries = entries.into_iter().map(|(k, v)| (k.data(), v)); + f.debug_map().entries(entries).finish() +}