use std::marker::PhantomData;
use std::ops::Deref;
use parking_lot::RwLockReadGuard;
pub struct Guard<'a, V> {
#[allow(unused)]
lock: RwLockReadGuard<'a, crate::shard::Shard>,
value: *const V,
marker: PhantomData<&'a V>,
}
impl<'a, V> Guard<'a, V> {
pub(crate) unsafe fn new(
lock: RwLockReadGuard<'a, crate::shard::Shard>,
value: *const V,
) -> Self {
Self {
lock,
value,
marker: PhantomData,
}
}
}
unsafe impl<V: Sync> Sync for Guard<'_, V> {}
impl<V> Deref for Guard<'_, V> {
type Target = V;
fn deref(&self) -> &V {
unsafe { &*self.value }
}
}
impl<V: std::fmt::Debug> std::fmt::Debug for Guard<'_, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
(**self).fmt(f)
}
}
impl<V: std::fmt::Display> std::fmt::Display for Guard<'_, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
(**self).fmt(f)
}
}
impl<V: PartialEq> PartialEq for Guard<'_, V> {
fn eq(&self, other: &Self) -> bool {
**self == **other
}
}
impl<V: Eq> Eq for Guard<'_, V> {}
impl<V: PartialOrd> PartialOrd for Guard<'_, V> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
(**self).partial_cmp(&**other)
}
}
impl<V: Ord> Ord for Guard<'_, V> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
(**self).cmp(&**other)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_guard_is_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<Guard<i32>>();
}
}