input/
idle.rs

1use crate::{Event, Loop};
2
3/// Idle arguments, such as expected idle time in seconds.
4#[derive(Copy, Clone, PartialEq, PartialOrd, Debug, Deserialize, Serialize)]
5pub struct IdleArgs {
6    /// Expected idle time in seconds.
7    pub dt: f64,
8}
9
10/// When background tasks should be performed.
11pub trait IdleEvent: Sized {
12    /// Creates an idle event.
13    fn from_idle_args(args: &IdleArgs, old_event: &Self) -> Option<Self>;
14    /// Creates an update event with delta time.
15    fn from_dt(dt: f64, old_event: &Self) -> Option<Self> {
16        IdleEvent::from_idle_args(&IdleArgs { dt }, old_event)
17    }
18    /// Calls closure if this is an idle event.
19    fn idle<U, F>(&self, f: F) -> Option<U>
20    where
21        F: FnMut(&IdleArgs) -> U;
22    /// Returns idle arguments.
23    fn idle_args(&self) -> Option<IdleArgs> {
24        self.idle(|args| *args)
25    }
26}
27
28impl IdleEvent for Event {
29    fn from_idle_args(args: &IdleArgs, _old_event: &Self) -> Option<Self> {
30        Some(Event::Loop(Loop::Idle(*args)))
31    }
32
33    fn idle<U, F>(&self, mut f: F) -> Option<U>
34    where
35        F: FnMut(&IdleArgs) -> U,
36    {
37        match *self {
38            Event::Loop(Loop::Idle(ref args)) => Some(f(args)),
39            _ => None,
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_input_idle() {
50        use IdleArgs;
51
52        let e: Event = IdleArgs { dt: 1.0 }.into();
53        let x: Option<Event> = IdleEvent::from_idle_args(&IdleArgs { dt: 1.0 }, &e);
54        let y: Option<Event> = x
55            .clone()
56            .unwrap()
57            .idle(|args| IdleEvent::from_idle_args(args, x.as_ref().unwrap()))
58            .unwrap();
59        assert_eq!(x, y);
60    }
61}