diff --git a/h3/Cargo.toml b/h3/Cargo.toml index 956d8ff1..ae2c1ee4 100644 --- a/h3/Cargo.toml +++ b/h3/Cargo.toml @@ -12,7 +12,8 @@ edition = "2018" bytes = "1" futures-util = { version = "0.3", default-features = false } http = "0.2.3" -tokio = { version = "1", features = ["sync"] } +tokio = { version = "1", features = ["sync"], optional = true } +async-channel = { version = "1", optional = true } tracing = "0.1.18" fastrand = "1.7.0" @@ -27,7 +28,7 @@ quinn = { version = "0.8.0", default-features = false, features = [ quinn-proto = { version = "0.8.0", default-features = false } rcgen = "0.9" rustls = "0.20" -tokio = { version = "1", features = ["rt", "macros", "io-util", "io-std"] } +tokio = { version = "1", features = ["rt", "macros", "io-util", "io-std", "sync"] } tracing-subscriber = { version = "0.3", default-features = false, features = [ "fmt", "ansi", @@ -35,3 +36,6 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [ "time", "tracing-log", ] } + +[features] +default = ["tokio"] diff --git a/h3/src/server.rs b/h3/src/server.rs index 54113124..cfa86bde 100644 --- a/h3/src/server.rs +++ b/h3/src/server.rs @@ -61,7 +61,6 @@ use bytes::{Buf, BytesMut}; use futures_util::future; use http::{response, HeaderMap, Request, Response, StatusCode}; use quic::StreamId; -use tokio::sync::mpsc; use crate::{ connection::{self, ConnectionInner, ConnectionState, SharedStateRef}, @@ -74,6 +73,38 @@ use crate::{ }; use tracing::{error, trace, warn}; +#[cfg(all(feature = "tokio", not(feature = "async-channel")))] +use tokio::sync::mpsc; + +#[cfg(feature = "async-channel")] +mod mpsc { + use futures_util::StreamExt; + use std::task::{Context, Poll}; + + pub fn unbounded_channel() -> (UnboundedSender, UnboundedReceiver) { + let (sender, receiver) = async_channel::unbounded(); + (UnboundedSender(sender), UnboundedReceiver(receiver)) + } + + #[derive(Clone)] + pub struct UnboundedSender(async_channel::Sender); + + impl UnboundedSender { + pub fn send(&self, msg: T) -> Result<(), async_channel::TrySendError> { + self.0.try_send(msg) + } + } + + #[derive(Clone)] + pub struct UnboundedReceiver(async_channel::Receiver); + + impl UnboundedReceiver { + pub fn poll_recv(&mut self, cx: &mut Context) -> Poll> { + self.0.poll_next_unpin(cx) + } + } +} + /// Create a builder of HTTP/3 server connections /// /// This function creates a [`Builder`] that carries settings that can