rat_salsa/poll/
timer.rs

1use crate::Control;
2use crate::poll::PollEvents;
3use crate::timer::{TimeOut, Timers};
4use std::any::Any;
5use std::rc::Rc;
6use std::time::Duration;
7
8/// Processes timers.
9#[derive(Debug, Default)]
10pub struct PollTimers {
11    timers: Rc<Timers>,
12}
13
14impl PollTimers {
15    pub fn new() -> Self {
16        Self {
17            timers: Rc::new(Timers::default()),
18        }
19    }
20
21    pub(crate) fn get_timers(&self) -> Rc<Timers> {
22        self.timers.clone()
23    }
24}
25
26impl<Event, Error> PollEvents<Event, Error> for PollTimers
27where
28    Event: 'static + From<TimeOut>,
29    Error: 'static + From<std::io::Error>,
30{
31    fn as_any(&self) -> &dyn Any {
32        self
33    }
34
35    fn sleep_time(&self) -> Option<Duration> {
36        self.timers.sleep_time()
37    }
38
39    fn poll(&mut self) -> Result<bool, Error> {
40        Ok(self.timers.poll())
41    }
42
43    fn read(&mut self) -> Result<Control<Event>, Error> {
44        Ok(self
45            .timers
46            .read()
47            .map(|v| Control::Event(v.0.into()))
48            .unwrap_or(Control::Continue))
49    }
50}