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
use crate::types::{Error, Params, Value};
use crate::BoxFuture;
use futures::{Future, IntoFuture};
use std::fmt;
use std::sync::Arc;
pub trait Metadata: Clone + Send + 'static {}
impl Metadata for () {}
impl<T: Metadata> Metadata for Option<T> {}
impl<T: Metadata> Metadata for Box<T> {}
impl<T: Sync + Send + 'static> Metadata for Arc<T> {}
pub trait RpcMethodSimple: Send + Sync + 'static {
type Out: Future<Item = Value, Error = Error> + Send;
fn call(&self, params: Params) -> Self::Out;
}
pub trait RpcMethod<T: Metadata>: Send + Sync + 'static {
fn call(&self, params: Params, meta: T) -> BoxFuture<Value>;
}
pub trait RpcNotificationSimple: Send + Sync + 'static {
fn execute(&self, params: Params);
}
pub trait RpcNotification<T: Metadata>: Send + Sync + 'static {
fn execute(&self, params: Params, meta: T);
}
#[derive(Clone)]
pub enum RemoteProcedure<T: Metadata> {
Method(Arc<dyn RpcMethod<T>>),
Notification(Arc<dyn RpcNotification<T>>),
Alias(String),
}
impl<T: Metadata> fmt::Debug for RemoteProcedure<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
use self::RemoteProcedure::*;
match *self {
Method(..) => write!(fmt, "<method>"),
Notification(..) => write!(fmt, "<notification>"),
Alias(ref alias) => write!(fmt, "alias => {:?}", alias),
}
}
}
impl<F: Send + Sync + 'static, X: Send + 'static, I> RpcMethodSimple for F
where
F: Fn(Params) -> I,
X: Future<Item = Value, Error = Error>,
I: IntoFuture<Item = Value, Error = Error, Future = X>,
{
type Out = X;
fn call(&self, params: Params) -> Self::Out {
self(params).into_future()
}
}
impl<F: Send + Sync + 'static> RpcNotificationSimple for F
where
F: Fn(Params),
{
fn execute(&self, params: Params) {
self(params)
}
}
impl<F: Send + Sync + 'static, X: Send + 'static, T, I> RpcMethod<T> for F
where
T: Metadata,
F: Fn(Params, T) -> I,
I: IntoFuture<Item = Value, Error = Error, Future = X>,
X: Future<Item = Value, Error = Error>,
{
fn call(&self, params: Params, meta: T) -> BoxFuture<Value> {
Box::new(self(params, meta).into_future())
}
}
impl<F: Send + Sync + 'static, T> RpcNotification<T> for F
where
T: Metadata,
F: Fn(Params, T),
{
fn execute(&self, params: Params, meta: T) {
self(params, meta)
}
}