atomic_state/state/
guard.rs

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