1+ #![ deny( warnings, rust_2018_idioms) ]
2+
13mod timeout;
24
35pub use self :: timeout:: { DetectTimeout , DetectTimeoutError } ;
@@ -26,55 +28,70 @@ pub trait Detect: Clone + Send + Sync + 'static {
2628 ) -> Result < Option < Self :: Protocol > , Error > ;
2729}
2830
29- #[ derive( Copy , Clone ) ]
30- pub struct NewDetectService < N , D > {
31- new_accept : N ,
31+ #[ derive( Copy , Clone , Debug ) ]
32+ pub struct NewDetectService < D , N > {
33+ inner : N ,
3234 detect : D ,
3335 capacity : usize ,
3436}
3537
36- #[ derive( Copy , Clone ) ]
37- pub struct DetectService < N , D , T > {
38+ #[ derive( Copy , Clone , Debug ) ]
39+ pub struct DetectService < T , D , N > {
3840 target : T ,
39- new_accept : N ,
41+ inner : N ,
4042 detect : D ,
4143 capacity : usize ,
4244}
4345
44- // === impl NewDetectService ===
46+ const BUFFER_CAPACITY : usize = 1024 ;
4547
46- impl < N , D : Clone > NewDetectService < N , D > {
47- const BUFFER_CAPACITY : usize = 1024 ;
48+ // === impl NewDetectService ===
4849
49- pub fn new ( new_accept : N , detect : D ) -> Self {
50+ impl < D : Clone , N > NewDetectService < D , N > {
51+ pub fn new ( detect : D , inner : N ) -> Self {
5052 Self {
5153 detect,
52- new_accept ,
53- capacity : Self :: BUFFER_CAPACITY ,
54+ inner ,
55+ capacity : BUFFER_CAPACITY ,
5456 }
5557 }
5658
5759 pub fn layer ( detect : D ) -> impl layer:: Layer < N , Service = Self > + Clone {
58- layer:: mk ( move |new| Self :: new ( new, detect. clone ( ) ) )
60+ layer:: mk ( move |inner| Self :: new ( detect. clone ( ) , inner) )
61+ }
62+ }
63+
64+ impl < D : Clone , N > NewDetectService < DetectTimeout < D > , N > {
65+ pub fn timeout (
66+ timeout : time:: Duration ,
67+ detect : D ,
68+ ) -> impl layer:: Layer < N , Service = NewDetectService < DetectTimeout < D > , N > > + Clone {
69+ Self :: layer ( DetectTimeout :: new ( timeout, detect) )
70+ }
71+ }
72+
73+ impl < D : Clone , N : Clone , T > NewService < T > for NewDetectService < D , N > {
74+ type Service = DetectService < T , D , N > ;
75+
76+ fn new_service ( & mut self , target : T ) -> DetectService < T , D , N > {
77+ DetectService :: new ( target, self . detect . clone ( ) , self . inner . clone ( ) )
5978 }
6079}
6180
62- impl < N : Clone , D : Clone , T > NewService < T > for NewDetectService < N , D > {
63- type Service = DetectService < N , D , T > ;
81+ // === impl DetectService ===
6482
65- fn new_service ( & mut self , target : T ) -> DetectService < N , D , T > {
83+ impl < T , D : Clone , N : Clone > DetectService < T , D , N > {
84+ pub fn new ( target : T , detect : D , inner : N ) -> Self {
6685 DetectService {
6786 target,
68- new_accept : self . new_accept . clone ( ) ,
69- detect : self . detect . clone ( ) ,
70- capacity : self . capacity ,
87+ detect ,
88+ inner ,
89+ capacity : BUFFER_CAPACITY ,
7190 }
7291 }
7392}
7493
75- // === impl DetectService ===
76-
77- impl < N , S , D , T , I > tower:: Service < I > for DetectService < N , D , T >
94+ impl < S , T , D , N , I > tower:: Service < I > for DetectService < T , D , N >
7895where
7996 T : Clone + Send + ' static ,
8097 I : io:: AsyncRead + Send + Unpin + ' static ,
94111 }
95112
96113 fn call ( & mut self , mut io : I ) -> Self :: Future {
97- let mut new_accept = self . new_accept . clone ( ) ;
114+ let mut inner = self . inner . clone ( ) ;
98115 let mut buf = BytesMut :: with_capacity ( self . capacity ) ;
99116 let detect = self . detect . clone ( ) ;
100117 let target = self . target . clone ( ) ;
@@ -108,7 +125,7 @@ where
108125 "Detected"
109126 ) ;
110127
111- let mut accept = new_accept
128+ let mut accept = inner
112129 . new_service ( ( protocol, target) )
113130 . ready_oneshot ( )
114131 . err_into :: < Error > ( )
0 commit comments