libutils 8.34.20

Common library types for faster development
//^
//^ HEAD
//^

//> HEAD -> MODULES
mod derivations;

//> HEAD -> STD
use std::sync::{
    RwLock,
    RwLockReadGuard,
    RwLockWriteGuard
};


//^
//^ CAGE
//^

//> CAGE -> DEFINITION
#[derive(Debug)]
pub struct Cage<Type: ?Sized> {
    being: RwLock<Type>
}

//> CAGE -> SIZED IMPLEMENTATION
impl<Type: Sized> Cage<Type> {
    pub const fn new(value: Type) -> Cage<Type> {return Self {
        being: RwLock::new(value)
    }}
    pub fn release(self) -> Type {return self.being.into_inner().unwrap()}
}

//> CAGE -> IMPLEMENTATION
impl<Type: ?Sized> Cage<Type> {
    #[inline]
    pub fn read<'valid>(&'valid self) -> RwLockReadGuard<'valid, Type> {return self.being.read().unwrap()}
    #[inline]
    pub fn write<'valid>(&'valid self) -> RwLockWriteGuard<'valid, Type> {return self.being.write().unwrap()}
}

//> CAGE -> COPY IMPLEMENTATION
impl<Type: Copy> Cage<Type> {
    #[inline]
    pub fn get(&self) -> Type {*self.read()}
}

//> CAGE -> CLONE IMPLEMENTATION
impl<Type: Clone> Cage<Type> {
    #[inline]
    pub fn cloned(&self) -> Type {self.read().clone()}
}