moka/common/concurrent/atomic_time/
atomic_time_compat.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use super::Instant;

use parking_lot::RwLock;

pub(crate) struct AtomicInstant {
    instant: RwLock<Option<Instant>>,
}

impl Default for AtomicInstant {
    fn default() -> Self {
        Self {
            instant: RwLock::new(None),
        }
    }
}

impl AtomicInstant {
    pub(crate) fn new(timestamp: Instant) -> Self {
        let ai = Self::default();
        ai.set_instant(timestamp);
        ai
    }

    pub(crate) fn is_set(&self) -> bool {
        self.instant.read().is_some()
    }

    pub(crate) fn instant(&self) -> Option<Instant> {
        *self.instant.read()
    }

    pub(crate) fn set_instant(&self, instant: Instant) {
        *self.instant.write() = Some(instant);
    }
}