atomic_state/state/
guard.rs

1use crate::prelude::*;
2use super::ERR_MSG;
3
4/// The state mutex guard
5pub struct AtomicStateGuard<T: Clone> {
6    pub(super) guard: OwnedMutexGuard<T>,
7    pub(super) fast: Arc<RwLock<T>>,
8}
9
10impl<T: Clone> ::std::ops::Drop for AtomicStateGuard<T> {
11    fn drop(self: &mut Self) {
12        *self.fast.write().expect(ERR_MSG) = (*self.guard).clone();
13    }
14}
15
16impl<T: Clone> ::std::ops::Deref for AtomicStateGuard<T> {
17    type Target = T;
18    
19    fn deref(&self) -> &Self::Target {
20        &self.guard
21    }
22}
23
24impl<T: Clone> ::std::ops::DerefMut for AtomicStateGuard<T> {
25    fn deref_mut(&mut self) -> &mut T {
26        &mut self.guard
27    }
28}
29
30impl<T: Clone + ::std::fmt::Debug> ::std::fmt::Debug for AtomicStateGuard<T> {
31    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
32        write!(f, "{:?}", &self.guard)
33    }
34}
35
36impl<T: Clone + ::std::fmt::Display> ::std::fmt::Display for AtomicStateGuard<T> {
37    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
38        write!(f, "{}", &self.guard)
39    }
40}