Skip to content

Commit e303f5a

Browse files
tottotoseanmonstar
authored andcommitted
refactor(common): move common::watch module to http1 feature
1 parent be2024a commit e303f5a

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ include = [
2121

2222
[dependencies]
2323
bytes = "1"
24-
futures-channel = "0.3"
25-
futures-util = { version = "0.3", default-features = false }
2624
http = "1"
2725
http-body = "1"
2826
pin-project-lite = "0.2.4"
2927
tokio = { version = "1", features = ["sync"] }
3028

3129
# Optional
3230

31+
futures-channel = { version = "0.3", optional = true }
32+
futures-util = { version = "0.3", default-features = false, optional = true }
3333
h2 = { version = "0.4", optional = true }
3434
http-body-util = { version = "0.1", optional = true }
3535
httparse = { version = "1.8", optional = true }
@@ -74,8 +74,8 @@ full = [
7474
]
7575

7676
# HTTP versions
77-
http1 = ["dep:httparse", "dep:itoa"]
78-
http2 = ["dep:h2"]
77+
http1 = ["dep:futures-channel", "dep:futures-util", "dep:httparse", "dep:itoa"]
78+
http2 = ["dep:futures-channel", "dep:futures-util", "dep:h2"]
7979

8080
# Client/Server
8181
client = ["dep:want"]

src/body/incoming.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use std::pin::Pin;
55
use std::task::{Context, Poll};
66

77
use bytes::Bytes;
8-
use futures_channel::mpsc;
9-
use futures_channel::oneshot;
8+
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
9+
use futures_channel::{mpsc, oneshot};
1010
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
1111
use futures_util::{stream::FusedStream, Stream}; // for mpsc::Receiver
12+
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
1213
use http::HeaderMap;
1314
use http_body::{Body, Frame, SizeHint};
1415

@@ -17,11 +18,14 @@ use http_body::{Body, Frame, SizeHint};
1718
any(feature = "client", feature = "server")
1819
))]
1920
use super::DecodedLength;
21+
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
2022
use crate::common::watch;
2123
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
2224
use crate::proto::h2::ping;
2325

26+
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
2427
type BodySender = mpsc::Sender<Result<Bytes, crate::Error>>;
28+
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
2529
type TrailersSender = oneshot::Sender<HeaderMap>;
2630

2731
/// A stream of `Bytes`, used when receiving bodies from the network.
@@ -78,13 +82,16 @@ enum Kind {
7882
/// [`Body::channel()`]: struct.Body.html#method.channel
7983
/// [`Sender::abort()`]: struct.Sender.html#method.abort
8084
#[must_use = "Sender does nothing unless sent on"]
85+
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
8186
pub(crate) struct Sender {
8287
want_rx: watch::Receiver,
8388
data_tx: BodySender,
8489
trailers_tx: Option<TrailersSender>,
8590
}
8691

92+
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
8793
const WANT_PENDING: usize = 1;
94+
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
8895
const WANT_READY: usize = 2;
8996

9097
impl Incoming {
@@ -331,6 +338,7 @@ impl fmt::Debug for Incoming {
331338
}
332339
}
333340

341+
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
334342
impl Sender {
335343
/// Check to see if this `Sender` can send more data.
336344
pub(crate) fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<crate::Result<()>> {
@@ -350,11 +358,13 @@ impl Sender {
350358
}
351359
}
352360

361+
#[cfg(test)]
353362
async fn ready(&mut self) -> crate::Result<()> {
354363
futures_util::future::poll_fn(|cx| self.poll_ready(cx)).await
355364
}
356365

357366
/// Send data on data channel when it is ready.
367+
#[cfg(test)]
358368
#[allow(unused)]
359369
pub(crate) async fn send_data(&mut self, chunk: Bytes) -> crate::Result<()> {
360370
self.ready().await?;
@@ -392,7 +402,7 @@ impl Sender {
392402
.map_err(|err| err.into_inner().expect("just sent Ok"))
393403
}
394404

395-
#[allow(unused)]
405+
#[cfg(test)]
396406
pub(crate) fn abort(mut self) {
397407
self.send_error(crate::Error::new_body_write_aborted());
398408
}
@@ -406,6 +416,7 @@ impl Sender {
406416
}
407417
}
408418

419+
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
409420
impl fmt::Debug for Sender {
410421
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
411422
#[derive(Debug)]

src/common/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#[cfg(all(
2+
any(feature = "client", feature = "server"),
3+
any(feature = "http1", feature = "http2")
4+
))]
15
macro_rules! ready {
26
($e:expr) => {
37
match $e {
@@ -18,4 +22,5 @@ pub(crate) mod task;
1822
all(any(feature = "client", feature = "server"), feature = "http2"),
1923
))]
2024
pub(crate) mod time;
25+
#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
2126
pub(crate) mod watch;

src/common/watch.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type Value = usize;
1515

1616
pub(crate) const CLOSED: usize = 0;
1717

18-
#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))]
1918
pub(crate) fn channel(initial: Value) -> (Sender, Receiver) {
2019
debug_assert!(
2120
initial != CLOSED,

src/error.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ pub(super) enum Kind {
5050
/// A pending item was dropped before ever being processed.
5151
Canceled,
5252
/// Indicates a channel (client or body sender) is closed.
53+
#[cfg(any(
54+
all(feature = "http1", any(feature = "client", feature = "server")),
55+
all(feature = "http2", feature = "client")
56+
))]
5357
ChannelClosed,
5458
/// An `io::Error` that occurred while trying to read or write to a network stream.
5559
#[cfg(all(
@@ -121,6 +125,10 @@ pub(super) enum User {
121125
))]
122126
Body,
123127
/// The user aborted writing of the outgoing body.
128+
#[cfg(any(
129+
all(feature = "http1", any(feature = "client", feature = "server")),
130+
feature = "ffi"
131+
))]
124132
BodyWriteAborted,
125133
/// Error from future of user's Service.
126134
#[cfg(any(
@@ -192,6 +200,16 @@ impl Error {
192200

193201
/// Returns true if a sender's channel is closed.
194202
pub fn is_closed(&self) -> bool {
203+
#[cfg(not(any(
204+
all(feature = "http1", any(feature = "client", feature = "server")),
205+
all(feature = "http2", feature = "client")
206+
)))]
207+
return false;
208+
209+
#[cfg(any(
210+
all(feature = "http1", any(feature = "client", feature = "server")),
211+
all(feature = "http2", feature = "client")
212+
))]
195213
matches!(self.inner.kind, Kind::ChannelClosed)
196214
}
197215

@@ -202,6 +220,16 @@ impl Error {
202220

203221
/// Returns true if the body write was aborted.
204222
pub fn is_body_write_aborted(&self) -> bool {
223+
#[cfg(not(any(
224+
all(feature = "http1", any(feature = "client", feature = "server")),
225+
feature = "ffi"
226+
)))]
227+
return false;
228+
229+
#[cfg(any(
230+
all(feature = "http1", any(feature = "client", feature = "server")),
231+
feature = "ffi"
232+
))]
205233
matches!(self.inner.kind, Kind::User(User::BodyWriteAborted))
206234
}
207235

@@ -280,6 +308,10 @@ impl Error {
280308
Error::new(Kind::Io).with(cause)
281309
}
282310

311+
#[cfg(any(
312+
all(feature = "http1", any(feature = "client", feature = "server")),
313+
all(feature = "http2", feature = "client")
314+
))]
283315
pub(super) fn new_closed() -> Error {
284316
Error::new(Kind::ChannelClosed)
285317
}
@@ -300,6 +332,10 @@ impl Error {
300332
Error::new(Kind::BodyWrite).with(cause)
301333
}
302334

335+
#[cfg(any(
336+
all(feature = "http1", any(feature = "client", feature = "server")),
337+
feature = "ffi"
338+
))]
303339
pub(super) fn new_body_write_aborted() -> Error {
304340
Error::new(Kind::User(User::BodyWriteAborted))
305341
}
@@ -407,6 +443,10 @@ impl Error {
407443
Kind::IncompleteMessage => "connection closed before message completed",
408444
#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
409445
Kind::UnexpectedMessage => "received unexpected message from connection",
446+
#[cfg(any(
447+
all(feature = "http1", any(feature = "client", feature = "server")),
448+
all(feature = "http2", feature = "client")
449+
))]
410450
Kind::ChannelClosed => "channel closed",
411451
Kind::Canceled => "operation was canceled",
412452
#[cfg(all(feature = "http1", feature = "server"))]
@@ -436,6 +476,10 @@ impl Error {
436476
any(feature = "http1", feature = "http2")
437477
))]
438478
Kind::User(User::Body) => "error from user's Body stream",
479+
#[cfg(any(
480+
all(feature = "http1", any(feature = "client", feature = "server")),
481+
feature = "ffi"
482+
))]
439483
Kind::User(User::BodyWriteAborted) => "user body write aborted",
440484
#[cfg(any(
441485
all(any(feature = "client", feature = "server"), feature = "http1"),

0 commit comments

Comments
 (0)