stateright/actor/
timers.rs

1use crate::{util::HashableHashSet, Rewrite, RewritePlan};
2use std::hash::Hash;
3
4use super::Id;
5
6/// A collection of timers that have been set for a given actor.
7#[derive(Clone, Debug, Hash, PartialEq, Eq, serde::Serialize)]
8pub struct Timers<T: Hash + Eq>(HashableHashSet<T>);
9
10impl<T: Hash + Eq> Default for Timers<T> {
11    fn default() -> Self {
12        Self::new()
13    }
14}
15
16impl<T> Timers<T>
17where
18    T: Hash + Eq,
19{
20    /// Create a new timer set.
21    pub fn new() -> Self {
22        Self(HashableHashSet::new())
23    }
24
25    /// Set a timer.
26    pub fn set(&mut self, timer: T) -> bool {
27        self.0.insert(timer)
28    }
29
30    /// Cancel a timer.
31    pub fn cancel(&mut self, timer: &T) -> bool {
32        self.0.remove(timer)
33    }
34
35    /// Cancels all timers.
36    pub fn cancel_all(&mut self) {
37        self.0.clear();
38    }
39
40    /// Iterate through the currently set timers.
41    pub fn iter(&self) -> impl Iterator<Item = &T> {
42        self.0.iter()
43    }
44}
45
46impl<T> Rewrite<Id> for Timers<T>
47where
48    T: Eq + Hash + Clone,
49{
50    fn rewrite<S>(&self, _plan: &RewritePlan<Id, S>) -> Self {
51        self.clone()
52    }
53}