atomic_state/state/
guard.rs

1use crate::prelude::*;
2
3/// The atomic state guard
4pub struct StateGuard<'a, T: Clone> {
5    pub(super) rw_lock: RwLockWriteGuard<'a, Arc<T>>,
6    pub(super) arc_swap: Arc<ArcSwapAny<Arc<T>>>,
7    pub(super) data: T,
8}
9
10impl<'a, T: Clone> ::std::ops::Drop for StateGuard<'a, T> {
11    fn drop(self: &mut Self) {
12        let data = Arc::new(self.data.clone());
13        
14        *self.rw_lock = data.clone();
15        self.arc_swap.store(data);
16    }
17}
18
19impl<'a, T: Clone> ::std::ops::Deref for StateGuard<'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 StateGuard<'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 StateGuard<'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 StateGuard<'a, T> {
40    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
41        write!(f, "{}", &self.data)
42    }
43}