Skip to content

Commit dd08d9c

Browse files
SabrinaJewsonseanmonstar
authored andcommitted
refactor(server): simplify server cfgs
`server.rs` is currently littered with `cfg`s for `http1` or `http2`, since the majority of server behaviour is only available with either one of those feature flags active. This is hard to maintain and confusing to read, so this commit extracts the two implementations into their own files, since there is very little benefit in sharing code between the two.
1 parent 0fec1c8 commit dd08d9c

File tree

4 files changed

+80
-77
lines changed

4 files changed

+80
-77
lines changed

src/server/conn.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
not(all(feature = "http1", feature = "http2"))
4949
))]
5050
use std::marker::PhantomData;
51+
#[cfg(all(any(feature = "http1", feature = "http2"), feature = "runtime"))]
5152
use std::time::Duration;
5253

5354
#[cfg(feature = "http2")]

src/server/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@
149149
150150
pub mod accept;
151151
pub mod conn;
152-
pub(crate) mod server;
153152
#[cfg(feature = "tcp")]
154153
mod tcp;
155154

@@ -158,7 +157,15 @@ pub use self::server::Server;
158157
cfg_feature! {
159158
#![any(feature = "http1", feature = "http2")]
160159

160+
pub(crate) mod server;
161161
pub use self::server::Builder;
162162

163163
mod shutdown;
164164
}
165+
166+
cfg_feature! {
167+
#![not(any(feature = "http1", feature = "http2"))]
168+
169+
mod server_stub;
170+
use server_stub as server;
171+
}

src/server/server.rs

Lines changed: 55 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
1+
use std::error::Error as StdError;
12
use std::fmt;
23
#[cfg(feature = "tcp")]
34
use std::net::{SocketAddr, TcpListener as StdTcpListener};
45
#[cfg(any(feature = "tcp", feature = "http1"))]
56
use std::time::Duration;
67

7-
#[cfg(all(feature = "tcp", any(feature = "http1", feature = "http2")))]
8+
use pin_project_lite::pin_project;
9+
use tokio::io::{AsyncRead, AsyncWrite};
10+
use tracing::trace;
11+
12+
use super::accept::Accept;
13+
#[cfg(all(feature = "tcp"))]
814
use super::tcp::AddrIncoming;
15+
use crate::body::{Body, HttpBody};
916
use crate::common::exec::Exec;
17+
use crate::common::exec::{ConnStreamExec, NewSvcExec};
18+
use crate::common::{task, Future, Pin, Poll, Unpin};
19+
// Renamed `Http` as `Http_` for now so that people upgrading don't see an
20+
// error that `hyper::server::Http` is private...
21+
use super::conn::{Connection, Http as Http_, UpgradeableConnection};
22+
use super::shutdown::{Graceful, GracefulWatcher};
23+
use crate::service::{HttpService, MakeServiceRef};
1024

11-
cfg_feature! {
12-
#![any(feature = "http1", feature = "http2")]
13-
14-
use std::error::Error as StdError;
25+
use self::new_svc::NewSvcTask;
1526

16-
use pin_project_lite::pin_project;
17-
use tokio::io::{AsyncRead, AsyncWrite};
18-
use tracing::trace;
19-
20-
use super::accept::Accept;
21-
use crate::body::{Body, HttpBody};
22-
use crate::common::{task, Future, Pin, Poll, Unpin};
23-
use crate::common::exec::{ConnStreamExec, NewSvcExec};
24-
// Renamed `Http` as `Http_` for now so that people upgrading don't see an
25-
// error that `hyper::server::Http` is private...
26-
use super::conn::{Connection, Http as Http_, UpgradeableConnection};
27-
use super::shutdown::{Graceful, GracefulWatcher};
28-
use crate::service::{HttpService, MakeServiceRef};
29-
30-
use self::new_svc::NewSvcTask;
31-
}
32-
33-
#[cfg(any(feature = "http1", feature = "http2"))]
3427
pin_project! {
3528
/// A listening HTTP server that accepts connections in both HTTP1 and HTTP2 by default.
3629
///
@@ -46,17 +39,8 @@ pin_project! {
4639
}
4740
}
4841

49-
/// A listening HTTP server that accepts connections in both HTTP1 and HTTP2 by default.
50-
///
51-
/// Needs at least one of the `http1` and `http2` features to be activated to actually be useful.
52-
#[cfg(not(any(feature = "http1", feature = "http2")))]
53-
pub struct Server<I, S, E = Exec> {
54-
_marker: std::marker::PhantomData<(I, S, E)>,
55-
}
56-
5742
/// A builder for a [`Server`](Server).
5843
#[derive(Debug)]
59-
#[cfg(any(feature = "http1", feature = "http2"))]
6044
#[cfg_attr(docsrs, doc(cfg(any(feature = "http1", feature = "http2"))))]
6145
pub struct Builder<I, E = Exec> {
6246
incoming: I,
@@ -65,7 +49,6 @@ pub struct Builder<I, E = Exec> {
6549

6650
// ===== impl Server =====
6751

68-
#[cfg(any(feature = "http1", feature = "http2"))]
6952
#[cfg_attr(docsrs, doc(cfg(any(feature = "http1", feature = "http2"))))]
7053
impl<I> Server<I, ()> {
7154
/// Starts a [`Builder`](Builder) with the provided incoming stream.
@@ -77,47 +60,48 @@ impl<I> Server<I, ()> {
7760
}
7861
}
7962

80-
cfg_feature! {
81-
#![all(feature = "tcp", any(feature = "http1", feature = "http2"))]
82-
83-
impl Server<AddrIncoming, ()> {
84-
/// Binds to the provided address, and returns a [`Builder`](Builder).
85-
///
86-
/// # Panics
87-
///
88-
/// This method will panic if binding to the address fails. For a method
89-
/// to bind to an address and return a `Result`, see `Server::try_bind`.
90-
pub fn bind(addr: &SocketAddr) -> Builder<AddrIncoming> {
91-
let incoming = AddrIncoming::new(addr).unwrap_or_else(|e| {
92-
panic!("error binding to {}: {}", addr, e);
93-
});
94-
Server::builder(incoming)
95-
}
63+
#[cfg(feature = "tcp")]
64+
#[cfg_attr(
65+
docsrs,
66+
doc(cfg(all(feature = "tcp", any(feature = "http1", feature = "http2"))))
67+
)]
68+
impl Server<AddrIncoming, ()> {
69+
/// Binds to the provided address, and returns a [`Builder`](Builder).
70+
///
71+
/// # Panics
72+
///
73+
/// This method will panic if binding to the address fails. For a method
74+
/// to bind to an address and return a `Result`, see `Server::try_bind`.
75+
pub fn bind(addr: &SocketAddr) -> Builder<AddrIncoming> {
76+
let incoming = AddrIncoming::new(addr).unwrap_or_else(|e| {
77+
panic!("error binding to {}: {}", addr, e);
78+
});
79+
Server::builder(incoming)
80+
}
9681

97-
/// Tries to bind to the provided address, and returns a [`Builder`](Builder).
98-
pub fn try_bind(addr: &SocketAddr) -> crate::Result<Builder<AddrIncoming>> {
99-
AddrIncoming::new(addr).map(Server::builder)
100-
}
82+
/// Tries to bind to the provided address, and returns a [`Builder`](Builder).
83+
pub fn try_bind(addr: &SocketAddr) -> crate::Result<Builder<AddrIncoming>> {
84+
AddrIncoming::new(addr).map(Server::builder)
85+
}
10186

102-
/// Create a new instance from a `std::net::TcpListener` instance.
103-
pub fn from_tcp(listener: StdTcpListener) -> Result<Builder<AddrIncoming>, crate::Error> {
104-
AddrIncoming::from_std(listener).map(Server::builder)
105-
}
87+
/// Create a new instance from a `std::net::TcpListener` instance.
88+
pub fn from_tcp(listener: StdTcpListener) -> Result<Builder<AddrIncoming>, crate::Error> {
89+
AddrIncoming::from_std(listener).map(Server::builder)
10690
}
10791
}
10892

109-
cfg_feature! {
110-
#![all(feature = "tcp", any(feature = "http1", feature = "http2"))]
111-
112-
impl<S, E> Server<AddrIncoming, S, E> {
113-
/// Returns the local address that this server is bound to.
114-
pub fn local_addr(&self) -> SocketAddr {
115-
self.incoming.local_addr()
116-
}
93+
#[cfg(feature = "tcp")]
94+
#[cfg_attr(
95+
docsrs,
96+
doc(cfg(all(feature = "tcp", any(feature = "http1", feature = "http2"))))
97+
)]
98+
impl<S, E> Server<AddrIncoming, S, E> {
99+
/// Returns the local address that this server is bound to.
100+
pub fn local_addr(&self) -> SocketAddr {
101+
self.incoming.local_addr()
117102
}
118103
}
119104

120-
#[cfg(any(feature = "http1", feature = "http2"))]
121105
#[cfg_attr(docsrs, doc(cfg(any(feature = "http1", feature = "http2"))))]
122106
impl<I, IO, IE, S, E, B> Server<I, S, E>
123107
where
@@ -220,7 +204,6 @@ where
220204
}
221205
}
222206

223-
#[cfg(any(feature = "http1", feature = "http2"))]
224207
#[cfg_attr(docsrs, doc(cfg(any(feature = "http1", feature = "http2"))))]
225208
impl<I, IO, IE, S, B, E> Future for Server<I, S, E>
226209
where
@@ -244,15 +227,13 @@ where
244227
impl<I: fmt::Debug, S: fmt::Debug> fmt::Debug for Server<I, S> {
245228
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246229
let mut st = f.debug_struct("Server");
247-
#[cfg(any(feature = "http1", feature = "http2"))]
248230
st.field("listener", &self.incoming);
249231
st.finish()
250232
}
251233
}
252234

253235
// ===== impl Builder =====
254236

255-
#[cfg(any(feature = "http1", feature = "http2"))]
256237
#[cfg_attr(docsrs, doc(cfg(any(feature = "http1", feature = "http2"))))]
257238
impl<I, E> Builder<I, E> {
258239
/// Start a new builder, wrapping an incoming stream and low-level options.
@@ -572,7 +553,11 @@ impl<I, E> Builder<I, E> {
572553
}
573554
}
574555

575-
#[cfg(all(feature = "tcp", any(feature = "http1", feature = "http2")))]
556+
#[cfg(feature = "tcp")]
557+
#[cfg_attr(
558+
docsrs,
559+
doc(cfg(all(feature = "tcp", any(feature = "http1", feature = "http2"))))
560+
)]
576561
impl<E> Builder<AddrIncoming, E> {
577562
/// Set whether TCP keepalive messages are enabled on accepted connections.
578563
///
@@ -619,7 +604,6 @@ impl<E> Builder<AddrIncoming, E> {
619604
// The `Server::with_graceful_shutdown` needs to keep track of all active
620605
// connections, and signal that they start to shutdown when prompted, so
621606
// it has a `GracefulWatcher` implementation to do that.
622-
#[cfg(any(feature = "http1", feature = "http2"))]
623607
pub trait Watcher<I, S: HttpService<Body>, E>: Clone {
624608
type Future: Future<Output = crate::Result<()>>;
625609

@@ -628,10 +612,8 @@ pub trait Watcher<I, S: HttpService<Body>, E>: Clone {
628612

629613
#[allow(missing_debug_implementations)]
630614
#[derive(Copy, Clone)]
631-
#[cfg(any(feature = "http1", feature = "http2"))]
632615
pub struct NoopWatcher;
633616

634-
#[cfg(any(feature = "http1", feature = "http2"))]
635617
impl<I, S, E> Watcher<I, S, E> for NoopWatcher
636618
where
637619
I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
@@ -647,7 +629,6 @@ where
647629
}
648630
}
649631

650-
#[cfg(any(feature = "http1", feature = "http2"))]
651632
// used by exec.rs
652633
pub(crate) mod new_svc {
653634
use std::error::Error as StdError;
@@ -759,7 +740,6 @@ pub(crate) mod new_svc {
759740
}
760741
}
761742

762-
#[cfg(any(feature = "http1", feature = "http2"))]
763743
pin_project! {
764744
/// A future building a new `Service` to a `Connection`.
765745
///
@@ -776,7 +756,6 @@ pin_project! {
776756
}
777757
}
778758

779-
#[cfg(any(feature = "http1", feature = "http2"))]
780759
impl<I, F, S, FE, E, B> Future for Connecting<I, F, E>
781760
where
782761
I: AsyncRead + AsyncWrite + Unpin,

src/server/server_stub.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::fmt;
2+
3+
use crate::common::exec::Exec;
4+
5+
/// A listening HTTP server that accepts connections in both HTTP1 and HTTP2 by default.
6+
///
7+
/// Needs at least one of the `http1` and `http2` features to be activated to actually be useful.
8+
pub struct Server<I, S, E = Exec> {
9+
_marker: std::marker::PhantomData<(I, S, E)>,
10+
}
11+
12+
impl<I: fmt::Debug, S: fmt::Debug> fmt::Debug for Server<I, S> {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
f.debug_struct("Server").finish()
15+
}
16+
}

0 commit comments

Comments
 (0)