1#![feature(test)]
7#![feature(lock_value_accessors)]
8
9extern crate test;
11
12#[cfg(test)]
14mod benches;
15mod derivations;
16#[cfg(test)]
17mod tests;
18
19use std::sync::{
21 RwLock,
22 RwLockReadGuard,
23 RwLockWriteGuard
24};
25
26
27#[derive(Debug)]
33pub struct Cage<Type: ?Sized> {
34 being: RwLock<Type>
35}
36
37impl<Type: Sized> Cage<Type> {
39 #[inline]
40 pub const fn new(value: Type) -> Cage<Type> {return Self {
41 being: RwLock::new(value)
42 }}
43 #[inline]
44 pub fn release(self) -> Type {return self.being.into_inner().unwrap()}
45 #[inline]
46 pub fn replace(&self, value: Type) -> () {self.being.replace(value).unwrap();}
47}
48
49impl<Type: ?Sized> Cage<Type> {
51 #[inline]
52 pub fn read<'valid>(&'valid self) -> RwLockReadGuard<'valid, Type> {return self.being.read().unwrap()}
53 #[inline]
54 pub fn write<'valid>(&'valid self) -> RwLockWriteGuard<'valid, Type> {return self.being.write().unwrap()}
55}
56
57impl<Type: Copy> Cage<Type> {
59 #[inline]
60 pub fn get(&self) -> Type {*self.read()}
61}
62
63impl<Type: Clone> Cage<Type> {
65 #[inline]
66 pub fn cloned(&self) -> Type {self.read().clone()}
67}