1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use std::fmt::Debug;
use faststr::FastStr;
pub use metainfo::MetaInfo;
use metainfo::TypeMap;
use super::net::Address;
#[macro_export]
macro_rules! newtype_impl_context {
($t:ident, $cf:ident, $inner: tt) => {
impl $crate::context::Context for $t {
type Config = $cf;
#[inline]
fn rpc_info(&self) -> &$crate::context::RpcInfo<Self::Config> {
self.$inner.rpc_info()
}
#[inline]
fn rpc_info_mut(&mut self) -> &mut $crate::context::RpcInfo<Self::Config> {
self.$inner.rpc_info_mut()
}
#[inline]
fn extensions_mut(&mut self) -> &mut $crate::context::Extensions {
self.$inner.extensions_mut()
}
#[inline]
fn extensions(&self) -> &$crate::context::Extensions {
self.$inner.extensions()
}
}
};
}
pub struct RpcCx<I, Config> {
pub rpc_info: RpcInfo<Config>,
pub inner: I,
pub extensions: Extensions,
}
#[derive(Default)]
pub struct Extensions(TypeMap);
impl std::ops::Deref for Extensions {
type Target = TypeMap;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for Extensions {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub trait Context {
type Config: Send + Debug;
fn rpc_info(&self) -> &RpcInfo<Self::Config>;
fn rpc_info_mut(&mut self) -> &mut RpcInfo<Self::Config>;
fn extensions(&self) -> &Extensions;
fn extensions_mut(&mut self) -> &mut Extensions;
}
impl<I, Config> Context for RpcCx<I, Config>
where
Config: Send + Debug,
{
type Config = Config;
#[inline]
fn rpc_info(&self) -> &RpcInfo<Self::Config> {
&self.rpc_info
}
#[inline]
fn rpc_info_mut(&mut self) -> &mut RpcInfo<Self::Config> {
&mut self.rpc_info
}
#[inline]
fn extensions(&self) -> &Extensions {
&self.extensions
}
#[inline]
fn extensions_mut(&mut self) -> &mut Extensions {
&mut self.extensions
}
}
impl<I, Config> std::ops::Deref for RpcCx<I, Config> {
type Target = I;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<I, Config> std::ops::DerefMut for RpcCx<I, Config> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<I, Config> RpcCx<I, Config> {
pub fn new(ri: RpcInfo<Config>, inner: I) -> Self {
Self {
rpc_info: ri,
inner,
extensions: Default::default(),
}
}
}
#[derive(Debug)]
pub struct Endpoint {
pub service_name: FastStr,
pub address: Option<Address>,
pub tags: TypeMap,
}
impl Endpoint {
#[inline]
pub fn new(service_name: FastStr) -> Self {
Self {
service_name,
address: None,
tags: TypeMap::default(),
}
}
#[inline]
pub fn service_name_ref(&self) -> &str {
&self.service_name
}
#[inline]
pub fn service_name(&self) -> FastStr {
self.service_name.clone()
}
#[inline]
pub fn insert<T: Send + Sync + 'static>(&mut self, val: T) {
self.tags.insert(val);
}
#[inline]
pub fn contains<T: 'static>(&self) -> bool {
self.tags.contains::<T>()
}
#[inline]
pub fn get<T: 'static>(&self) -> Option<&T> {
self.tags.get::<T>()
}
#[inline]
pub fn set_address(&mut self, address: Address) {
self.address = Some(address)
}
#[inline]
pub fn address(&self) -> Option<Address> {
self.address.clone()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Role {
Client,
Server,
}
#[derive(Debug)]
pub struct RpcInfo<Config> {
pub role: Role,
pub caller: Option<Endpoint>,
pub callee: Option<Endpoint>,
pub method: Option<FastStr>,
pub config: Option<Config>,
}
impl<Config> RpcInfo<Config> {
pub fn with_role(role: Role) -> RpcInfo<Config> {
RpcInfo {
role,
caller: None,
callee: None,
method: None,
config: None,
}
}
pub fn new(
role: Role,
method: FastStr,
caller: Endpoint,
callee: Endpoint,
config: Config,
) -> Self {
RpcInfo {
role,
caller: Some(caller),
callee: Some(callee),
method: Some(method),
config: Some(config),
}
}
#[inline]
pub fn role(&self) -> Role {
self.role
}
#[inline]
pub fn method(&self) -> Option<&FastStr> {
self.method.as_ref()
}
#[inline]
pub fn method_mut(&mut self) -> Option<&mut FastStr> {
self.method.as_mut()
}
#[inline]
pub fn caller(&self) -> Option<&Endpoint> {
self.caller.as_ref()
}
#[inline]
pub fn caller_mut(&mut self) -> Option<&mut Endpoint> {
self.caller.as_mut()
}
#[inline]
pub fn callee(&self) -> Option<&Endpoint> {
self.callee.as_ref()
}
#[inline]
pub fn callee_mut(&mut self) -> Option<&mut Endpoint> {
self.callee.as_mut()
}
#[inline]
pub fn config(&self) -> Option<&Config> {
self.config.as_ref()
}
#[inline]
pub fn config_mut(&mut self) -> Option<&mut Config> {
self.config.as_mut()
}
}