1
1
//! Provides a Stub trait, implemented by types that can call remote services.
2
2
3
+ use std:: future:: Future ;
4
+
3
5
use crate :: {
4
6
client:: { Channel , RpcError } ,
5
7
context,
6
- server:: Serve ,
8
+ server:: { SendServe , Serve } ,
7
9
RequestName ,
8
10
} ;
9
11
@@ -15,7 +17,6 @@ mod mock;
15
17
16
18
/// A connection to a remote service.
17
19
/// Calls the service with requests of type `Req` and receives responses of type `Resp`.
18
- #[ allow( async_fn_in_trait) ]
19
20
pub trait Stub {
20
21
/// The service request type.
21
22
type Req : RequestName ;
@@ -24,8 +25,28 @@ pub trait Stub {
24
25
type Resp ;
25
26
26
27
/// Calls a remote service.
27
- async fn call ( & self , ctx : context:: Context , request : Self :: Req )
28
- -> Result < Self :: Resp , RpcError > ;
28
+ fn call (
29
+ & self ,
30
+ ctx : context:: Context ,
31
+ request : Self :: Req ,
32
+ ) -> impl Future < Output = Result < Self :: Resp , RpcError > > ;
33
+ }
34
+
35
+ /// A connection to a remote service.
36
+ /// Calls the service with requests of type `Req` and receives responses of type `Resp`.
37
+ pub trait SendStub : Send {
38
+ /// The service request type.
39
+ type Req : RequestName ;
40
+
41
+ /// The service response type.
42
+ type Resp ;
43
+
44
+ /// Calls a remote service.
45
+ fn call (
46
+ & self ,
47
+ ctx : context:: Context ,
48
+ request : Self :: Req ,
49
+ ) -> impl Future < Output = Result < Self :: Resp , RpcError > > + Send ;
29
50
}
30
51
31
52
impl < Req , Resp > Stub for Channel < Req , Resp >
40
61
}
41
62
}
42
63
64
+ impl < Req , Resp > SendStub for Channel < Req , Resp >
65
+ where
66
+ Req : RequestName + Send ,
67
+ Resp : Send ,
68
+ {
69
+ type Req = Req ;
70
+ type Resp = Resp ;
71
+
72
+ async fn call ( & self , ctx : context:: Context , request : Req ) -> Result < Self :: Resp , RpcError > {
73
+ Self :: call ( self , ctx, request) . await
74
+ }
75
+ }
76
+
43
77
impl < S > Stub for S
44
78
where
45
79
S : Serve + Clone ,
50
84
self . clone ( ) . serve ( ctx, req) . await . map_err ( RpcError :: Server )
51
85
}
52
86
}
87
+
88
+ impl < S > SendStub for S
89
+ where
90
+ S : SendServe + Clone + Sync ,
91
+ S :: Req : Send + Sync ,
92
+ S :: Resp : Send ,
93
+ {
94
+ type Req = S :: Req ;
95
+ type Resp = S :: Resp ;
96
+ async fn call ( & self , ctx : context:: Context , req : Self :: Req ) -> Result < Self :: Resp , RpcError > {
97
+ self . clone ( ) . serve ( ctx, req) . await . map_err ( RpcError :: Server )
98
+ }
99
+ }
0 commit comments