1
+ use std:: error:: Error as StdError ;
1
2
use std:: fmt;
2
3
#[ cfg( feature = "tcp" ) ]
3
4
use std:: net:: { SocketAddr , TcpListener as StdTcpListener } ;
4
5
#[ cfg( any( feature = "tcp" , feature = "http1" ) ) ]
5
6
use std:: time:: Duration ;
6
7
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" ) ) ]
8
14
use super :: tcp:: AddrIncoming ;
15
+ use crate :: body:: { Body , HttpBody } ;
9
16
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 } ;
10
24
11
- cfg_feature ! {
12
- #![ any( feature = "http1" , feature = "http2" ) ]
13
-
14
- use std:: error:: Error as StdError ;
25
+ use self :: new_svc:: NewSvcTask ;
15
26
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" ) ) ]
34
27
pin_project ! {
35
28
/// A listening HTTP server that accepts connections in both HTTP1 and HTTP2 by default.
36
29
///
@@ -46,17 +39,8 @@ pin_project! {
46
39
}
47
40
}
48
41
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
-
57
42
/// A builder for a [`Server`](Server).
58
43
#[ derive( Debug ) ]
59
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
60
44
#[ cfg_attr( docsrs, doc( cfg( any( feature = "http1" , feature = "http2" ) ) ) ) ]
61
45
pub struct Builder < I , E = Exec > {
62
46
incoming : I ,
@@ -65,7 +49,6 @@ pub struct Builder<I, E = Exec> {
65
49
66
50
// ===== impl Server =====
67
51
68
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
69
52
#[ cfg_attr( docsrs, doc( cfg( any( feature = "http1" , feature = "http2" ) ) ) ) ]
70
53
impl < I > Server < I , ( ) > {
71
54
/// Starts a [`Builder`](Builder) with the provided incoming stream.
@@ -77,47 +60,48 @@ impl<I> Server<I, ()> {
77
60
}
78
61
}
79
62
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
+ }
96
81
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
+ }
101
86
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)
106
90
}
107
91
}
108
92
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 ( )
117
102
}
118
103
}
119
104
120
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
121
105
#[ cfg_attr( docsrs, doc( cfg( any( feature = "http1" , feature = "http2" ) ) ) ) ]
122
106
impl < I , IO , IE , S , E , B > Server < I , S , E >
123
107
where
@@ -220,7 +204,6 @@ where
220
204
}
221
205
}
222
206
223
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
224
207
#[ cfg_attr( docsrs, doc( cfg( any( feature = "http1" , feature = "http2" ) ) ) ) ]
225
208
impl < I , IO , IE , S , B , E > Future for Server < I , S , E >
226
209
where
@@ -244,15 +227,13 @@ where
244
227
impl < I : fmt:: Debug , S : fmt:: Debug > fmt:: Debug for Server < I , S > {
245
228
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
246
229
let mut st = f. debug_struct ( "Server" ) ;
247
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
248
230
st. field ( "listener" , & self . incoming ) ;
249
231
st. finish ( )
250
232
}
251
233
}
252
234
253
235
// ===== impl Builder =====
254
236
255
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
256
237
#[ cfg_attr( docsrs, doc( cfg( any( feature = "http1" , feature = "http2" ) ) ) ) ]
257
238
impl < I , E > Builder < I , E > {
258
239
/// Start a new builder, wrapping an incoming stream and low-level options.
@@ -572,7 +553,11 @@ impl<I, E> Builder<I, E> {
572
553
}
573
554
}
574
555
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
+ ) ]
576
561
impl < E > Builder < AddrIncoming , E > {
577
562
/// Set whether TCP keepalive messages are enabled on accepted connections.
578
563
///
@@ -619,7 +604,6 @@ impl<E> Builder<AddrIncoming, E> {
619
604
// The `Server::with_graceful_shutdown` needs to keep track of all active
620
605
// connections, and signal that they start to shutdown when prompted, so
621
606
// it has a `GracefulWatcher` implementation to do that.
622
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
623
607
pub trait Watcher < I , S : HttpService < Body > , E > : Clone {
624
608
type Future : Future < Output = crate :: Result < ( ) > > ;
625
609
@@ -628,10 +612,8 @@ pub trait Watcher<I, S: HttpService<Body>, E>: Clone {
628
612
629
613
#[ allow( missing_debug_implementations) ]
630
614
#[ derive( Copy , Clone ) ]
631
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
632
615
pub struct NoopWatcher ;
633
616
634
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
635
617
impl < I , S , E > Watcher < I , S , E > for NoopWatcher
636
618
where
637
619
I : AsyncRead + AsyncWrite + Unpin + Send + ' static ,
@@ -647,7 +629,6 @@ where
647
629
}
648
630
}
649
631
650
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
651
632
// used by exec.rs
652
633
pub ( crate ) mod new_svc {
653
634
use std:: error:: Error as StdError ;
@@ -759,7 +740,6 @@ pub(crate) mod new_svc {
759
740
}
760
741
}
761
742
762
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
763
743
pin_project ! {
764
744
/// A future building a new `Service` to a `Connection`.
765
745
///
@@ -776,7 +756,6 @@ pin_project! {
776
756
}
777
757
}
778
758
779
- #[ cfg( any( feature = "http1" , feature = "http2" ) ) ]
780
759
impl < I , F , S , FE , E , B > Future for Connecting < I , F , E >
781
760
where
782
761
I : AsyncRead + AsyncWrite + Unpin ,
0 commit comments