#![allow(unsafe_code)]
use core::{
cell::UnsafeCell,
ops::{Deref, DerefMut},
};
use crate::raw_mutex::RawMutex;
#[must_use = "if unused, the Mutex will immediately unlock"]
pub struct MutexGuard<'a, R: RawMutex + 'a, T: ?Sized> {
pub(crate) data: &'a UnsafeCell<T>,
pub(crate) _raw_guard: R::Guard<'a>,
}
impl<R: RawMutex, T: ?Sized> Deref for MutexGuard<'_, R, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.data.get() }
}
}
impl<R: RawMutex, T: ?Sized> DerefMut for MutexGuard<'_, R, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.data.get() }
}
}
impl<R: RawMutex, T: ?Sized + core::fmt::Debug> core::fmt::Debug for MutexGuard<'_, R, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(&**self, f)
}
}
unsafe impl<'a, R: RawMutex + 'a, T: ?Sized + Send> Send for MutexGuard<'a, R, T> where
R::Guard<'a>: Send
{
}
unsafe impl<'a, R: RawMutex + 'a, T: ?Sized + Sync> Sync for MutexGuard<'a, R, T> where
R::Guard<'a>: Sync
{
}