Skip to main content

dope/runtime/
dispatcher.rs

1use std::pin::Pin;
2use std::time::Instant;
3
4use crate::{Driver, backend};
5
6pub enum Idle {
7    Busy,
8    Park(Option<Instant>),
9}
10
11impl Idle {
12    pub fn reduce(self, other: Self) -> Self {
13        match (self, other) {
14            (Idle::Busy, _) | (_, Idle::Busy) => Idle::Busy,
15            (Idle::Park(a), Idle::Park(b)) => Idle::Park(match (a, b) {
16                (Some(x), Some(y)) => Some(x.min(y)),
17                (a, b) => a.or(b),
18            }),
19        }
20    }
21}
22
23pub trait Dispatcher {
24    const SHUTDOWN_DRAIN: std::time::Duration = std::time::Duration::from_secs(2);
25
26    fn dispatch(self: Pin<&mut Self>, ev: backend::Event, driver: &mut Driver);
27
28    fn on_wake(self: Pin<&mut Self>, target: backend::token::Token, driver: &mut Driver);
29
30    fn pre_park(self: Pin<&mut Self>, driver: &mut Driver);
31
32    fn idle(self: Pin<&Self>) -> Idle;
33
34    fn on_shutdown(self: Pin<&mut Self>, driver: &mut Driver) {
35        let _ = (self, driver);
36    }
37}