polyoxide_rt/tokio.rs
1//! `TokioRuntime`, the native runtime.
2
3use std::time::Duration;
4
5use crate::{BoxFuture, Instant, Runtime};
6
7/// The runtime over tokio's spawn, sleep and clock.
8///
9/// It reads tokio's clock, so a test on `tokio::time::pause` (or
10/// `#[tokio::test(start_paused = true)]`) drives it. `spawn` panics outside a
11/// tokio runtime, as `tokio::spawn` does.
12#[derive(Debug, Clone, Copy, Default)]
13pub struct TokioRuntime;
14
15impl Runtime for TokioRuntime {
16 fn spawn(&self, task: BoxFuture<()>) {
17 ::tokio::spawn(task);
18 }
19
20 fn sleep(&self, duration: Duration) -> BoxFuture<()> {
21 Box::pin(::tokio::time::sleep(duration))
22 }
23
24 fn now(&self) -> Instant {
25 ::tokio::time::Instant::now().into_std()
26 }
27}