use core::{
cell::{BorrowError, BorrowMutError, Cell, Ref, RefCell, RefMut},
cmp::Ordering,
fmt,
};
use crate::{barrier::Unlock, Collect, Collection, Gc, Mutation};
macro_rules! make_lock_wrapper {
(
$(#[$meta:meta])*
locked = $locked_type:ident as $gc_locked_type:ident;
unlocked = $unlocked_type:ident unsafe $unsafe_unlock_method:ident;
impl Sized { $($sized_items:tt)* }
impl ?Sized { $($unsized_items:tt)* }
) => {
#[doc = stringify!($unlocked_type)]
#[doc = stringify!($unlocked_type)]
#[doc = stringify!($locked_type)]
$(#[$meta])*
#[repr(transparent)]
pub struct $locked_type<T: ?Sized> {
cell: $unlocked_type<T>,
}
#[doc = concat!("An alias for `Gc<'gc, ", stringify!($locked_type), "<T>>`.")]
pub type $gc_locked_type<'gc, T> = Gc<'gc, $locked_type<T>>;
impl<T> $locked_type<T> {
#[inline]
pub fn new(t: T) -> $locked_type<T> {
Self { cell: $unlocked_type::new(t) }
}
#[inline]
pub fn into_inner(self) -> T {
self.cell.into_inner()
}
$($sized_items)*
}
impl<T: ?Sized> $locked_type<T> {
#[inline]
pub fn as_ptr(&self) -> *mut T {
self.cell.as_ptr()
}
$($unsized_items)*
#[doc = concat!("Access the wrapped [`", stringify!($unlocked_type), "`].")]
#[doc = stringify!($unlocked_type)]
#[inline]
pub unsafe fn $unsafe_unlock_method(&self) -> &$unlocked_type<T> {
&self.cell
}
#[inline]
pub fn get_mut(&mut self) -> &mut T {
self.cell.get_mut()
}
}
impl<T: ?Sized> Unlock for $locked_type<T> {
type Unlocked = $unlocked_type<T>;
#[inline]
unsafe fn unlock_unchecked(&self) -> &Self::Unlocked {
&self.cell
}
}
impl<T> From<T> for $locked_type<T> {
#[inline]
fn from(t: T) -> Self {
Self::new(t)
}
}
impl<T> From<$unlocked_type<T>> for $locked_type<T> {
#[inline]
fn from(cell: $unlocked_type<T>) -> Self {
Self { cell }
}
}
};
}
make_lock_wrapper!(
#[derive(Default)]
locked = Lock as GcLock;
unlocked = Cell unsafe as_cell;
impl Sized {
#[inline]
pub fn get(&self) -> T where T: Copy {
self.cell.get()
}
#[inline]
pub fn take(&self) -> T where T: Default {
self.cell.take()
}
}
impl ?Sized {}
);
impl<T: Copy + fmt::Debug> fmt::Debug for Lock<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Lock").field(&self.cell).finish()
}
}
impl<'gc, T: Copy + 'gc> Gc<'gc, Lock<T>> {
#[inline]
pub fn get(self) -> T {
self.cell.get()
}
#[inline]
pub fn set(self, mc: &Mutation<'gc>, t: T) {
self.unlock(mc).set(t);
}
}
unsafe impl<'gc, T: Collect + Copy + 'gc> Collect for Lock<T> {
#[inline]
fn needs_trace() -> bool {
T::needs_trace()
}
#[inline]
fn trace(&self, cc: &Collection) {
T::trace(&self.get(), cc);
}
}
impl<T: Copy> Clone for Lock<T> {
#[inline]
fn clone(&self) -> Self {
Self::new(self.get())
}
}
impl<T: PartialEq + Copy> PartialEq for Lock<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.get() == other.get()
}
}
impl<T: Eq + Copy> Eq for Lock<T> {}
impl<T: PartialOrd + Copy> PartialOrd for Lock<T> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.get().partial_cmp(&other.get())
}
}
impl<T: Ord + Copy> Ord for Lock<T> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.get().cmp(&other.get())
}
}
make_lock_wrapper!(
#[derive(Clone, Default, Eq, PartialEq, Ord, PartialOrd)]
locked = RefLock as GcRefLock;
unlocked = RefCell unsafe as_ref_cell;
impl Sized {
#[inline]
pub fn take(&self) -> T where T: Default {
self.cell.take()
}
}
impl ?Sized {
#[track_caller]
#[inline]
pub fn borrow<'a>(&'a self) -> Ref<'a, T> {
self.cell.borrow()
}
#[inline]
pub fn try_borrow<'a>(&'a self) -> Result<Ref<'a, T>, BorrowError> {
self.cell.try_borrow()
}
}
);
impl<T: fmt::Debug + ?Sized> fmt::Debug for RefLock<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut fmt = fmt.debug_tuple("RefLock");
match self.try_borrow() {
Ok(borrow) => fmt.field(&borrow),
Err(_) => {
struct BorrowedPlaceholder;
impl fmt::Debug for BorrowedPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<borrowed>")
}
}
fmt.field(&BorrowedPlaceholder)
}
}
.finish()
}
}
impl<'gc, T: ?Sized + 'gc> Gc<'gc, RefLock<T>> {
#[track_caller]
#[inline]
pub fn borrow(self) -> Ref<'gc, T> {
RefLock::borrow(self.as_ref())
}
#[inline]
pub fn try_borrow(self) -> Result<Ref<'gc, T>, BorrowError> {
RefLock::try_borrow(self.as_ref())
}
#[track_caller]
#[inline]
pub fn borrow_mut(self, mc: &Mutation<'gc>) -> RefMut<'gc, T> {
self.unlock(mc).borrow_mut()
}
#[inline]
pub fn try_borrow_mut(self, mc: &Mutation<'gc>) -> Result<RefMut<'gc, T>, BorrowMutError> {
self.unlock(mc).try_borrow_mut()
}
}
unsafe impl<'gc, T: Collect + 'gc> Collect for RefLock<T> {
#[inline]
fn needs_trace() -> bool {
T::needs_trace()
}
#[inline]
fn trace(&self, cc: &Collection) {
self.borrow().trace(cc);
}
}