6
6
7
7
//! Provides a server that concurrently handles many connections sending multiplexed requests.
8
8
9
+ use crate :: client:: stub:: Stub ;
10
+ use crate :: client:: RpcError ;
9
11
use crate :: {
10
12
cancellations:: { cancellations, CanceledRequests , RequestCancellation } ,
11
13
context:: { self , SpanExt } ,
@@ -66,6 +68,27 @@ impl Config {
66
68
}
67
69
}
68
70
71
+ /// A [`Stub`] implementation that simply warps a `Serve`.
72
+ pub struct ServeStub < S > {
73
+ serve : S ,
74
+ }
75
+
76
+ impl < S > Stub for ServeStub < S >
77
+ where
78
+ S : Serve + Clone ,
79
+ {
80
+ type Req = <S as Serve >:: Req ;
81
+ type Resp = <S as Serve >:: Resp ;
82
+
83
+ async fn call ( & self , ctx : context:: Context , req : Self :: Req ) -> Result < Self :: Resp , RpcError > {
84
+ self . serve
85
+ . clone ( )
86
+ . serve ( ctx, req)
87
+ . await
88
+ . map_err ( RpcError :: Server )
89
+ }
90
+ }
91
+
69
92
/// Equivalent to a `FnOnce(Req) -> impl Future<Output = Resp>`.
70
93
#[ allow( async_fn_in_trait) ]
71
94
pub trait Serve {
@@ -77,6 +100,16 @@ pub trait Serve {
77
100
78
101
/// Responds to a single request.
79
102
async fn serve ( self , ctx : context:: Context , req : Self :: Req ) -> Result < Self :: Resp , ServerError > ;
103
+
104
+ /// Wrap this `Serve` in a type that implements [`Stub`].
105
+ async fn into_stub ( self ) -> ServeStub < Self >
106
+ where
107
+ Self : Clone ,
108
+ {
109
+ ServeStub {
110
+ serve : self . clone ( ) ,
111
+ }
112
+ }
80
113
}
81
114
82
115
/// A Serve wrapper around a Fn.
0 commit comments