use std::{array, cell, fmt::Debug, mem::MaybeUninit, num::NonZero, ops, ptr};
pub(crate) use account::Account;
use crate::{
base::marker::ThreadBound,
mem::{rc, slot},
};
mod account;
mod balance;
pub use balance::{acquire_raw, demand, release_raw, try_acquire};
mod owner;
pub use owner::Owner;
pub(crate) fn cast_uninit<T, U, const N: usize>(
source: &mut [MaybeUninit<T>; N],
) -> &mut [MaybeUninit<U>; N] {
const {
assert!(size_of::<U>() <= size_of::<T>(), "size");
assert!(align_of::<T>().is_multiple_of(align_of::<U>()), "align");
};
unsafe { &mut *ptr::from_mut(source).cast::<[MaybeUninit<U>; N]>() }
}
#[derive(Debug)]
#[repr(transparent)]
pub struct Occupied<'a, T> {
source: &'a mut Option<T>,
bound: ThreadBound,
}
#[derive(Debug)]
#[must_use]
#[repr(transparent)]
pub struct Vacant<'a, T> {
source: &'a mut Option<T>,
bound: ThreadBound,
}
pub unsafe fn project<T, M>(source: &slot::Storage<T, M>) -> &T {
unsafe { initialized(&source.value) }
}
pub unsafe fn project_mut<T, M>(source: &mut slot::Storage<T, M>) -> &mut T {
unsafe { initialized_mut(&mut source.value) }
}
pub(crate) unsafe fn initialized<T>(source: &Option<T>) -> &T {
unsafe { source.as_slice().get_unchecked(0) }
}
unsafe fn initialized_mut<T>(source: &mut Option<T>) -> &mut T {
unsafe { &mut *source.as_mut_slice().as_mut_ptr() }
}
pub unsafe fn occupy<T, M>(storage: &mut slot::Storage<T, M>) -> Occupied<'_, T> {
Occupied {
source: &mut storage.value,
bound: ThreadBound::NEW,
}
}
pub unsafe fn vacancy<T, M>(storage: &mut slot::Storage<T, M>) -> Vacant<'_, T> {
Vacant {
source: &mut storage.value,
bound: ThreadBound::NEW,
}
}
impl<'a, T> Occupied<'a, T> {
pub fn clear(self) {
unsafe {
ptr::drop_in_place(self.source.as_mut_slice().as_mut_ptr());
ptr::write(self.source, None);
}
}
pub fn get(&self) -> &T {
unsafe { initialized(self.source) }
}
pub fn get_mut(&mut self) -> &mut T {
unsafe { initialized_mut(self.source) }
}
pub fn into_mut(self) -> &'a mut T {
unsafe { initialized_mut(self.source) }
}
pub fn take(self) -> (T, Vacant<'a, T>) {
let value = unsafe {
let value = self.source.as_mut_slice().as_mut_ptr().read();
ptr::write(self.source, None);
value
};
(
value,
Vacant {
source: self.source,
bound: ThreadBound::NEW,
},
)
}
}
impl<T> super::Return for Occupied<'_, T> {
type Item = ();
fn return_item(self, _: ()) {
self.clear();
}
}
impl<'a, T> Vacant<'a, T> {
pub fn insert(self, value: T) -> Occupied<'a, T> {
unsafe { ptr::write(self.source, Some(value)) };
Occupied {
source: self.source,
bound: ThreadBound::NEW,
}
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct Count<W: Counter = u32> {
value: cell::Cell<W>,
thread: ThreadBound,
}
impl<W: Counter> Count<W> {
pub const fn empty() -> Self {
Self {
value: cell::Cell::new(W::ZERO),
thread: ThreadBound::NEW,
}
}
pub const fn one() -> Self {
Self {
value: cell::Cell::new(W::ONE),
thread: ThreadBound::NEW,
}
}
#[inline]
pub(crate) unsafe fn try_add(&self, amount: W::NonZero) -> Result<(), W> {
let current: W = unsafe { self.value.get().nonzero() }.into();
self.value
.set(current.checked_add(amount.into()).ok_or(current)?);
Ok(())
}
}
const _: () = assert!(size_of::<Count>() == size_of::<u32>());
pub unsafe trait Edge<W: Counter = u32> {
fn count(&self) -> &crate::mem::raw::Count<W>;
#[inline]
fn is_empty(&self) -> bool {
self.count().value.get() == W::ZERO
}
#[inline]
fn is_unique(&self) -> bool {
self.count().value.get() == W::ONE
}
#[inline]
unsafe fn activate(&self) {
self.count().value.set(W::ONE);
}
unsafe fn start_pair(&self) {
self.count().value.set(W::TWO);
}
unsafe fn retire(&self) {
self.count().value.set(W::ZERO);
}
#[inline]
unsafe fn try_retain(&self) -> Result<(), rc::Error> {
unsafe { self.count().try_add(W::ONE.nonzero()) }.map_err(|_| rc::Error::Exhausted)
}
#[inline]
unsafe fn release(&self) -> rc::Release {
let count = self.count();
let current: W = unsafe { count.value.get().nonzero() }.into();
let next = current - W::ONE;
let release = if next == W::ZERO {
rc::Release::Last
} else {
rc::Release::Shared
};
count.value.set(next);
release
}
}
pub unsafe trait Target<W: Counter = u32>: crate::mem::raw::Edge<W> + Sized {
unsafe fn destroy(pointer: ptr::NonNull<Self>);
}
pub unsafe trait Amount<const N: usize, W: crate::mem::raw::Word>: Copy + Eq {
const AXES: ops::Range<usize> = 0..N;
fn get(self) -> [W; N];
}
unsafe impl<const N: usize, W: crate::mem::raw::Word> Amount<N, W> for [W; N] {
fn get(self) -> [W; N] {
self
}
}
unsafe impl<const N: usize, W: Word> Sum<N, W> for [W; N] {
unsafe fn add_raw(self, other: Self) -> Self {
array::from_fn(|axis| self[axis] + other[axis])
}
}
pub unsafe trait Sum<const N: usize, W: Word>: Amount<N, W> {
unsafe fn add_raw(self, other: Self) -> Self;
}
pub unsafe trait Word:
Copy + Eq + Ord + ops::Add<Output = Self> + ops::Sub<Output = Self> + owner::Sealed
{
const ZERO: Self;
fn checked_add(self, other: Self) -> Option<Self>;
fn sub_or_zero(self, other: Self) -> Self;
}
unsafe impl Word for u32 {
const ZERO: Self = 0;
fn checked_add(self, other: Self) -> Option<Self> {
self.checked_add(other)
}
fn sub_or_zero(self, other: Self) -> Self {
self.saturating_sub(other)
}
}
unsafe impl Word for usize {
const ZERO: Self = 0;
fn checked_add(self, other: Self) -> Option<Self> {
self.checked_add(other)
}
fn sub_or_zero(self, other: Self) -> Self {
self.saturating_sub(other)
}
}
pub trait Counter: Word {
type NonZero: Copy + Debug + Into<Self>;
const ONE: Self;
const TWO: Self;
unsafe fn nonzero(self) -> Self::NonZero;
}
impl Counter for u32 {
type NonZero = NonZero<Self>;
const ONE: Self = 1;
const TWO: Self = 2;
unsafe fn nonzero(self) -> Self::NonZero {
unsafe { NonZero::new_unchecked(self) }
}
}
impl Counter for usize {
type NonZero = NonZero<Self>;
const ONE: Self = 1;
const TWO: Self = 2;
unsafe fn nonzero(self) -> Self::NonZero {
unsafe { NonZero::new_unchecked(self) }
}
}