use core::{
cell::UnsafeCell,
fmt, mem,
ops::{Deref, DerefMut},
};
#[derive(Debug)]
pub struct FairlyUnsafeCell<T: ?Sized>(UnsafeCell<T>);
impl<T> FairlyUnsafeCell<T> {
#[inline]
pub const fn new(value: T) -> Self {
Self(UnsafeCell::new(value))
}
#[inline]
pub fn into_inner(self) -> T {
self.0.into_inner()
}
#[inline]
pub unsafe fn replace(&self, t: T) -> T {
mem::replace(&mut *self.borrow_mut(), t)
}
#[inline]
pub unsafe fn replace_with<F: FnOnce(&mut T) -> T>(&self, f: F) -> T {
let mut_borrow = &mut *self.borrow_mut();
let replacement = f(mut_borrow);
mem::replace(mut_borrow, replacement)
}
#[inline]
pub unsafe fn swap(&self, other: &Self) {
mem::swap(&mut *self.borrow_mut(), &mut *other.borrow_mut())
}
}
impl<T: ?Sized> FairlyUnsafeCell<T> {
#[inline]
pub unsafe fn borrow(&self) -> Ref<'_, T> {
Ref(unsafe { &*self.0.get() })
}
#[inline]
pub unsafe fn borrow_mut(&self) -> RefMut<'_, T> {
RefMut(unsafe { &mut *self.0.get() })
}
#[inline]
pub fn as_ptr(&self) -> *mut T {
self.0.get()
}
#[inline(always)]
pub fn get_mut(&mut self) -> &mut T {
self.0.get_mut()
}
}
impl<T: Default> Default for FairlyUnsafeCell<T> {
#[inline]
fn default() -> FairlyUnsafeCell<T> {
Self(UnsafeCell::default())
}
}
impl<T> From<T> for FairlyUnsafeCell<T> {
fn from(t: T) -> FairlyUnsafeCell<T> {
Self(UnsafeCell::from(t))
}
}
#[derive(Debug)]
pub struct Ref<'b, T>(&'b T)
where
T: 'b + ?Sized;
impl<'b, T: ?Sized> Ref<'b, T> {
#[must_use]
#[inline]
pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> {
Ref(orig.0)
}
#[inline]
pub fn map<U: ?Sized, F>(orig: Ref<'b, T>, f: F) -> Ref<'b, U>
where
F: FnOnce(&T) -> &U,
{
Ref(f(orig.0))
}
#[inline]
pub fn filter_map<U: ?Sized, F>(orig: Ref<'b, T>, f: F) -> Result<Ref<'b, U>, Self>
where
F: FnOnce(&T) -> Option<&U>,
{
match f(orig.0) {
Some(yay) => Ok(Ref(yay)),
None => Err(orig),
}
}
#[inline]
pub fn map_split<U: ?Sized, V: ?Sized, F>(orig: Ref<'b, T>, f: F) -> (Ref<'b, U>, Ref<'b, V>)
where
F: FnOnce(&T) -> (&U, &V),
{
let (a, b) = f(orig.0);
(Ref(a), Ref(b))
}
}
impl<T: ?Sized> Deref for Ref<'_, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.0
}
}
impl<T: ?Sized + fmt::Display> fmt::Display for Ref<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug)]
pub struct RefMut<'b, T>(&'b mut T)
where
T: 'b + ?Sized;
impl<'b, T: ?Sized> RefMut<'b, T> {
#[inline]
pub fn map<U: ?Sized, F>(orig: RefMut<'b, T>, f: F) -> RefMut<'b, U>
where
F: FnOnce(&mut T) -> &mut U,
{
RefMut(f(orig.0))
}
#[inline]
pub fn map_split<U: ?Sized, V: ?Sized, F>(
orig: RefMut<'b, T>,
f: F,
) -> (RefMut<'b, U>, RefMut<'b, V>)
where
F: FnOnce(&mut T) -> (&mut U, &mut V),
{
let (a, b) = f(orig.0);
(RefMut(a), RefMut(b))
}
}
impl<T: ?Sized> Deref for RefMut<'_, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.0
}
}
impl<T: ?Sized> DerefMut for RefMut<'_, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
self.0
}
}
impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}