nxtquic-api 0.1.3

High-level async API for NxtQuic
Documentation
//! Async runtime abstractions and drivers for driving QUIC endpoints and connections.

pub mod driver;

pub use driver::EndpointDriver;
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;

/// Trait abstracting asynchronous runtime operations for QUIC endpoints.
pub trait Runtime: Send + Sync + 'static {
    /// Spawns a future on the runtime.
    fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>);

    /// Returns a future that completes after `duration`.
    fn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send>>;
}

/// Tokio runtime implementation for [`Runtime`].
#[derive(Clone, Copy, Debug, Default)]
pub struct TokioRuntime;

impl Runtime for TokioRuntime {
    fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
        tokio::spawn(future);
    }

    fn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send>> {
        Box::pin(tokio::time::sleep(duration))
    }
}