1mod clock;
2mod executor;
3mod test_scheduler;
4#[cfg(test)]
5mod tests;
6
7pub use clock::*;
8pub use executor::*;
9pub use test_scheduler::*;
10
11use async_task::Runnable;
12use futures::{FutureExt as _, channel::oneshot, future::LocalBoxFuture};
13use std::{
14 future::Future,
15 pin::Pin,
16 sync::Arc,
17 task::{Context, Poll},
18 time::Duration,
19};
20
21pub trait Scheduler: Send + Sync {
22 // todo!("consider not taking sessions")
23 fn block(
24 &self,
25 session_id: Option<SessionId>,
26 future: LocalBoxFuture<()>,
27 timeout: Option<Duration>,
28 );
29 fn schedule_foreground(&self, session_id: SessionId, runnable: Runnable);
30 fn schedule_background(&self, runnable: Runnable);
31 fn timer(&self, timeout: Duration) -> Timer;
32 fn clock(&self) -> Arc<dyn Clock>;
33 fn as_test(&self) -> &TestScheduler {
34 panic!("this is not a test scheduler")
35 }
36}
37
38#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
39pub struct SessionId(u16);
40
41impl SessionId {
42 pub fn new(id: u16) -> Self {
43 SessionId(id)
44 }
45}
46
47pub struct Timer(oneshot::Receiver<()>);
48
49impl Timer {
50 pub fn new(rx: oneshot::Receiver<()>) -> Self {
51 Timer(rx)
52 }
53}
54
55impl Future for Timer {
56 type Output = ();
57
58 fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
59 match self.0.poll_unpin(cx) {
60 Poll::Ready(_) => Poll::Ready(()),
61 Poll::Pending => Poll::Pending,
62 }
63 }
64}