Skip to main content

libutils_cage/
mod.rs

1//^
2//^ HEAD
3//^
4
5//> HEAD -> MODULES
6mod derivations;
7#[cfg(test)]
8mod tests;
9
10//> HEAD -> STD
11use std::sync::{
12    RwLock,
13    RwLockReadGuard,
14    RwLockWriteGuard
15};
16
17
18//^
19//^ CAGE
20//^
21
22//> CAGE -> DEFINITION
23#[derive(Debug)]
24pub struct Cage<Type: ?Sized> {
25    being: RwLock<Type>
26}
27
28//> CAGE -> SIZED IMPLEMENTATION
29impl<Type: Sized> Cage<Type> {
30    pub const fn new(value: Type) -> Cage<Type> {return Self {
31        being: RwLock::new(value)
32    }}
33    pub fn release(self) -> Type {return self.being.into_inner().unwrap()}
34}
35
36//> CAGE -> IMPLEMENTATION
37impl<Type: ?Sized> Cage<Type> {
38    #[inline]
39    pub fn read<'valid>(&'valid self) -> RwLockReadGuard<'valid, Type> {return self.being.read().unwrap()}
40    #[inline]
41    pub fn write<'valid>(&'valid self) -> RwLockWriteGuard<'valid, Type> {return self.being.write().unwrap()}
42}
43
44//> CAGE -> COPY IMPLEMENTATION
45impl<Type: Copy> Cage<Type> {
46    #[inline]
47    pub fn get(&self) -> Type {*self.read()}
48}
49
50//> CAGE -> CLONE IMPLEMENTATION
51impl<Type: Clone> Cage<Type> {
52    #[inline]
53    pub fn cloned(&self) -> Type {self.read().clone()}
54}