A better Rust ATProto crate
1use super::Instant;
2
3use std::sync::RwLock;
4
5pub(crate) struct AtomicInstant {
6 instant: RwLock<Option<Instant>>,
7}
8
9impl Default for AtomicInstant {
10 fn default() -> Self {
11 Self {
12 instant: RwLock::new(None),
13 }
14 }
15}
16
17impl AtomicInstant {
18 pub(crate) fn new(timestamp: Instant) -> Self {
19 let ai = Self::default();
20 ai.set_instant(timestamp);
21 ai
22 }
23
24 pub(crate) fn is_set(&self) -> bool {
25 self.instant.read().expect("lock poisoned").is_some()
26 }
27
28 pub(crate) fn instant(&self) -> Option<Instant> {
29 *self.instant.read().expect("lock poisoned")
30 }
31
32 pub(crate) fn set_instant(&self, instant: Instant) {
33 *self.instant.write().expect("lock poisoned") = Some(instant);
34 }
35}