Skip to content

Commit 1f10a0b

Browse files
make tokio dependency optional by adding alternative channel provider (async-channel)
1 parent 50c2d12 commit 1f10a0b

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

h3/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ edition = "2018"
1212
bytes = "1"
1313
futures-util = { version = "0.3", default-features = false }
1414
http = "0.2.3"
15-
tokio = { version = "1", features = ["sync"] }
15+
tokio = { version = "1", features = ["sync"], optional = true }
16+
async-channel = { version = "1", optional = true }
1617
tracing = "0.1.18"
1718
fastrand = "1.7.0"
1819

@@ -27,11 +28,14 @@ quinn = { version = "0.8.0", default-features = false, features = [
2728
quinn-proto = { version = "0.8.0", default-features = false }
2829
rcgen = "0.9"
2930
rustls = "0.20"
30-
tokio = { version = "1", features = ["rt", "macros", "io-util", "io-std"] }
31+
tokio = { version = "1", features = ["rt", "macros", "io-util", "io-std", "sync"] }
3132
tracing-subscriber = { version = "0.3", default-features = false, features = [
3233
"fmt",
3334
"ansi",
3435
"env-filter",
3536
"time",
3637
"tracing-log",
3738
] }
39+
40+
[features]
41+
default = ["tokio"]

h3/src/server.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ use bytes::{Buf, BytesMut};
6161
use futures_util::future;
6262
use http::{response, HeaderMap, Request, Response, StatusCode};
6363
use quic::StreamId;
64-
use tokio::sync::mpsc;
6564

6665
use crate::{
6766
connection::{self, ConnectionInner, ConnectionState, SharedStateRef},
@@ -74,6 +73,38 @@ use crate::{
7473
};
7574
use tracing::{error, trace, warn};
7675

76+
#[cfg(all(feature = "tokio", not(feature = "async-channel")))]
77+
use tokio::sync::mpsc;
78+
79+
#[cfg(feature = "async-channel")]
80+
mod mpsc {
81+
use futures_util::StreamExt;
82+
use std::task::{Context, Poll};
83+
84+
pub fn unbounded_channel<T>() -> (UnboundedSender<T>, UnboundedReceiver<T>) {
85+
let (sender, receiver) = async_channel::unbounded();
86+
(UnboundedSender(sender), UnboundedReceiver(receiver))
87+
}
88+
89+
#[derive(Clone)]
90+
pub struct UnboundedSender<T>(async_channel::Sender<T>);
91+
92+
impl<T> UnboundedSender<T> {
93+
pub fn send(&self, msg: T) -> Result<(), async_channel::TrySendError<T>> {
94+
self.0.try_send(msg)
95+
}
96+
}
97+
98+
#[derive(Clone)]
99+
pub struct UnboundedReceiver<T>(async_channel::Receiver<T>);
100+
101+
impl<T> UnboundedReceiver<T> {
102+
pub fn poll_recv(&mut self, cx: &mut Context) -> Poll<Option<T>> {
103+
self.0.poll_next_unpin(cx)
104+
}
105+
}
106+
}
107+
77108
/// Create a builder of HTTP/3 server connections
78109
///
79110
/// This function creates a [`Builder`] that carries settings that can

0 commit comments

Comments
 (0)