pub trait Pool<V> {
type Error: core::error::Error;
type Iter<'a>: Iterator<Item = (usize, &'a V)>
where
Self: 'a,
V: 'a;
type IterMut<'a>: Iterator<Item = (usize, &'a mut V)>
where
Self: 'a,
V: 'a;
fn new() -> Self;
fn with_capacity(capacity: usize) -> Result<Self, Self::Error>
where
Self: Sized;
fn vacant_key(&self) -> Result<usize, Self::Error>;
fn is_empty(&self) -> bool;
fn len(&self) -> usize;
fn get(&self, key: usize) -> Option<&V>;
fn get_mut(&mut self, key: usize) -> Option<&mut V>;
fn insert(&mut self, value: V) -> Result<usize, Self::Error>;
fn try_remove(&mut self, key: usize) -> Option<V>;
fn iter(&self) -> Self::Iter<'_>;
fn iter_mut(&mut self) -> Self::IterMut<'_>;
}
#[cfg(feature = "slab")]
#[cfg_attr(docsrs, doc(cfg(feature = "slab")))]
impl<T> Pool<T> for slab::Slab<T> {
type Error = core::convert::Infallible;
type Iter<'a>
= slab::Iter<'a, T>
where
Self: 'a;
type IterMut<'a>
= slab::IterMut<'a, T>
where
Self: 'a;
#[inline]
fn new() -> Self {
slab::Slab::new()
}
#[inline]
fn with_capacity(capacity: usize) -> Result<Self, Self::Error>
where
Self: Sized,
{
Ok(slab::Slab::with_capacity(capacity))
}
#[inline]
fn vacant_key(&self) -> Result<usize, Self::Error> {
Ok(slab::Slab::vacant_key(self))
}
#[inline]
fn is_empty(&self) -> bool {
slab::Slab::is_empty(self)
}
#[inline]
fn len(&self) -> usize {
slab::Slab::len(self)
}
#[inline]
fn get(&self, key: usize) -> Option<&T> {
slab::Slab::get(self, key)
}
#[inline]
fn get_mut(&mut self, key: usize) -> Option<&mut T> {
slab::Slab::get_mut(self, key)
}
#[inline]
fn insert(&mut self, value: T) -> Result<usize, Self::Error> {
Ok(slab::Slab::insert(self, value))
}
#[inline]
fn try_remove(&mut self, key: usize) -> Option<T> {
slab::Slab::try_remove(self, key)
}
#[inline]
fn iter(&self) -> Self::Iter<'_> {
slab::Slab::iter(self)
}
#[inline]
fn iter_mut(&mut self) -> Self::IterMut<'_> {
slab::Slab::iter_mut(self)
}
}
#[cfg(all(test, feature = "slab"))]
pub(crate) struct FixedPool<V, const N: usize>(slab::Slab<V>);
#[cfg(all(test, feature = "slab"))]
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct FixedPoolFull;
#[cfg(all(test, feature = "slab"))]
impl core::fmt::Display for FixedPoolFull {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("fixed pool capacity exhausted")
}
}
#[cfg(all(test, feature = "slab"))]
impl core::error::Error for FixedPoolFull {}
#[cfg(all(test, feature = "slab"))]
impl<V, const N: usize> Pool<V> for FixedPool<V, N> {
type Error = FixedPoolFull;
type Iter<'a>
= slab::Iter<'a, V>
where
Self: 'a;
type IterMut<'a>
= slab::IterMut<'a, V>
where
Self: 'a;
fn new() -> Self {
Self(slab::Slab::new())
}
fn with_capacity(capacity: usize) -> Result<Self, Self::Error> {
if capacity > N {
Err(FixedPoolFull)
} else {
Ok(Self(slab::Slab::new()))
}
}
fn vacant_key(&self) -> Result<usize, Self::Error> {
if slab::Slab::len(&self.0) >= N {
Err(FixedPoolFull)
} else {
Ok(slab::Slab::vacant_key(&self.0))
}
}
fn is_empty(&self) -> bool {
slab::Slab::is_empty(&self.0)
}
fn len(&self) -> usize {
slab::Slab::len(&self.0)
}
fn get(&self, key: usize) -> Option<&V> {
slab::Slab::get(&self.0, key)
}
fn get_mut(&mut self, key: usize) -> Option<&mut V> {
slab::Slab::get_mut(&mut self.0, key)
}
fn insert(&mut self, value: V) -> Result<usize, Self::Error> {
if slab::Slab::len(&self.0) >= N {
return Err(FixedPoolFull);
}
Ok(slab::Slab::insert(&mut self.0, value))
}
fn try_remove(&mut self, key: usize) -> Option<V> {
slab::Slab::try_remove(&mut self.0, key)
}
fn iter(&self) -> Self::Iter<'_> {
slab::Slab::iter(&self.0)
}
fn iter_mut(&mut self) -> Self::IterMut<'_> {
slab::Slab::iter_mut(&mut self.0)
}
}
#[cfg(test)]
mod tests;