use crate::depot::{
arena::slab::{
self, Plane, entries,
key::{self, cycle, cycle::Policy},
},
capacity,
};
use std::{marker, pin::Pin};
pub struct Pinned<
T,
Tag = (),
const MAX: u32 = { u32::MAX },
const RECYCLE: bool = false,
const PARTITIONS: usize = 1,
S = slab::Heap,
> where
cycle::Mode<RECYCLE>: Policy,
S: slab::Storage,
{
pub(in crate::depot) core: slab::raw::Core<T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS, S>,
_pin: marker::PhantomPinned,
tag: marker::PhantomData<fn() -> Tag>,
}
impl<T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
Pinned<T, Tag, MAX, RECYCLE, PARTITIONS, S>
where
cycle::Mode<RECYCLE>: Policy,
S: slab::Storage,
S::Plane<T, MAX, entries::Exclusive<RECYCLE>>:
slab::HeapPlane<T, MAX, entries::Exclusive<RECYCLE>>,
{
pub fn try_new(cap: capacity::Count) -> Result<Self, crate::alloc::Error> {
Ok(Self {
core: slab::raw::Core::try_new(cap)?,
_pin: marker::PhantomPinned,
tag: marker::PhantomData,
})
}
}
impl<T, Tag, const MAX: u32, const PARTITIONS: usize, const N: usize> Default
for Pinned<T, Tag, MAX, false, PARTITIONS, slab::Inline<N>>
{
fn default() -> Self {
Self {
core: slab::raw::Core::new(),
_pin: marker::PhantomPinned,
tag: marker::PhantomData,
}
}
}
impl<T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
Pinned<T, Tag, MAX, RECYCLE, PARTITIONS, S>
where
cycle::Mode<RECYCLE>: Policy,
S: slab::Storage,
{
pub fn capacity(&self) -> usize {
self.core.slots.capacity()
}
pub fn is_full(&self) -> bool {
self.core.is_full()
}
pub fn vacant(
self: Pin<&mut Self>,
) -> Option<slab::raw::Anchor<'_, T, Tag, MAX, RECYCLE, PARTITIONS, S>> {
let store = self.into_ref().get_ref();
Some(unsafe { slab::raw::Anchor::pinned(store.core.free()?) })
}
pub fn anchor(
self: Pin<&mut Self>,
parts: key::Parts<MAX>,
) -> Option<slab::raw::Anchored<'_, T, Tag, MAX, RECYCLE, PARTITIONS, S>> {
let store = self.into_ref().get_ref();
let entry = slab::raw::Position::key(&store.core, parts.index(), parts.generation())?;
Some(unsafe { slab::raw::Anchored::pinned(entry.take()) })
}
}