11use crate :: FutureService ;
2+ use futures:: future;
3+ use linkerd2_error:: Never ;
4+ use std:: task:: { Context , Poll } ;
25use tower:: util:: { Oneshot , ServiceExt } ;
36
47/// Immediately and infalliby creates (usually) a Service.
58pub trait NewService < T > {
69 type Service ;
710
811 fn new_service ( & self , target : T ) -> Self :: Service ;
12+
13+ fn into_make_service ( self ) -> IntoMakeService < Self >
14+ where
15+ Self : Sized ,
16+ {
17+ IntoMakeService { new_service : self }
18+ }
919}
1020
1121/// A Layer that modifies inner `MakeService`s to be exposd as a `NewService`.
@@ -18,6 +28,12 @@ pub struct FromMakeService<S> {
1828 make_service : S ,
1929}
2030
31+ /// Modifies inner `MakeService`s to be exposd as a `NewService`.
32+ #[ derive( Clone , Copy , Debug ) ]
33+ pub struct IntoMakeService < N > {
34+ new_service : N ,
35+ }
36+
2137// === impl NewService ===
2238
2339impl < F , T , S > NewService < T > for F
5369 FutureService :: new ( self . make_service . clone ( ) . oneshot ( target) )
5470 }
5571}
72+
73+ impl < T , N : NewService < T > > tower:: Service < T > for IntoMakeService < N > {
74+ type Response = N :: Service ;
75+ type Error = Never ;
76+ type Future = future:: Ready < Result < N :: Service , Never > > ;
77+
78+ fn poll_ready ( & mut self , _: & mut Context < ' _ > ) -> Poll < Result < ( ) , Never > > {
79+ Poll :: Ready ( Ok ( ( ) ) )
80+ }
81+
82+ fn call ( & mut self , target : T ) -> Self :: Future {
83+ future:: ok ( self . new_service . new_service ( target) )
84+ }
85+ }
0 commit comments