use crate::error::Result;
pub struct ExposeGuard<'a, T: ?Sized> {
data: &'a T,
relock: Option<Box<dyn FnOnce() + 'a>>,
}
impl<'a, T: ?Sized> ExposeGuard<'a, T> {
pub(crate) fn new(data: &'a T, relock: impl FnOnce() + 'a) -> Self {
Self {
data,
relock: Some(Box::new(relock)),
}
}
pub(crate) fn unguarded(data: &'a T) -> Self {
Self { data, relock: None }
}
}
impl<T: ?Sized> core::ops::Deref for ExposeGuard<'_, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
self.data
}
}
impl<T: ?Sized> Drop for ExposeGuard<'_, T> {
fn drop(&mut self) {
if let Some(relock) = self.relock.take() {
relock();
}
}
}
pub struct ExposeGuardMut<'a, T: ?Sized> {
data: &'a mut T,
relock: Option<Box<dyn FnOnce() + 'a>>,
}
impl<'a, T: ?Sized> ExposeGuardMut<'a, T> {
pub(crate) fn new(data: &'a mut T, relock: impl FnOnce() + 'a) -> Self {
Self {
data,
relock: Some(Box::new(relock)),
}
}
pub(crate) fn unguarded(data: &'a mut T) -> Self {
Self { data, relock: None }
}
}
impl<T: ?Sized> core::ops::Deref for ExposeGuardMut<'_, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
self.data
}
}
impl<T: ?Sized> core::ops::DerefMut for ExposeGuardMut<'_, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.data
}
}
impl<T: ?Sized> Drop for ExposeGuardMut<'_, T> {
fn drop(&mut self) {
if let Some(relock) = self.relock.take() {
relock();
}
}
}
pub trait Expose {
type Target: ?Sized;
fn expose(&self) -> &Self::Target;
}
pub trait ExposeMut: Expose {
fn expose_mut(&mut self) -> &mut Self::Target;
}
pub trait ExposeGuarded: Expose {
fn expose_guarded(&self) -> Result<ExposeGuard<'_, Self::Target>>;
}
pub trait ExposeGuardedMut: ExposeGuarded + ExposeMut {
fn expose_guarded_mut(&mut self) -> Result<ExposeGuardMut<'_, Self::Target>>;
}