use std::{cell, mem};
use crate::depot::{
self,
arena::linked::{fifo, fifo::ends, slots},
raw::EndsPop,
};
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub(in crate::depot) struct State<L: crate::depot::raw::Link> {
head: L,
tail: L,
}
impl<L: crate::depot::raw::Link> State<L> {
const EMPTY: Self = Self {
head: L::NONE,
tail: L::NONE,
};
fn is_empty(self) -> bool {
self.head == L::NONE
}
unsafe fn push_front(ends: &cell::Cell<Self>, index: L::Item, previous: Option<L::Item>) {
let link = unsafe { L::some(index) };
Self::set_front(ends, link, link, previous.is_none());
}
unsafe fn push_back(ends: &cell::Cell<Self>, index: L::Item, previous: Option<L::Item>) {
let link = unsafe { L::some(index) };
Self::set_back(ends, link, link, previous.is_none());
}
fn set_back(ends: &cell::Cell<Self>, head: L, tail: L, empty: bool) {
let state = unsafe { &mut *ends.as_ptr() };
if empty {
state.head = head;
}
state.tail = tail;
}
fn set_front(ends: &cell::Cell<Self>, head: L, tail: L, empty: bool) {
let state = unsafe { &mut *ends.as_ptr() };
state.head = head;
if empty {
state.tail = tail;
}
}
fn advance(ends: &cell::Cell<Self>, next: L) {
Self::set_front(ends, next, L::NONE, next == L::NONE);
}
}
#[repr(transparent)]
#[must_use = "dropping an exclusive chain abandons ownership of its external links"]
#[derive(Debug)]
pub struct Exclusive<L: crate::depot::raw::Link = u32>(pub(in crate::depot) State<L>);
#[repr(transparent)]
pub struct Cell<L: crate::depot::raw::Link = u32>(pub(in crate::depot) cell::Cell<State<L>>);
#[must_use = "a present front must be consumed or left linked"]
pub struct Front<O, L: crate::depot::raw::Link = u32> {
owner: O,
present: L::Item,
}
impl<O, L: crate::depot::raw::Link> Front<O, L> {
pub fn get(&self) -> L::Item {
self.present
}
}
impl<L: depot::raw::Link, O: crate::depot::raw::EndsPop<L>> Front<O, L> {
pub fn pop<T, R, F: FnOnce(L::Item, T) -> (L, R)>(
proof: depot::raw::Proof<(Self, T, F), fifo::Detach>,
) -> R {
let (front, input, pop) = proof.into_inner();
let (next, result) = pop(front.present, input);
unsafe { front.owner.advance(next) };
result
}
}
impl<L: depot::raw::Link> Front<&Cell<L>, L> {
pub fn advance<T, R, F: FnOnce(L::Item, T) -> (L, R)>(
proof: depot::raw::Proof<(Self, T, F), fifo::Detach>,
) -> (R, Option<Self>) {
let (front, input, pop) = proof.into_inner();
let (next, result) = pop(front.present, input);
unsafe { front.owner.advance(next) };
let next = next.item().map(|present| Self {
owner: front.owner,
present,
});
(result, next)
}
}
impl<L: crate::depot::raw::Link> Exclusive<L> {
pub const fn empty() -> Self {
Self(State::EMPTY)
}
pub fn head(&self) -> Option<L::Item> {
self.0.head.item()
}
pub fn back(&self) -> Option<L::Item> {
self.0.tail.item()
}
pub(crate) const fn head_raw(&self) -> L {
self.0.head
}
pub(crate) const fn tail_raw(&self) -> L {
self.0.tail
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn front(&mut self) -> Option<Front<&mut Self, L>> {
let present = self.0.head.item()?;
Some(Front {
owner: self,
present,
})
}
pub fn push_front(
proof: depot::raw::Proof<(&mut Self, L::Item, Option<L::Item>), fifo::Prepend>,
) {
let (owner, index, previous) = proof.into_inner();
unsafe { State::push_front(cell::Cell::from_mut(&mut owner.0), index, previous) };
}
pub fn push_back(
proof: depot::raw::Proof<(&mut Self, L::Item, Option<L::Item>), fifo::Append>,
) {
let (owner, index, previous) = proof.into_inner();
unsafe { State::push_back(cell::Cell::from_mut(&mut owner.0), index, previous) };
}
}
impl<L: crate::depot::raw::Link> Cell<L> {
pub const fn empty() -> Self {
Self(cell::Cell::new(State::EMPTY))
}
pub(in crate::depot) const unsafe fn from_parts(head: L, tail: L) -> Self {
Self(cell::Cell::new(State { head, tail }))
}
pub fn head(&self) -> Option<L::Item> {
self.0.get().head.item()
}
pub fn back(&self) -> Option<L::Item> {
self.0.get().tail.item()
}
pub fn is_empty(&self) -> bool {
self.0.get().is_empty()
}
pub fn front(&self) -> Option<Front<&Self, L>> {
let present = self.0.get().head.item()?;
Some(Front {
owner: self,
present,
})
}
pub fn pop_front<T, R, F: FnOnce(L::Item, T) -> (L, R)>(
proof: depot::raw::Proof<(&Self, T, F), fifo::Detach>,
) -> Option<R> {
let (owner, input, pop) = proof.into_inner();
let front = owner.head()?;
let (next, result) = pop(front, input);
unsafe { owner.advance(next) };
Some(result)
}
pub fn push_front(proof: depot::raw::Proof<(&Self, L::Item, Option<L::Item>), fifo::Prepend>) {
let (owner, index, previous) = proof.into_inner();
unsafe { State::push_front(&owner.0, index, previous) };
}
pub fn push_back(proof: depot::raw::Proof<(&Self, L::Item, Option<L::Item>), fifo::Append>) {
let (owner, index, previous) = proof.into_inner();
unsafe { State::push_back(&owner.0, index, previous) };
}
pub fn append(proof: depot::raw::Proof<(&Self, L, L, Option<L::Item>), fifo::Join>) {
let (owner, head, tail, previous) = proof.into_inner();
State::set_back(&owner.0, head, tail, previous.is_none());
}
}
unsafe impl<L: depot::raw::Link> EndsPop<L> for &mut Exclusive<L> {
unsafe fn advance(self, next: L) {
State::advance(cell::Cell::from_mut(&mut self.0), next);
}
}
unsafe impl<L: depot::raw::Link> EndsPop<L> for &Cell<L> {
unsafe fn advance(self, next: L) {
State::advance(&self.0, next);
}
}
const _: () = {
let _: [(); mem::size_of::<State<u16>>()] = [(); 2 * mem::size_of::<u16>()];
let _: [(); mem::size_of::<State<u32>>()] = [(); 2 * mem::size_of::<u32>()];
let _: [(); mem::size_of::<Exclusive<u16>>()] = [(); 2 * mem::size_of::<u16>()];
let _: [(); mem::size_of::<Exclusive<u32>>()] = [(); 2 * mem::size_of::<u32>()];
let _: [(); mem::size_of::<Cell<u16>>()] = [(); 2 * mem::size_of::<u16>()];
let _: [(); mem::size_of::<Cell<u32>>()] = [(); 2 * mem::size_of::<u32>()];
let _: [(); mem::align_of::<Exclusive<u32>>()] = [(); mem::align_of::<State<u32>>()];
let _: [(); mem::size_of::<Option<Exclusive<u16>>>()] =
[(); mem::size_of::<Option<State<u16>>>()];
let _: [(); mem::size_of::<Option<Exclusive<u32>>>()] =
[(); mem::size_of::<Option<State<u32>>>()];
let _: [(); mem::size_of::<Front<&'static mut Exclusive>>()] =
[(); 2 * mem::size_of::<usize>()];
let _: [(); mem::size_of::<Front<&'static Cell>>()] = [(); 2 * mem::size_of::<usize>()];
};
impl<Tag> ends::Exclusive<slots::Index<Tag>> {
pub(crate) const unsafe fn from_ends(head: slots::Index<Tag>, tail: slots::Index<Tag>) -> Self {
Self(State { head, tail })
}
pub(crate) unsafe fn append_end<T>(
&mut self,
store: &slots::Store<T>,
index: slots::Index<Tag>,
) {
let previous = self.back();
unsafe {
if let Some(previous) = previous {
store.set_next(previous, index);
}
State::push_back(cell::Cell::from_mut(&mut self.0), index, previous);
}
}
pub(crate) unsafe fn prepend_link<T>(
&mut self,
store: &slots::Store<T>,
index: slots::Index<Tag>,
) {
if self.0.head == slots::Index::NONE {
self.0.tail = index;
}
unsafe { self.link_front(store, index) };
}
pub(crate) unsafe fn link_front<T>(
&mut self,
store: &slots::Store<T>,
index: slots::Index<Tag>,
) {
unsafe { store.set_next(index, self.0.head) };
self.0.head = index;
}
pub(crate) unsafe fn append_chain<T>(&mut self, store: &slots::Store<T>, source: Self) {
unsafe { store.set_next(self.0.tail, source.0.head) };
self.0.tail = source.0.tail;
}
pub(crate) unsafe fn unlink<T>(&mut self, store: &slots::Store<T>, index: slots::Index<Tag>) {
unsafe { self.advance(store.next(index)) };
}
}
const _: [(); mem::size_of::<ends::Exclusive<slots::Index>>()] = [(); 2 * mem::size_of::<u32>()];