use std::{cell, marker, pin};
use crate::base::cell::{Sealed, brand};
pub trait Brand: Sealed {}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Identity<'id, const ID: u8 = 0>(marker::PhantomData<*mut &'id ()>);
#[repr(transparent)]
pub struct Token<B: Sealed> {
proof: marker::PhantomData<B>,
}
#[derive(Debug)]
#[repr(transparent)]
pub struct Value<B: Sealed, T> {
value: cell::UnsafeCell<T>,
brand: marker::PhantomData<B>,
}
impl<const ID: u8> Sealed for Identity<'_, ID> {}
impl<const ID: u8> Brand for Identity<'_, ID> {}
impl<'id, const ID: u8> Identity<'id, ID> {
pub const fn bind<T>(self, value: T) -> Value<Self, T> {
Value::new(value)
}
pub const fn reborrow(&self) -> Identity<'_, ID> {
Identity(marker::PhantomData)
}
}
impl Identity<'_> {
pub fn scope<R>(f: impl for<'id> FnOnce(Identity<'id>) -> R) -> R {
f(Identity(marker::PhantomData))
}
pub fn scope_mut<T: ?Sized, R>(
root: &mut T,
f: impl for<'id> FnOnce(Identity<'id>, &'id mut T) -> R,
) -> R {
f(Identity(marker::PhantomData), root)
}
}
impl<B: Sealed> Token<B> {
pub(in crate::base::cell) const fn new() -> Self {
Self {
proof: marker::PhantomData,
}
}
pub const fn bind<T>(&self, value: T) -> Value<B, T> {
Value::new(value)
}
}
impl<B: Brand> Token<B> {
pub fn borrow<'a, T>(&'a self, value: &'a Value<B, T>) -> &'a T {
unsafe { &*value.value.get() }
}
pub fn borrow_mut<'a, T: Unpin>(&'a mut self, value: &'a Value<B, T>) -> &'a mut T {
unsafe { &mut *value.value.get() }
}
}
impl<'id, const ID: u8> Token<Identity<'id, ID>> {
pub const fn proof(&self) -> Identity<'id, ID> {
Identity(marker::PhantomData)
}
}
impl Token<Identity<'_>> {
pub fn scope<R>(f: impl for<'id> FnOnce(Token<Identity<'id>>) -> R) -> R {
f(Token::new())
}
pub fn scope_pinned<T: ?Sized, R>(
root: pin::Pin<&mut T>,
f: impl for<'id> FnOnce(Token<Identity<'id>>, pin::Pin<&'id mut T>) -> R,
) -> R {
f(Token::new(), root)
}
}
impl<'id> Token<brand::Identity<'id>> {
pub fn pin_mut<'a, T>(
&'a mut self,
value: pin::Pin<&'a Value<brand::Identity<'id>, T>>,
) -> pin::Pin<&'a mut T> {
unsafe { pin::Pin::new_unchecked(&mut *value.value.get()) }
}
pub fn borrow_pin<'a, T>(
&'a self,
value: pin::Pin<&'a Value<brand::Identity<'id>, T>>,
) -> pin::Pin<&'a T> {
unsafe { pin::Pin::new_unchecked(&*value.value.get()) }
}
}
impl<B: Sealed, T> Value<B, T> {
const fn new(value: T) -> Self {
Self {
value: cell::UnsafeCell::new(value),
brand: marker::PhantomData,
}
}
}
impl<B: Brand, T> Value<B, T> {
pub fn get_mut(&mut self) -> &mut T {
self.value.get_mut()
}
pub fn into_inner(self) -> T {
self.value.into_inner()
}
}
const _: () = {
assert!(size_of::<Identity<'static>>() == 0);
assert!(size_of::<Identity<'static, 1>>() == 0);
assert!(size_of::<Token<Identity<'static>>>() == 0);
assert!(size_of::<Value<Identity<'static>, u64>>() == size_of::<u64>());
assert!(align_of::<Value<Identity<'static>, u64>>() == align_of::<u64>());
assert!(size_of::<(Token<Identity<'static>>, Token<Identity<'static, 1>>)>() == 0);
assert!(size_of::<Value<Identity<'static, 1>, u64>>() == size_of::<u64>());
assert!(align_of::<Value<Identity<'static, 1>, u64>>() == align_of::<u64>());
assert!(size_of::<Token<brand::Identity<'static>>>() == 0);
assert!(size_of::<Value<brand::Identity<'static>, u64>>() == size_of::<u64>());
assert!(align_of::<Value<brand::Identity<'static>, u64>>() == align_of::<u64>());
};