rat_salsa/poll/
timer.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
36
37
38
39
40
41
42
43
44
use crate::timer::{TimeOut, Timers};
use crate::{Control, PollEvents};
use std::any::Any;
use std::rc::Rc;

/// Processes timers.
#[derive(Debug, Default)]
pub struct PollTimers {
    timers: Rc<Timers>,
}

impl PollTimers {
    pub fn new() -> Self {
        Self {
            timers: Rc::new(Timers::default()),
        }
    }

    pub(crate) fn get_timers(&self) -> Rc<Timers> {
        self.timers.clone()
    }
}

impl<Event, Error> PollEvents<Event, Error> for PollTimers
where
    Event: 'static + Send + From<TimeOut>,
    Error: 'static + Send + From<std::io::Error>,
{
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn poll(&mut self) -> Result<bool, Error> {
        Ok(self.timers.poll())
    }

    fn read(&mut self) -> Result<Control<Event>, Error> {
        Ok(self
            .timers
            .read()
            .map(|v| Control::Event(v.0.into()))
            .unwrap_or(Control::Continue))
    }
}