polyoxide_rt/runtime.rs
1//! The `Runtime` trait.
2
3use std::fmt;
4use std::time::Duration;
5
6use crate::{BoxFuture, Instant};
7
8/// What a polyoxide crate needs of an async runtime: a detached spawn, a
9/// sleep, and a monotonic clock. Everything else in this crate (`spawn`'s
10/// `Task`, `timeout`, `Interval`) is built on these, so an implementor writes
11/// only these.
12///
13/// A client takes its runtime from its builder, and falls back to
14/// `default_runtime()`; nothing is process-wide.
15pub trait Runtime: Send + Sync + fmt::Debug + 'static {
16 /// Run `task` to completion, detached.
17 fn spawn(&self, task: BoxFuture<()>);
18
19 /// Complete after `duration`. Any duration, `Duration::MAX` included, is
20 /// valid.
21 fn sleep(&self, duration: Duration) -> BoxFuture<()>;
22
23 /// The runtime's monotonic clock.
24 fn now(&self) -> Instant;
25
26 /// Complete at `deadline`, at once when it has passed.
27 fn sleep_until(&self, deadline: Instant) -> BoxFuture<()> {
28 self.sleep(deadline.saturating_duration_since(self.now()))
29 }
30}