1+ use std:: error:: Error as StdError ;
12use std:: fmt;
23#[ cfg( feature = "tcp" ) ]
34use std:: net:: { SocketAddr , TcpListener as StdTcpListener } ;
45#[ cfg( any( feature = "tcp" , feature = "http1" ) ) ]
56use 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" ) ) ]
814use super :: tcp:: AddrIncoming ;
15+ use crate :: body:: { Body , HttpBody } ;
916use 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" ) ) ]
3427pin_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" ) ) ) ) ]
6145pub 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" ) ) ) ) ]
7053impl < 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" ) ) ) ) ]
122106impl < I , IO , IE , S , E , B > Server < I , S , E >
123107where
@@ -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" ) ) ) ) ]
225208impl < I , IO , IE , S , B , E > Future for Server < I , S , E >
226209where
@@ -244,15 +227,13 @@ where
244227impl < 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" ) ) ) ) ]
257238impl < 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+ ) ]
576561impl < 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" ) ) ]
623607pub 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" ) ) ]
632615pub struct NoopWatcher ;
633616
634- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
635617impl < I , S , E > Watcher < I , S , E > for NoopWatcher
636618where
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
652633pub ( 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" ) ) ]
763743pin_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" ) ) ]
780759impl < I , F , S , FE , E , B > Future for Connecting < I , F , E >
781760where
782761 I : AsyncRead + AsyncWrite + Unpin ,
0 commit comments