quick_cache/
shim.rs

1#![allow(dead_code)]
2
3#[cfg(not(feature = "shuttle"))]
4pub(crate) use crate::rw_lock;
5#[cfg(not(feature = "shuttle"))]
6pub use std::{sync, thread};
7
8#[cfg(feature = "shuttle")]
9pub use shuttle::*;
10
11// TODO: OnceLock isn't current present in shuttle.
12// Using the std version like this only works in shuttle
13// because we're not relying on any OnceLock blocking behavior.
14pub use std::sync::OnceLock;
15
16#[cfg(feature = "shuttle")]
17pub mod rw_lock {
18    use std::ops::{Deref, DerefMut};
19
20    #[derive(Default, Debug)]
21    pub struct RwLock<T>(::shuttle::sync::RwLock<T>);
22
23    #[derive(Debug)]
24    pub struct RwLockReadGuard<'rwlock, T>(::shuttle::sync::RwLockReadGuard<'rwlock, T>);
25
26    #[derive(Debug)]
27    pub struct RwLockWriteGuard<'rwlock, T>(::shuttle::sync::RwLockWriteGuard<'rwlock, T>);
28
29    impl<T> RwLock<T> {
30        pub const fn new(t: T) -> Self {
31            Self(::shuttle::sync::RwLock::new(t))
32        }
33
34        pub fn into_inner(self) -> T {
35            self.0.into_inner().unwrap()
36        }
37
38        pub fn read(&self) -> RwLockReadGuard<'_, T> {
39            RwLockReadGuard(self.0.read().unwrap())
40        }
41
42        pub fn write(&self) -> RwLockWriteGuard<'_, T> {
43            RwLockWriteGuard(self.0.write().unwrap())
44        }
45
46        pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>> {
47            self.0.try_write().map(RwLockWriteGuard).ok()
48        }
49
50        pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> {
51            self.0.try_read().map(RwLockReadGuard).ok()
52        }
53    }
54
55    impl<T> Deref for RwLockReadGuard<'_, T> {
56        type Target = T;
57
58        fn deref(&self) -> &Self::Target {
59            &self.0
60        }
61    }
62
63    impl<T> Deref for RwLockWriteGuard<'_, T> {
64        type Target = T;
65
66        fn deref(&self) -> &Self::Target {
67            &self.0
68        }
69    }
70
71    impl<T> DerefMut for RwLockWriteGuard<'_, T> {
72        fn deref_mut(&mut self) -> &mut Self::Target {
73            &mut self.0
74        }
75    }
76}