Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -126,7 +126,6 @@ impl<T: fmt::Debug> fmt::Debug for Slot<T> {
/// Slot map, storage with stable unique keys.
///
/// See [crate documentation](crate) for more details.
#[derive(Debug)]
pub struct SlotMap<K: Key, V> {
slots: Vec<Slot<V>>,
free_head: u32,
Expand Down Expand Up @@ -938,6 +937,12 @@ impl<K: Key, V> IndexMut<K> for SlotMap<K, V> {
}
}

impl<K: Key, V: fmt::Debug> fmt::Debug for SlotMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
debug_fmt_entries(self, f)
}
}

// Iterators.
/// A draining iterator for [`SlotMap`].
///
Expand Down
10 changes: 8 additions & 2 deletions src/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<K: Key, V> {
keys: Vec<K>,
values: Vec<V>,
Expand Down Expand Up @@ -834,6 +834,12 @@ impl<K: Key, V> IndexMut<K> for DenseSlotMap<K, V> {
}
}

impl<K: Key, V: fmt::Debug> fmt::Debug for DenseSlotMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
debug_fmt_entries(self, f)
}
}

// Iterators.
/// A draining iterator for [`DenseSlotMap`].
///
Expand Down
9 changes: 7 additions & 2 deletions src/hop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -144,7 +144,6 @@ impl<T: fmt::Debug> fmt::Debug for Slot<T> {
/// Hop slot map, storage with stable unique keys.
///
/// See [crate documentation](crate) for more details.
#[derive(Debug)]
pub struct HopSlotMap<K: Key, V> {
slots: Vec<Slot<V>>,
num_elems: u32,
Expand Down Expand Up @@ -1024,6 +1023,12 @@ impl<K: Key, V> IndexMut<K> for HopSlotMap<K, V> {
}
}

impl<K: Key, V: fmt::Debug> fmt::Debug for HopSlotMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
debug_fmt_entries(self, f)
}
}

// Iterators.
/// A draining iterator for [`HopSlotMap`].
///
Expand Down
11 changes: 9 additions & 2 deletions src/secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -118,7 +119,7 @@ impl<T> Slot<T> {
/// health[bob] -= ammo[alice] * 3;
/// ammo[alice] = 0;
/// ```
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct SecondaryMap<K: Key, V> {
slots: Vec<Slot<V>>,
num_elems: usize,
Expand Down Expand Up @@ -916,6 +917,12 @@ impl<'a, K: Key, V: 'a + Copy> Extend<(K, &'a V)> for SecondaryMap<K, V> {
}
}

impl<K: Key, V: fmt::Debug> fmt::Debug for SecondaryMap<K, V> {
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)]
Expand Down
16 changes: 14 additions & 2 deletions src/sparse_secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand Down Expand Up @@ -67,7 +68,7 @@ struct Slot<T> {
/// ammo[alice] = 0;
/// ```

#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct SparseSecondaryMap<K: Key, V, S: hash::BuildHasher = hash_map::RandomState> {
slots: HashMap<u32, Slot<V>, S>,
_k: PhantomData<fn(K) -> K>,
Expand Down Expand Up @@ -912,6 +913,17 @@ where
}
}

impl<K, V, S> fmt::Debug for SparseSecondaryMap<K, V, S>
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)]
Expand Down
18 changes: 16 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::fmt::Debug;
use crate::Key;
use core::fmt;
use core::hint::unreachable_unchecked;

/// Internal stable replacement for !.
Expand Down Expand Up @@ -32,7 +33,7 @@ impl<T> UnwrapUnchecked<T> for Option<T> {
}
}

impl<T, E: Debug> UnwrapUnchecked<T> for Result<T, E> {
impl<T, E: fmt::Debug> UnwrapUnchecked<T> for Result<T, E> {
unsafe fn unwrap_unchecked_(self) -> T {
if cfg!(debug_assertions) {
self.unwrap()
Expand All @@ -44,3 +45,16 @@ impl<T, E: Debug> UnwrapUnchecked<T> for Result<T, E> {
}
}
}

/// 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<I, K, V>(entries: I, f: &mut fmt::Formatter) -> fmt::Result
where
I: IntoIterator<Item = (K, V)>,
K: Key,
V: fmt::Debug
{
let entries = entries.into_iter().map(|(k, v)| (k.data(), v));
f.debug_map().entries(entries).finish()
}