Skip to content

Commit

Permalink
Copy the AsyncStdRuntime module to create SmolRuntime
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Jan 23, 2024
1 parent c91504c commit eeeb84a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions quinn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tls-rustls = ["rustls", "proto/tls-rustls", "ring"]
ring = ["proto/ring"]
runtime-tokio = ["tokio/time", "tokio/rt", "tokio/net"]
runtime-async-std = ["async-io", "async-std"]
runtime-smol = ["async-io", "smol"]

# Write logs via the `log` crate when no `tracing` subscriber exists
log = ["tracing/log", "proto/log", "udp/log"]
Expand Down
13 changes: 9 additions & 4 deletions quinn/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ pub fn default_runtime() -> Option<Arc<dyn Runtime>> {
return Some(Arc::new(AsyncStdRuntime));
}

#[cfg(feature = "runtime-smol")]
{
return Some(Arc::new(SmolRuntime));
}

#[cfg(not(feature = "runtime-async-std"))]
None
}
Expand All @@ -93,7 +98,7 @@ mod tokio;
#[cfg(feature = "runtime-tokio")]
pub use self::tokio::TokioRuntime;

#[cfg(feature = "runtime-async-std")]
mod async_std;
#[cfg(feature = "runtime-async-std")]
pub use self::async_std::AsyncStdRuntime;
#[cfg(feature = "async-io")]
mod async_io;
#[cfg(feature = "async-io")]
pub use self::async_io::*;
29 changes: 28 additions & 1 deletion quinn/src/runtime/async_std.rs → quinn/src/runtime/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,40 @@ use async_io::{Async, Timer};

use super::{AsyncTimer, AsyncUdpSocket, Runtime};

#[cfg(feature = "smol")]
pub use self::smol::SmolRuntime;

#[cfg(feature = "smol")]
mod smol {
use super::*;
/// A Quinn runtime for smol
#[derive(Debug)]
pub struct SmolRuntime;

impl Runtime for SmolRuntime {
fn new_timer(&self, t: Instant) -> Pin<Box<dyn AsyncTimer>> {
Box::pin(Timer::at(t))
}

fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
::smol::spawn(future).detach();
}

fn wrap_udp_socket(
&self,
sock: std::net::UdpSocket,
) -> io::Result<Arc<dyn AsyncUdpSocket>> {
Ok(Arc::new(UdpSocket::new(sock)?))
}
}
}

#[cfg(feature = "async-std")]
pub use self::async_std::AsyncStdRuntime;

#[cfg(feature = "async-std")]
mod async_std {
use super::*;

/// A Quinn runtime for async-std
#[derive(Debug)]
pub struct AsyncStdRuntime;
Expand Down

0 comments on commit eeeb84a

Please sign in to comment.