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 authored and djc committed Jan 24, 2024
1 parent b2e0667 commit 2e4074a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
3 changes: 2 additions & 1 deletion 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 All @@ -43,7 +44,7 @@ rustc-hash = "1.1"
pin-project-lite = "0.2"
proto = { package = "quinn-proto", path = "../quinn-proto", version = "0.11", default-features = false }
rustls = { version = "0.21.0", default-features = false, features = ["quic"], optional = true }
smol = { version = "1.3", optional = true }
smol = { version = "2", optional = true }
thiserror = "1.0.21"
tracing = "0.1.10"
tokio = { version = "1.28.1", features = ["sync"] }
Expand Down
18 changes: 12 additions & 6 deletions quinn/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ pub trait AsyncUdpSocket: Send + Sync + Debug + 'static {
///
/// If `runtime-tokio` is enabled and this function is called from within a Tokio runtime context,
/// then `TokioRuntime` is returned. Otherwise, if `runtime-async-std` is enabled, `AsyncStdRuntime`
/// is returned. Otherwise, `None` is returned.
/// is returned. Otherwise, if `runtime-smol` is enabled, `SmolRuntime` is returned.
/// Otherwise, `None` is returned.
pub fn default_runtime() -> Option<Arc<dyn Runtime>> {
#[cfg(feature = "runtime-tokio")]
{
Expand All @@ -84,7 +85,12 @@ pub fn default_runtime() -> Option<Arc<dyn Runtime>> {
return Some(Arc::new(AsyncStdRuntime));
}

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

#[cfg(not(any(feature = "runtime-async-std", feature = "runtime-smol")))]
None
}

Expand All @@ -93,7 +99,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: 29 additions & 0 deletions 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,6 +11,35 @@ 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;

Expand Down

0 comments on commit 2e4074a

Please sign in to comment.