use std::{mem, num::NonZero, slice};
pub use proof::Proof;
pub(in crate::depot) use proof::{ChainCount, Drain, Numeric, Sealed};
pub(in crate::depot) type Live<S, K, P = ()> = Proof<(S, K), Initialized<P>>;
pub unsafe trait Project<K, P = ()> {
type Output;
unsafe fn project(self, key: K) -> Self::Output;
}
use crate::depot::{
arena::linked::{head, slots},
capacity,
index::{Initialized, Stored, bitmap, bitmap::entry::Row, set},
queue::{
indexed::keyed::{self, lanes},
links::Index,
pool::{Lane, fifo::Fifo},
ring, 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 unsafe trait Key: Copy + Eq {
type Raw: Copy + Eq;
type Live: 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()) })
}
fn decode(raw: Self::Raw) -> Option<Self>;
fn into_live(self) -> Self::Live;
fn from_live(live: Self::Live) -> Self;
}
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 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(in crate::depot) use hooks::{
Detach, EndpointHook, EndpointWord, Insert, Membership, Neighbor, Pop, Removal, Splice,
};
pub(crate) use proof::{Present, Slot};
mod arena;
pub use arena::Arena;
mod words;
pub use words::Words;
pub trait Rows<W: bitmap::Width = usize>: Words<W> + Sized {
unsafe fn source_mut<P, M>(row: &mut Row<Self, P, M, W>) -> &mut Self {
&mut row.source
}
unsafe fn map_row<T: Words<W>>(
row: Row<Self, (), (), W>,
map: impl FnOnce(Self) -> T,
) -> Row<T, (), (), W> {
unsafe { row.map(map) }
}
}
impl<W: bitmap::Width, S: Words<W>> Rows<W> for S {}
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 Exact: Iterator {
type Output;
fn remaining(&self) -> capacity::Count;
unsafe fn finish(self) -> Self::Output;
}
pub unsafe trait Input<T>: Sized {
type Item;
type Error;
type Context;
type Output;
fn count(&self) -> capacity::Count;
fn reject(self, error: capacity::Shortage) -> Self::Error;
}
pub unsafe trait Start<T>: Input<T> {
fn start(
self,
) -> (
impl Exact<Item = Self::Item, Output = Self::Output>,
Self::Context,
);
}
pub unsafe trait TryStart<T>: Input<T> {
fn try_start(self) -> Started<Self, T, impl Exact<Item = Self::Item, Output = Self::Output>>;
}
pub unsafe trait BitIndex<const N: usize>: Copy {
fn index(self) -> usize;
unsafe fn from_index(index: usize) -> Self;
}
pub unsafe trait SetSource<Tag, Raw, const N: usize> {
fn planes(&self) -> &set::State<Tag, Raw, N>;
}
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 capacity(&self) -> usize;
unsafe fn hook(&self, index: Index) -> &tagged::Hook;
}
pub unsafe trait TagTarget: TagEntry {
fn lane(&self) -> tagged::Endpoint<&Self::Store>;
}
pub unsafe trait TagFinish<E: TagEntry> {
type Storage;
fn new(entry: E) -> Self::Storage;
fn entry(storage: &Self::Storage) -> &E;
fn entry_mut(storage: &mut Self::Storage) -> &mut E;
fn into_inner(storage: Self::Storage) -> E;
}
mod tagentry;
pub use tagentry::TagEntry;
pub unsafe trait RingStore {
type Item;
type Word: Word;
fn state(&self) -> ring::State<Self::Word>;
fn capacity(&self) -> usize;
fn entries(&self) -> *const mem::MaybeUninit<Self::Item>;
}
pub unsafe trait RingEdit: crate::depot::raw::RingStore {
unsafe fn slices_mut(&mut self) -> (&mut [Self::Item], &mut [Self::Item]) {
unsafe {
let (state, entries) = self.parts_mut();
let (start, [front, back]) = state.prefix(entries.len(), state.len());
let entries = entries.as_mut_ptr().cast::<Self::Item>();
(
slice::from_raw_parts_mut(entries.add(start), front),
slice::from_raw_parts_mut(entries, back),
)
}
}
fn push<const FRONT: bool>(vacant: ring::Vacant<'_, Self>, value: Self::Item) {
unsafe {
let (state, entries) = vacant.into_source().parts_mut();
let index = state.push::<FRONT>(entries.len());
(*entries.as_mut_ptr().add(index)).write(value);
}
}
unsafe fn parts_mut(
&mut self,
) -> (
&mut ring::State<Self::Word>,
&mut [mem::MaybeUninit<Self::Item>],
);
unsafe fn front_raw(&mut self) -> ring::Front<'_, Self> {
unsafe { ring::Front::new(self) }
}
unsafe fn vacant_raw(&mut self) -> ring::Vacant<'_, Self> {
unsafe { ring::Vacant::new(self) }
}
}
mod ringcell;
pub use ringcell::RingCell;
pub(crate) use ringcell::{RingDestroy, RingRead, drop_ring};
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<O: head::Owner<L>>(self, front: head::Front<O, 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<O: head::Owner<L>>(self, head: O, input: I) {
let item = unsafe { self.push(input, head.raw()) };
head.store(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: Key;
type Fifo;
fn parts(&self) -> (Self::Key, &keyed::Link<Self::Key, P>);
fn slot(&self) -> keyed::Entry<'_, Self::Key, P> {
keyed::Entry::from_source(self)
}
fn fifo(&self) -> &Self::Fifo;
fn entry<'a>(slot: Proof<(&'a Self, Index), Initialized<P>>) -> keyed::Entry<'a, Self::Key, P>;
}
pub unsafe trait KeyBorrow<'a, P = ()> {
type Key: Key;
fn parts(self) -> (Self::Key, &'a keyed::Link<Self::Key, P>);
}
unsafe impl<'a, O: KeyOwner<P>, P> KeyBorrow<'a, P> for &'a O {
type Key = O::Key;
fn parts(self) -> (Self::Key, &'a keyed::Link<Self::Key, P>) {
O::parts(self)
}
}
unsafe impl<'a, K: Key, P> KeyBorrow<'a, P> for keyed::Entry<'a, K, P> {
type Key = K;
fn parts(self) -> (K, &'a keyed::Link<K, P>) {
(self.key(), self.link())
}
}
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: Key;
fn fifo(&self) -> &keyed::Fifo<Self::Key, P>;
}
pub unsafe trait KeyRead<P = ()>: KeySource<P> {
fn entry<'a>(live: Proof<(&'a Self, Index), Initialized<P>>) -> keyed::Entry<'a, 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 LaneSelect<const N: usize, const L: usize, P = ()>:
Sized + Project<Index, P> + AsRef<lanes::Fifo<Self::Key, N, P>>
{
type Key: Key;
type Slot: KeyOwner<P, Key = Self::Key, Fifo = lanes::Fifo<Self::Key, N, P>>
+ From<Self::Output>;
}
pub unsafe trait Compact: Copy {
const MAX: u32;
fn into_raw(self) -> u32;
unsafe fn from_raw(raw: u32) -> Self;
}
pub unsafe trait LinkedBatch<'a, T: 'a, Tag: 'a>: Sized {
type Output;
type Error;
fn nodes(&self) -> &'a Arena<T, Tag>;
fn count(&self) -> capacity::Count;
fn reject(self, error: capacity::Shortage) -> Self::Error;
fn run(self, reserve: &mut slots::Reserve<'a, T, Tag>) -> Self::Output;
}
pub unsafe trait PoolInsert<'a, const TRACK: bool = true>: Sized {
type Item: 'a;
type State: 'a;
unsafe fn into_parts(self) -> (Lane<'a, Self::Item, Self::State, TRACK>, Self::Item);
}
pub unsafe trait Reserve<'fifo, 'pool: 'fifo, const TRACK: bool = true>: Sized {
type Item: 'pool;
type Credit;
type Reserved;
unsafe fn into_parts(self) -> (&'fifo mut Fifo<'pool, Self::Item, TRACK>, Self::Credit);
unsafe fn build(
slot: slots::Vacant<Self::Item, &'fifo mut Fifo<'pool, Self::Item, TRACK>>,
credit: Self::Credit,
) -> Self::Reserved;
}
pub(crate) use arena::{EndsPop, SlotOwner};
pub(in crate::depot) use arena::{
IndexedBlock, IndexedHook, IndexedMeta, IndexedState, PoolMutable, PoolOwner, PoolPolicy,
PoolSharing, QueueBlock, QueueSlot, Retired, SlabCapacity, SlabLinks, SlabSlot, StagedMove,
StagedNode, StagedPhase, StagedState,
};
pub(in crate::depot) use hooks::{LaneInit, LaneStore};
mod entry;
mod links;
pub use entry::Entry;
pub use links::Links;
type Started<S, T, I> = Result<(I, <S as Input<T>>::Context), <S as Input<T>>::Error>;
pub trait Admit {
type Output;
fn admit(self) -> Self::Output;
}