use core::ops::DerefMut;
use std::{
sync::{
Mutex,
MutexGuard
},
fmt::{
Debug,
Formatter,
Result as Format
}
};
pub struct Cage<Type: ?Sized>(Mutex<Type>);
impl<Type: Sized> Cage<Type> {
pub const fn new(value: Type) -> Cage<Type> {return Self(Mutex::new(value))}
pub fn release(self) -> Type {return self.0.into_inner().unwrap()}
}
impl<Type: ?Sized> Cage<Type> {
#[inline]
pub fn peak<Return, Closure: FnOnce(&mut Type) -> Return>(&self, closure: Closure) -> Return {
return closure(self.0.lock().unwrap().deref_mut());
}
#[inline]
pub fn free(&self) -> MutexGuard<'_, Type> {return self.0.lock().unwrap()}
}
impl<Type: Clone> Cage<Type> {
pub fn cloned(&self) -> Type {return self.0.lock().unwrap().clone()}
}
impl<Type: Debug + ?Sized> Debug for Cage<Type> {
fn fmt(&self, formatter: &mut Formatter<'_>) -> Format {
write!(formatter, "Cage({:?})", &self.0)
}
}