use crate::tcell::TCell;
use std::{
cell::UnsafeCell,
fmt::{self, Debug, Formatter},
mem::ManuallyDrop,
ops::{Deref, DerefMut},
};
#[derive(PartialEq, Eq)]
pub struct Error {
_private: (),
}
impl Debug for Error {
#[cold]
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.pad("Error { .. }")
}
}
impl<T> From<SetError<T>> for Error {
#[inline]
fn from(_: SetError<T>) -> Self {
Self::RETRY
}
}
impl Error {
pub const RETRY: Self = Error { _private: () };
}
#[derive(Debug)]
pub struct SetError<T> {
pub value: T,
pub error: Error,
}
impl<T> SetError<T> {
#[inline]
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> SetError<U> {
SetError {
value: f(self.value),
error: self.error,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Ordering {
ReadWrite,
Read,
}
impl Default for Ordering {
#[inline]
fn default() -> Self {
Ordering::ReadWrite
}
}
pub unsafe trait Read<'a> {
#[doc(hidden)]
unsafe fn _get_unchecked<T>(
&self,
tcell: &TCell<T>,
ordering: Ordering,
) -> Result<ManuallyDrop<T>, Error>;
}
pub unsafe trait Write<'a> {
#[doc(hidden)]
unsafe fn _set_unchecked<T: Send + 'static>(
&self,
tcell: &TCell<T>,
src: impl _TValue<T>,
) -> Result<(), SetError<T>>;
#[doc(hidden)]
fn _privatize<F: FnOnce() + Copy + Send + 'static>(&self, privatizer: F);
}
pub trait RW<'tcell>: Read<'tcell> + Write<'tcell> {}
impl<'tcell, T: Read<'tcell> + Write<'tcell>> RW<'tcell> for T {}
#[doc(hidden)]
pub unsafe trait _TValue<T: 'static>: 'static {
const REQUEST_TCELL_LIFETIME: bool;
}
unsafe impl<T: 'static> _TValue<T> for T {
const REQUEST_TCELL_LIFETIME: bool = false;
}
pub unsafe auto trait Borrow {}
impl<T: ?Sized> !Borrow for UnsafeCell<T> {}
unsafe impl<T: ?Sized> Borrow for Box<T> {}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub(crate) struct AssertBorrow<T> {
value: T,
}
unsafe impl<T> Borrow for AssertBorrow<T> {}
impl<T> std::borrow::Borrow<T> for AssertBorrow<T> {
#[inline]
fn borrow(&self) -> &T {
&self.value
}
}
impl<T> std::borrow::BorrowMut<T> for AssertBorrow<T> {
#[inline]
fn borrow_mut(&mut self) -> &mut T {
&mut self.value
}
}
impl<T> Deref for AssertBorrow<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&self.value
}
}
impl<T> DerefMut for AssertBorrow<T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
&mut self.value
}
}