restor/concurrent_black_box/
mod.rs

1use std::any::{Any, TypeId};
2
3use super::black_box::{
4    DynamicResult,
5    ErrorDesc::{self, *},
6    StorageUnit, Unit,
7};
8mod newtype;
9pub use newtype::{MutexStorage, RwLockStorage};
10use parking_lot::{
11    MappedMutexGuard, MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, MutexGuard, RwLock,
12    RwLockReadGuard, RwLockWriteGuard,
13};
14
15#[repr(transparent)]
16pub struct MutexUnit<T> {
17    inner: Mutex<T>,
18}
19
20impl<T> MutexUnit<T> {
21    pub fn new(data: T) -> Self {
22        Self {
23            inner: Mutex::new(data),
24        }
25    }
26}
27
28impl<'a, T: 'static + Send> Unit<'a> for MutexUnit<StorageUnit<T>> {
29    type Borrowed = MappedMutexGuard<'a, dyn Any>;
30    type MutBorrowed = MappedMutexGuard<'a, dyn Any>;
31    fn insert_any(&self, new: Box<dyn Any>) -> Option<(Box<dyn Any>, ErrorDesc)> {
32        let newtype = new.type_id();
33        if let Some(mut x) = self.inner.try_lock() {
34            if new.is::<T>() {
35                x.insert(*new.downcast::<T>().unwrap_or_else(|_| {
36                    panic!(
37                        "Tried to insert an object with type {:?} into a storage of type {:?}",
38                        newtype,
39                        TypeId::of::<T>()
40                    )
41                }));
42                None
43            } else if new.is::<Vec<T>>() {
44                x.insert_many(*new.downcast::<Vec<T>>().unwrap());
45                None
46            } else {
47                Some((new, ErrorDesc::NoMatchingType))
48            }
49        } else {
50            Some((new, ErrorDesc::BorrowedIncompatibly))
51        }
52    }
53    fn waiting_insert(&self, new: Box<dyn Any>) -> Option<(Box<dyn Any>, ErrorDesc)> {
54        let newtype = new.type_id();
55        if new.is::<T>() || new.is::<Vec<T>>() {
56            let mut x = self.inner.lock();
57            if new.is::<T>() {
58                x.insert(*new.downcast::<T>().unwrap_or_else(|_| {
59                    panic!(
60                        "Tried to insert an object with type {:?} into a storage of type {:?}",
61                        newtype,
62                        TypeId::of::<T>()
63                    )
64                }));
65                None
66            } else {
67                x.insert_many(*new.downcast::<Vec<T>>().unwrap());
68                None
69            }
70        } else {
71            Some((new, ErrorDesc::NoMatchingType))
72        }
73    }
74    fn storage(&'a self) -> DynamicResult<MappedMutexGuard<'a, dyn Any>> {
75        self.inner
76            .try_lock()
77            .map(|x| MutexGuard::map::<dyn Any, _>(x, |z| &mut *z))
78            .ok_or(BorrowedIncompatibly)
79    }
80    fn storage_mut(&'a self) -> DynamicResult<MappedMutexGuard<'a, dyn Any>> {
81        self.storage()
82    }
83
84    fn waiting_storage(&'a self) -> MappedMutexGuard<'a, dyn Any> {
85        MutexGuard::map::<dyn Any, _>(self.inner.lock(), |z| &mut *z)
86    }
87    fn waiting_storage_mut(&'a self) -> MappedMutexGuard<'a, dyn Any> {
88        self.waiting_storage()
89    }
90
91    fn id(&self) -> TypeId {
92        TypeId::of::<T>()
93    }
94}
95
96#[repr(transparent)]
97pub struct RwLockUnit<T> {
98    inner: RwLock<T>,
99}
100
101impl<T> RwLockUnit<T> {
102    pub fn new(data: T) -> Self {
103        Self {
104            inner: RwLock::new(data),
105        }
106    }
107    #[cfg(test)]
108    pub fn inner(&self) -> &RwLock<T> {
109        &self.inner
110    }
111}
112
113impl<'a, T: 'static + Send> Unit<'a> for RwLockUnit<StorageUnit<T>> {
114    type Borrowed = MappedRwLockReadGuard<'a, dyn Any>;
115    type MutBorrowed = MappedRwLockWriteGuard<'a, dyn Any>;
116
117    fn insert_any(&self, new: Box<dyn Any>) -> Option<(Box<dyn Any>, ErrorDesc)> {
118        let newtype = new.type_id();
119        if let Some(mut x) = self.inner.try_write() {
120            if new.is::<T>() {
121                x.insert(*new.downcast::<T>().unwrap_or_else(|_| {
122                    panic!(
123                        "Tried to insert an object with type {:?} into a storage of type {:?}",
124                        newtype,
125                        TypeId::of::<T>()
126                    )
127                }));
128                None
129            } else if new.is::<Vec<T>>() {
130                x.insert_many(*new.downcast::<Vec<T>>().unwrap());
131                None
132            } else {
133                Some((new, ErrorDesc::NoMatchingType))
134            }
135        } else {
136            Some((new, ErrorDesc::BorrowedIncompatibly))
137        }
138    }
139
140    fn waiting_insert(&self, new: Box<dyn Any>) -> Option<(Box<dyn Any>, ErrorDesc)> {
141        let newtype = new.type_id();
142        if new.is::<T>() || new.is::<Vec<T>>() {
143            let mut x = self.inner.write();
144            if new.is::<T>() {
145                x.insert(*new.downcast::<T>().unwrap_or_else(|_| {
146                    panic!(
147                        "Tried to insert an object with type {:?} into a storage of type {:?}",
148                        newtype,
149                        TypeId::of::<T>()
150                    )
151                }));
152                None
153            } else {
154                x.insert_many(*new.downcast::<Vec<T>>().unwrap());
155                None
156            }
157        } else {
158            Some((new, ErrorDesc::NoMatchingType))
159        }
160    }
161
162    fn storage(&'a self) -> DynamicResult<MappedRwLockReadGuard<'a, dyn Any>> {
163        self.inner
164            .try_read()
165            .map(|x| RwLockReadGuard::map::<dyn Any, _>(x, |z| &*z))
166            .ok_or(BorrowedIncompatibly)
167    }
168    fn storage_mut(&'a self) -> DynamicResult<MappedRwLockWriteGuard<'a, dyn Any>> {
169        self.inner
170            .try_write()
171            .map(|x| RwLockWriteGuard::map::<dyn Any, _>(x, |z| &mut *z))
172            .ok_or(BorrowedIncompatibly)
173    }
174
175    fn waiting_storage(&'a self) -> MappedRwLockReadGuard<'a, dyn Any> {
176        RwLockReadGuard::map::<dyn Any, _>(self.inner.read(), |z| &*z)
177    }
178    fn waiting_storage_mut(&'a self) -> MappedRwLockWriteGuard<'a, dyn Any> {
179        RwLockWriteGuard::map::<dyn Any, _>(self.inner.write(), |z| &mut *z)
180    }
181
182    fn id(&self) -> TypeId {
183        TypeId::of::<T>()
184    }
185}