Skip to main content

nxtquic_api/runtime/
mod.rs

1//! Async runtime abstractions and drivers for driving QUIC endpoints and connections.
2
3pub mod driver;
4
5pub use driver::EndpointDriver;
6use std::future::Future;
7use std::pin::Pin;
8use std::time::Duration;
9
10/// Trait abstracting asynchronous runtime operations for QUIC endpoints.
11pub trait Runtime: Send + Sync + 'static {
12    /// Spawns a future on the runtime.
13    fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>);
14
15    /// Returns a future that completes after `duration`.
16    fn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send>>;
17}
18
19/// Tokio runtime implementation for [`Runtime`].
20#[derive(Clone, Copy, Debug, Default)]
21pub struct TokioRuntime;
22
23impl Runtime for TokioRuntime {
24    fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
25        tokio::spawn(future);
26    }
27
28    fn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send>> {
29        Box::pin(tokio::time::sleep(duration))
30    }
31}