Skip to content
This repository was archived by the owner on May 26, 2026. It is now read-only.
Closed
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
26 changes: 26 additions & 0 deletions src/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::private::Entry;

/// Read-only iterator to access all occupied slots.
#[derive(Debug)]
pub struct Iter<'a, IT> {
inner: core::slice::Iter<'a, Entry<IT>>,
}
Expand All @@ -26,7 +27,24 @@ impl<'a, IT> Iterator for Iter<'a, IT> {
}
}

impl<IT> Clone for Iter<'_, IT> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}

impl<IT> Default for Iter<'_, IT> {
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}

/// Read-write iterator to access all occupied slots.
#[derive(Debug)]
pub struct IterMut<'a, IT> {
inner: core::slice::IterMut<'a, Entry<IT>>,
}
Expand All @@ -52,6 +70,14 @@ impl<'a, IT> Iterator for IterMut<'a, IT> {
}
}

impl<IT> Default for IterMut<'_, IT> {
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}

#[cfg(test)]
mod iter_test {
use crate::{slots::Slots, unrestricted::UnrestrictedSlots};
Expand Down
1 change: 1 addition & 0 deletions src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! that are conceptually private but must be technically public.

#[doc(hidden)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Entry<IT> {
Used(IT),
EmptyNext(usize),
Expand Down
53 changes: 52 additions & 1 deletion src/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,51 @@ pub struct Key<IT, const N: usize> {
_item_marker: PhantomData<IT>,
}

impl<IT, const N: usize> Clone for Key<IT, N> {
fn clone(&self) -> Self {
*self
}
}

impl<IT, const N: usize> Copy for Key<IT, N> {}

impl<IT, const N: usize> PartialEq for Key<IT, N> {
fn eq(&self, other: &Self) -> bool {
#[cfg(feature = "runtime_checks")]
let owner_id_partial_eq = self.owner_id == other.owner_id;
#[cfg(not(feature = "runtime_checks"))]
let owner_id_partial_eq = true;
owner_id_partial_eq && self.index == other.index
}
}

impl<IT, const N: usize> Eq for Key<IT, N> {}

impl<IT, const N: usize> PartialOrd for Key<IT, N> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<IT, const N: usize> Ord for Key<IT, N> {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
#[cfg(feature = "runtime_checks")]
let owner_id_cmp = self.owner_id.cmp(&other.owner_id);
#[cfg(not(feature = "runtime_checks"))]
let owner_id_cmp = core::cmp::Ordering::Equal;
owner_id_cmp.then(self.index.cmp(&other.index))
}
}

impl<IT, const N: usize> core::hash::Hash for Key<IT, N> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
#[cfg(feature = "runtime_checks")]
self.owner_id.hash(state);
self.index.hash(state);
self._item_marker.hash(state);
}
}

impl<IT, const N: usize> Key<IT, N> {
fn new(owner: &Slots<IT, N>, idx: usize) -> Self {
Self {
Expand All @@ -153,13 +198,19 @@ impl<IT, const N: usize> Key<IT, N> {
/// - `N` is the number of slots.
///
/// For more information, see the [module level documentation](./index.html)
#[derive(Default)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct Slots<IT, const N: usize> {
#[cfg(feature = "runtime_checks")]
id: usize,
inner: UnrestrictedSlots<IT, N>,
}

impl<IT, const N: usize> Default for Slots<IT, N> {
fn default() -> Self {
Self::new()
}
}

#[cfg(feature = "runtime_checks")]
fn new_instance_id() -> usize {
use core::sync::atomic::{AtomicUsize, Ordering};
Expand Down
1 change: 1 addition & 0 deletions src/unrestricted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use crate::private::Entry;
/// - `N` is the number of slots.
///
/// For more information, see the [module level documentation](crate::unrestricted)
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct UnrestrictedSlots<IT, const N: usize> {
items: [Entry<IT>; N],
next_free: usize,
Expand Down