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