use std::{cell, marker, mem, num::NonZero, ops};
use crate::alloc::array;
pub use proof::Proof;
pub(in crate::depot) use proof::{ChainCount, Domain, Numeric, Sealed};
pub unsafe trait Project<K, P = ()> {
type Output;
unsafe fn project(self, key: K) -> Self::Output;
}
use crate::depot::{
arena::{
linked::{self, fifo, head},
pool::shared,
},
capacity,
index::{Initialized, Stored, bitmap, set},
queue::{
indexed::keyed,
links::{Hook, Index},
pool, tagged,
},
};
mod proof;
impl crate::alloc::raw::Empty<capacity::Count> {
pub const EMPTY: Self = unsafe { Self::assume(capacity::Count::EMPTY) };
}
pub unsafe trait Address: Copy + Eq {
fn index(self) -> usize;
}
pub(super) type KeyDomain<K> = marker::PhantomData<(K, fn(K) -> K)>;
pub unsafe trait Key: Copy + Eq {
type Raw: Copy + Eq;
const VACANT: Self::Raw;
fn index(self) -> usize;
fn into_raw(self) -> Self::Raw;
fn from_raw(raw: Stored<Self>) -> Self;
unsafe fn rebuild<K: Key<Raw = Self::Raw>>(stored: Stored<K>) -> Self {
Self::from_raw(unsafe { Stored::new(stored.into_raw()) })
}
}
pub unsafe trait Link: Copy + Eq {
type Item: Copy + Eq;
const NONE: Self;
fn item(self) -> Option<Self::Item>;
unsafe fn some(item: Self::Item) -> Self;
}
pub(in crate::depot) fn inserted<L: Link, O, const FRONT: bool>(
proof: fifo::Insertion<O, L::Item, FRONT>,
) -> (O, L, bool) {
let (owner, item, previous) = proof.into_inner();
(owner, unsafe { L::some(item) }, previous.is_none())
}
unsafe impl<Tag> Link for linked::raw::Index<Tag> {
type Item = Self;
const NONE: Self = Self::NONE;
fn item(self) -> Option<Self> {
(self != Self::NONE).then_some(self)
}
unsafe fn some(item: Self) -> Self {
item
}
}
pub unsafe trait Source<P = ()>: Sized {
type Key: Copy;
fn key(&self) -> Self::Key;
}
pub unsafe trait Word: Copy + Eq {
const ZERO: Self;
const MAX: NonZero<usize>;
fn offset(self) -> usize;
unsafe fn from_offset(offset: usize) -> Self;
}
pub unsafe trait Words<W: bitmap::Width = usize> {
type Plane: Copy;
type Size: Copy + Into<u32>;
fn shape(&self) -> bitmap::Shape<Self::Size, W>;
fn words(&self, plane: Self::Plane) -> &[bitmap::Word<W>];
fn row(entry: bitmap::raw::Row<&Self, (), (), W>, plane: Self::Plane) -> &[bitmap::Word<W>]
where
Self: Sized,
{
(*entry.source()).words(plane)
}
}
mod hooks;
pub use hooks::Hooks;
pub unsafe trait Count<I: Compact = u32>: Copy {
const MIN: u32 = 0;
const MAX: u32 = u32::MAX;
fn count(self) -> capacity::Count;
}
pub unsafe trait SetSource<Tag, Raw, const N: usize> {
type Mode;
fn planes(&self) -> &set::State<Tag, Raw, N, Self::Mode>;
}
pub unsafe trait Bitmap<W: bitmap::Width = usize>: Words<W> {}
pub unsafe trait TagStore {
type Head: tagged::raw::Head;
type Heads: AsRef<[Self::Head]> + ?Sized;
fn heads(&self) -> &Self::Heads;
fn is_node(&self, word: u32) -> bool {
(u32::MAX - word) as usize >= self.heads().as_ref().len()
}
unsafe fn hook(&self, index: Index) -> &tagged::raw::Hook;
}
pub unsafe trait HeadPop<L: Link, I = ()>: Sized {
type Output;
unsafe fn pop(self, item: L::Item, input: I) -> (L, Self::Output);
unsafe fn apply(self, front: head::Front<'_, L>, input: I) -> Self::Output {
unsafe { front.pop(self, input) }
}
}
pub unsafe trait HeadPush<L: Link, I = ()>: Sized {
unsafe fn push(self, input: I, head: L) -> L::Item;
unsafe fn apply(self, head: &head::Cell<L>, input: I) {
let item = unsafe { self.push(input, head.raw()) };
head.0.set(unsafe { L::some(item) });
}
}
pub unsafe trait KeyFinish<const LINKED: bool, P = ()>: Sized {
type Output;
unsafe fn finish(self) -> Self::Output;
}
pub unsafe trait KeyOwner<P = ()>: Sized {
type Key: Address;
type Fifo;
fn parts(&self) -> (Self::Key, &keyed::Link<Self::Key, P>);
fn fifo(&self) -> &Self::Fifo;
fn link(live: Proof<(&Self, Index), Initialized<P>>) -> &keyed::Link<Self::Key, P>;
}
pub unsafe trait KeyRoute<P = ()>: KeyOwner<P> {
type Target: KeyOwner<P, Key = Self::Key, Fifo = Self::Fifo>;
unsafe fn route(self) -> Self::Target;
}
pub unsafe trait KeySource<P = ()> {
type Key: Address;
fn fifo(&self) -> &keyed::Fifo<Self::Key, P>;
}
pub unsafe trait KeyRead<P = ()>: KeySource<P> {
fn link(live: Proof<(&Self, Index), Initialized<P>>) -> &keyed::Link<Self::Key, P>;
}
pub unsafe trait KeyPlane<P = ()>:
KeySource<P, Key = <Self::Slot as KeyOwner<P>>::Key> + Sized
{
type Slot: KeyOwner<P, Fifo = keyed::Fifo<Self::Key, P>>;
fn select(slot: Proof<(Self, Index), Initialized<P>>) -> Self::Slot;
}
pub unsafe trait KeyJoin<P = ()>: KeySource<P> {
fn target(&self) -> &keyed::Fifo<Self::Key, P>;
fn link(live: Proof<(&Self, Index), Initialized<P>>) -> &keyed::Link<Self::Key, P>;
}
pub unsafe trait Compact: Copy {
const MAX: u32;
fn into_raw(self) -> u32;
unsafe fn from_raw(raw: u32) -> Self;
}
pub unsafe trait PoolInsert<'a, const TRACK: bool = true>: Sized {
type Item: Copy + 'a;
type State: 'a;
unsafe fn into_parts(
self,
) -> (
pool::raw::Lane<'a, Self::Item, Self::State, TRACK>,
&'a Self::Item,
);
}
mod queues;
pub(in crate::depot) use queues::{LaneStore, QueueBlock, QueueSlot};
mod slabslot;
pub(in crate::depot) use slabslot::{SlabCapacity, SlabLinks, SlabSlot};
mod indexed;
pub(in crate::depot) use indexed::{IndexedBlock, IndexedHook, IndexedMeta, IndexedState};
mod arena;
pub use arena::Arena;
mod links;
pub use links::Links;
pub(in crate::depot) use links::{EndpointHook, Neighbor};
#[repr(C)]
pub struct Entry<T> {
links: Links<T>,
value: T,
pinned: marker::PhantomPinned,
}
impl<T> Entry<T> {
pub const fn new(value: T) -> Self {
Self {
links: Links::new(),
value,
pinned: marker::PhantomPinned,
}
}
pub fn value(&self) -> &T {
&self.value
}
}
const _: () = {
assert!(mem::align_of::<Entry<usize>>() == mem::align_of::<usize>());
assert!(mem::size_of::<Entry<usize>>() == 3 * mem::size_of::<usize>());
};
pub trait Admit {
type Output;
fn admit(self) -> Self::Output;
}
#[derive(Debug)]
#[repr(C)]
pub struct Node<T> {
value: T,
hook: Hook,
}
impl<T> Node<T> {
pub fn vacant(value: T, index: u32, next: u32) -> Self {
Self {
value,
hook: Hook::vacant(index, next),
}
}
pub const fn hook(&self) -> &Hook {
&self.hook
}
}
impl<T> ops::Deref for Node<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
#[derive(Clone, Copy)]
pub enum StagedPhase {
Vacant,
Queued,
Parked,
}
#[repr(C)]
pub struct StagedNode<K, Q, P> {
fields: queues::Fields<K, Q, P>,
pub hook: Hook,
pub phase: cell::Cell<StagedPhase>,
}
impl<K, Q, P> StagedNode<K, Q, P> {
pub fn init<S>(
mut block: array::raw::Block<mem::MaybeUninit<Self>, capacity::Count, S>,
) -> array::raw::Block<Self, capacity::Count, S> {
for (index, node) in block.iter_mut().enumerate() {
Self::write(node, index as u32);
}
unsafe { block.assume_init() }
}
pub fn write(storage: &mut mem::MaybeUninit<Self>, index: u32) -> &mut Self {
let node = storage.as_mut_ptr();
unsafe {
(&raw mut (*node).hook).write(Hook::vacant(index, Index::END));
(&raw mut (*node).phase).write(cell::Cell::new(StagedPhase::Vacant));
&mut *node
}
}
pub unsafe fn park(&self, value: P) -> Q {
let previous = unsafe { self.queued_ptr().read() };
unsafe { self.store_parked(value) };
previous
}
pub unsafe fn set_parked(&self, value: P)
where
Q: Copy,
{
unsafe { self.store_parked(value) };
}
unsafe fn store_parked(&self, value: P) {
unsafe { self.parked_ptr().write(value) };
self.phase.set(StagedPhase::Parked);
}
pub unsafe fn queue(&self, value: Q) -> P {
let previous = unsafe { self.parked_ptr().read() };
unsafe { self.queued_ptr().write(value) };
self.phase.set(StagedPhase::Queued);
previous
}
pub fn key_ptr(&self) -> *mut K {
self.fields.key.get().cast()
}
pub fn queued_ptr(&self) -> *mut Q {
self.fields.payload.get().cast()
}
pub fn parked_ptr(&self) -> *mut P {
self.fields.payload.get().cast()
}
pub unsafe fn drop_queued(&self) {
unsafe {
self.key_ptr().drop_in_place();
self.queued_ptr().drop_in_place();
}
}
pub unsafe fn drop_parked(&self) {
unsafe {
self.key_ptr().drop_in_place();
self.parked_ptr().drop_in_place();
}
}
}
pub unsafe trait EndsPop<L: Link> {
unsafe fn advance(self, next: L);
}
unsafe impl<T> crate::mem::raw::Edge for shared::Resident<T> {
fn count(&self) -> &crate::mem::raw::Count {
&self.references
}
}