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
17 changes: 17 additions & 0 deletions examples/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use slotmap::SlotMap;

slotmap::new_key_type! { struct CustomKey; }

fn main() {
let mut x: SlotMap<CustomKey, &'_ str> = SlotMap::with_key();
let k0 = x.insert("a");
x.insert("b");
x.insert("c");
dbg!(&x);

x.remove(k0);

x.insert("later");

dbg!(x);
}
13 changes: 12 additions & 1 deletion src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -109,14 +110,24 @@ 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, Clone)]
#[derive(Clone)]
pub struct SlotMap<K: Key, V> {
slots: Vec<Slot<V>>,
free_head: u32,
num_elems: u32,
_k: PhantomData<fn(K) -> K>,
}

impl<K: Debug + Key, V: Debug> Debug for SlotMap<K, V> {
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<V> SlotMap<DefaultKey, V> {
/// Constructs a new, empty [`SlotMap`].
///
Expand Down
13 changes: 12 additions & 1 deletion src/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,14 +31,24 @@ 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<K: Key, V> {
keys: Vec<K>,
values: Vec<V>,
slots: Vec<Slot>,
free_head: u32,
}

impl<K: Debug + Key, V: Debug> Debug for DenseSlotMap<K, V> {
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<V> DenseSlotMap<DefaultKey, V> {
/// Construct a new, empty [`DenseSlotMap`].
///
Expand Down
14 changes: 12 additions & 2 deletions src/hop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,13 +129,23 @@ 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, Clone)]
#[derive(Clone)]
pub struct HopSlotMap<K: Key, V> {
slots: Vec<Slot<V>>,
num_elems: u32,
_k: PhantomData<fn(K) -> K>,
}

impl<K: Debug + Key, V: Debug> Debug for HopSlotMap<K, V> {
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<V> HopSlotMap<DefaultKey, V> {
/// Constructs a new, empty [`HopSlotMap`].
///
Expand Down
15 changes: 13 additions & 2 deletions src/secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -117,13 +118,23 @@ 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,
_k: PhantomData<fn(K) -> K>,
}

impl<K: Debug + Key, V: Debug> Debug for SecondaryMap<K, V> {
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<K: Key, V> SecondaryMap<K, V> {
/// Constructs a new, empty [`SecondaryMap`].
///
Expand Down Expand Up @@ -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() }
Expand Down
13 changes: 12 additions & 1 deletion src/sparse_secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,12 +66,22 @@ 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>,
}

impl<K: Debug + Key, V: Debug, S: hash::BuildHasher> Debug for SparseSecondaryMap<K, V, S> {
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<K: Key, V> SparseSecondaryMap<K, V, hash_map::RandomState> {
/// Constructs a new, empty [`SparseSecondaryMap`].
///
Expand Down