@@ -25,15 +25,16 @@ use common::exec::{Exec, H2Exec, NewSvcExec};
25
25
use common:: io:: Rewind ;
26
26
use error:: { Kind , Parse } ;
27
27
use proto;
28
- use service:: { NewService , Service } ;
28
+ use service:: Service ;
29
29
use upgrade:: Upgraded ;
30
30
31
+ pub ( super ) use self :: make_service:: MakeServiceRef ;
31
32
pub ( super ) use self :: spawn_all:: NoopWatcher ;
32
33
use self :: spawn_all:: NewSvcTask ;
33
34
pub ( super ) use self :: spawn_all:: Watcher ;
34
35
pub ( super ) use self :: upgrades:: UpgradeableConnection ;
35
36
36
- #[ cfg( feature = "runtime" ) ] pub use super :: tcp:: AddrIncoming ;
37
+ #[ cfg( feature = "runtime" ) ] pub use super :: tcp:: { AddrIncoming , AddrStream } ;
37
38
38
39
/// A lower-level configuration of the HTTP protocol.
39
40
///
@@ -69,13 +70,13 @@ enum ConnectionMode {
69
70
#[ derive( Debug ) ]
70
71
pub struct Serve < I , S , E = Exec > {
71
72
incoming : I ,
72
- new_service : S ,
73
+ make_service : S ,
73
74
protocol : Http < E > ,
74
75
}
75
76
76
77
/// A future building a new `Service` to a `Connection`.
77
78
///
78
- /// Wraps the future returned from `NewService ` into one that returns
79
+ /// Wraps the future returned from `MakeService ` into one that returns
79
80
/// a `Connection`.
80
81
#[ must_use = "futures do nothing unless polled" ]
81
82
#[ derive( Debug ) ]
@@ -349,12 +350,16 @@ impl<E> Http<E> {
349
350
///
350
351
/// This method will bind the `addr` provided with a new TCP listener ready
351
352
/// to accept connections. Each connection will be processed with the
352
- /// `new_service ` object provided, creating a new service per
353
+ /// `make_service ` object provided, creating a new service per
353
354
/// connection.
354
355
#[ cfg( feature = "runtime" ) ]
355
- pub fn serve_addr < S , Bd > ( & self , addr : & SocketAddr , new_service : S ) -> :: Result < Serve < AddrIncoming , S , E > >
356
+ pub fn serve_addr < S , Bd > ( & self , addr : & SocketAddr , make_service : S ) -> :: Result < Serve < AddrIncoming , S , E > >
356
357
where
357
- S : NewService < ReqBody =Body , ResBody =Bd > ,
358
+ S : MakeServiceRef <
359
+ AddrStream ,
360
+ ReqBody =Body ,
361
+ ResBody =Bd ,
362
+ > ,
358
363
S :: Error : Into < Box < :: std:: error:: Error + Send + Sync > > ,
359
364
Bd : Payload ,
360
365
E : H2Exec < <S :: Service as Service >:: Future , Bd > ,
@@ -363,19 +368,23 @@ impl<E> Http<E> {
363
368
if self . keep_alive {
364
369
incoming. set_keepalive ( Some ( Duration :: from_secs ( 90 ) ) ) ;
365
370
}
366
- Ok ( self . serve_incoming ( incoming, new_service ) )
371
+ Ok ( self . serve_incoming ( incoming, make_service ) )
367
372
}
368
373
369
374
/// Bind the provided `addr` with the `Handle` and return a [`Serve`](Serve)
370
375
///
371
376
/// This method will bind the `addr` provided with a new TCP listener ready
372
377
/// to accept connections. Each connection will be processed with the
373
- /// `new_service ` object provided, creating a new service per
378
+ /// `make_service ` object provided, creating a new service per
374
379
/// connection.
375
380
#[ cfg( feature = "runtime" ) ]
376
- pub fn serve_addr_handle < S , Bd > ( & self , addr : & SocketAddr , handle : & Handle , new_service : S ) -> :: Result < Serve < AddrIncoming , S , E > >
381
+ pub fn serve_addr_handle < S , Bd > ( & self , addr : & SocketAddr , handle : & Handle , make_service : S ) -> :: Result < Serve < AddrIncoming , S , E > >
377
382
where
378
- S : NewService < ReqBody =Body , ResBody =Bd > ,
383
+ S : MakeServiceRef <
384
+ AddrStream ,
385
+ ReqBody =Body ,
386
+ ResBody =Bd ,
387
+ > ,
379
388
S :: Error : Into < Box < :: std:: error:: Error + Send + Sync > > ,
380
389
Bd : Payload ,
381
390
E : H2Exec < <S :: Service as Service >:: Future , Bd > ,
@@ -384,23 +393,27 @@ impl<E> Http<E> {
384
393
if self . keep_alive {
385
394
incoming. set_keepalive ( Some ( Duration :: from_secs ( 90 ) ) ) ;
386
395
}
387
- Ok ( self . serve_incoming ( incoming, new_service ) )
396
+ Ok ( self . serve_incoming ( incoming, make_service ) )
388
397
}
389
398
390
- /// Bind the provided stream of incoming IO objects with a `NewService `.
391
- pub fn serve_incoming < I , S , Bd > ( & self , incoming : I , new_service : S ) -> Serve < I , S , E >
399
+ /// Bind the provided stream of incoming IO objects with a `MakeService `.
400
+ pub fn serve_incoming < I , S , Bd > ( & self , incoming : I , make_service : S ) -> Serve < I , S , E >
392
401
where
393
402
I : Stream ,
394
403
I :: Error : Into < Box < :: std:: error:: Error + Send + Sync > > ,
395
404
I :: Item : AsyncRead + AsyncWrite ,
396
- S : NewService < ReqBody =Body , ResBody =Bd > ,
405
+ S : MakeServiceRef <
406
+ I :: Item ,
407
+ ReqBody =Body ,
408
+ ResBody =Bd ,
409
+ > ,
397
410
S :: Error : Into < Box < :: std:: error:: Error + Send + Sync > > ,
398
411
Bd : Payload ,
399
412
E : H2Exec < <S :: Service as Service >:: Future , Bd > ,
400
413
{
401
414
Serve {
402
- incoming : incoming ,
403
- new_service : new_service ,
415
+ incoming,
416
+ make_service ,
404
417
protocol : self . clone ( ) ,
405
418
}
406
419
}
@@ -604,8 +617,9 @@ where
604
617
I : Stream ,
605
618
I :: Item : AsyncRead + AsyncWrite ,
606
619
I :: Error : Into < Box < :: std:: error:: Error + Send + Sync > > ,
607
- S : NewService < ReqBody =Body , ResBody =B > ,
608
- S :: Error : Into < Box < :: std:: error:: Error + Send + Sync > > ,
620
+ S : MakeServiceRef < I :: Item , ReqBody =Body , ResBody =B > ,
621
+ //S::Error2: Into<Box<::std::error::Error + Send + Sync>>,
622
+ //SME: Into<Box<::std::error::Error + Send + Sync>>,
609
623
B : Payload ,
610
624
E : H2Exec < <S :: Service as Service >:: Future , B > ,
611
625
{
@@ -614,7 +628,7 @@ where
614
628
615
629
fn poll ( & mut self ) -> Poll < Option < Self :: Item > , Self :: Error > {
616
630
if let Some ( io) = try_ready ! ( self . incoming. poll( ) . map_err( :: Error :: new_accept) ) {
617
- let new_fut = self . new_service . new_service ( ) ;
631
+ let new_fut = self . make_service . make_service_ref ( & io ) ;
618
632
Ok ( Async :: Ready ( Some ( Connecting {
619
633
future : new_fut,
620
634
io : Some ( io) ,
@@ -666,8 +680,11 @@ where
666
680
I : Stream ,
667
681
I :: Error : Into < Box < :: std:: error:: Error + Send + Sync > > ,
668
682
I :: Item : AsyncRead + AsyncWrite + Send + ' static ,
669
- S : NewService < ReqBody =Body , ResBody =B > ,
670
- S :: Error : Into < Box < :: std:: error:: Error + Send + Sync > > ,
683
+ S : MakeServiceRef <
684
+ I :: Item ,
685
+ ReqBody =Body ,
686
+ ResBody =B ,
687
+ > ,
671
688
B : Payload ,
672
689
E : H2Exec < <S :: Service as Service >:: Future , B > ,
673
690
{
@@ -873,3 +890,37 @@ mod upgrades {
873
890
}
874
891
}
875
892
893
+ pub ( crate ) mod make_service {
894
+ use std:: error:: Error as StdError ;
895
+
896
+ pub trait MakeServiceRef < Ctx > {
897
+ type Error : Into < Box < StdError + Send + Sync > > ;
898
+ type ReqBody : :: body:: Payload ;
899
+ type ResBody : :: body:: Payload ;
900
+ type Service : :: service:: Service < ReqBody =Self :: ReqBody , ResBody =Self :: ResBody , Error =Self :: Error > ;
901
+ type Future : :: futures:: Future < Item =Self :: Service > ;
902
+
903
+ fn make_service_ref ( & mut self , ctx : & Ctx ) -> Self :: Future ;
904
+ }
905
+
906
+ impl < T , Ctx , E , ME , S , F , IB , OB > MakeServiceRef < Ctx > for T
907
+ where
908
+ T : for < ' a > :: service:: MakeService < & ' a Ctx , Error =E , MakeError =ME , Service =S , Future =F , ReqBody =IB , ResBody =OB > ,
909
+ E : Into < Box < StdError + Send + Sync > > ,
910
+ ME : Into < Box < StdError + Send + Sync > > ,
911
+ S : :: service:: Service < ReqBody =IB , ResBody =OB , Error =E > ,
912
+ F : :: futures:: Future < Item =S , Error =ME > ,
913
+ IB : :: body:: Payload ,
914
+ OB : :: body:: Payload ,
915
+ {
916
+ type Error = E ;
917
+ type Service = S ;
918
+ type ReqBody = IB ;
919
+ type ResBody = OB ;
920
+ type Future = F ;
921
+
922
+ fn make_service_ref ( & mut self , ctx : & Ctx ) -> Self :: Future {
923
+ self . make_service ( ctx)
924
+ }
925
+ }
926
+ }
0 commit comments