use spin::{RwLockReadGuard, RwLockWriteGuard};
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};
pub struct RefGuard<'a, V> {
pub(crate) _lock: RwLockReadGuard<'a, ()>,
pub(crate) _value: &'a V,
}
impl<'a, V> Deref for RefGuard<'a, V> {
type Target = V;
#[inline]
fn deref(&self) -> &Self::Target {
self._value
}
}
impl<'a, V> Debug for RefGuard<'a, V>
where
V: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self._value)
}
}
impl<'a, V> PartialEq for RefGuard<'a, V>
where
V: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self._value.eq(other._value)
}
}
impl<'a, V> Eq for RefGuard<'a, V> where V: Eq {}
impl<'a, V> std::fmt::Display for RefGuard<'a, V>
where
V: std::fmt::Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self._value.fmt(f)
}
}
impl<'a, V> AsRef<V> for RefGuard<'a, V> {
#[inline]
fn as_ref(&self) -> &V {
self._value
}
}
pub struct MutRefGuard<'a, V> {
pub(crate) _lock: RwLockReadGuard<'a, ()>,
pub(crate) _value: &'a mut V,
}
impl<'a, V> Deref for MutRefGuard<'a, V> {
type Target = V;
#[inline]
fn deref(&self) -> &Self::Target {
&*self._value
}
}
impl<'a, V> DerefMut for MutRefGuard<'a, V> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut *self._value
}
}
impl<'a, V> Debug for MutRefGuard<'a, V>
where
V: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", &*self._value)
}
}
impl<'a, V> PartialEq for MutRefGuard<'a, V>
where
V: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
(*self._value).eq(&*other._value)
}
}
impl<'a, V> Eq for MutRefGuard<'a, V> where V: Eq {}
impl<'a, V> std::fmt::Display for MutRefGuard<'a, V>
where
V: std::fmt::Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
(*self._value).fmt(f)
}
}
impl<'a, V> AsRef<V> for MutRefGuard<'a, V> {
#[inline]
fn as_ref(&self) -> &V {
&*self._value
}
}
impl<'a, V> AsMut<V> for MutRefGuard<'a, V> {
#[inline]
fn as_mut(&mut self) -> &mut V {
&mut *self._value
}
}
pub struct ReadGuard<'a, V> {
pub(crate) _lock: RwLockReadGuard<'a, ()>,
pub(crate) _value_lock: RwLockReadGuard<'a, V>,
}
impl<'a, V> Deref for ReadGuard<'a, V> {
type Target = V;
#[inline]
fn deref(&self) -> &Self::Target {
&*self._value_lock
}
}
impl<'a, V> Debug for ReadGuard<'a, V>
where
V: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", &*self._value_lock)
}
}
impl<'a, V> PartialEq for ReadGuard<'a, V>
where
V: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
(*self._value_lock).eq(&*other._value_lock)
}
}
impl<'a, V> Eq for ReadGuard<'a, V> where V: Eq {}
impl<'a, V> std::fmt::Display for ReadGuard<'a, V>
where
V: std::fmt::Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
(*self._value_lock).fmt(f)
}
}
impl<'a, V> AsRef<V> for ReadGuard<'a, V> {
#[inline]
fn as_ref(&self) -> &V {
&*self._value_lock
}
}
pub struct WriteGuard<'a, V> {
pub(crate) _lock: RwLockReadGuard<'a, ()>,
pub(crate) _value_lock: RwLockWriteGuard<'a, V>,
}
impl<'a, V> Deref for WriteGuard<'a, V> {
type Target = V;
#[inline]
fn deref(&self) -> &Self::Target {
&*self._value_lock
}
}
impl<'a, V> DerefMut for WriteGuard<'a, V> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut *self._value_lock
}
}
impl<'a, V> Debug for WriteGuard<'a, V>
where
V: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", &*self._value_lock)
}
}
impl<'a, V> PartialEq for WriteGuard<'a, V>
where
V: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
(*self._value_lock).eq(&*other._value_lock)
}
}
impl<'a, V> Eq for WriteGuard<'a, V> where V: Eq {}
impl<'a, V> std::fmt::Display for WriteGuard<'a, V>
where
V: std::fmt::Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
(*self._value_lock).fmt(f)
}
}
impl<'a, V> AsRef<V> for WriteGuard<'a, V> {
#[inline]
fn as_ref(&self) -> &V {
&*self._value_lock
}
}
impl<'a, V> AsMut<V> for WriteGuard<'a, V> {
#[inline]
fn as_mut(&mut self) -> &mut V {
&mut *self._value_lock
}
}
pub struct EntryGuard<'a, V> {
pub(crate) _lock: RwLockWriteGuard<'a, ()>,
pub(crate) _value_lock: RwLockWriteGuard<'a, V>,
}
impl<'a, V> Deref for EntryGuard<'a, V> {
type Target = V;
#[inline]
fn deref(&self) -> &Self::Target {
&*self._value_lock
}
}
impl<'a, V> DerefMut for EntryGuard<'a, V> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut *self._value_lock
}
}
impl<'a, V> Debug for EntryGuard<'a, V>
where
V: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", &*self._value_lock)
}
}
impl<'a, V> PartialEq for EntryGuard<'a, V>
where
V: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
(*self._value_lock).eq(&*other._value_lock)
}
}
impl<'a, V> Eq for EntryGuard<'a, V> where V: Eq {}
impl<'a, V> std::fmt::Display for EntryGuard<'a, V>
where
V: std::fmt::Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
(*self._value_lock).fmt(f)
}
}
impl<'a, V> AsRef<V> for EntryGuard<'a, V> {
#[inline]
fn as_ref(&self) -> &V {
&*self._value_lock
}
}
impl<'a, V> AsMut<V> for EntryGuard<'a, V> {
#[inline]
fn as_mut(&mut self) -> &mut V {
&mut *self._value_lock
}
}