solana_jsonrpc_core/
calls.rs1use std::fmt;
2use std::sync::Arc;
3use types::{Params, Value, Error};
4use futures::{Future, IntoFuture};
5use BoxFuture;
6
7pub trait Metadata: Clone + Send + 'static {}
9impl Metadata for () {}
10impl<T: Metadata> Metadata for Option<T> {}
11impl<T: Metadata> Metadata for Box<T> {}
12impl<T: Sync + Send + 'static> Metadata for Arc<T> {}
13
14pub trait RpcMethodSimple: Send + Sync + 'static {
16 type Out: Future<Item = Value, Error = Error> + Send;
18 fn call(&self, params: Params) -> Self::Out;
20}
21
22pub trait RpcMethod<T: Metadata>: Send + Sync + 'static {
24 fn call(&self, params: Params, meta: T) -> BoxFuture<Value>;
26}
27
28pub trait RpcNotificationSimple: Send + Sync + 'static {
30 fn execute(&self, params: Params);
32}
33
34pub trait RpcNotification<T: Metadata>: Send + Sync + 'static {
36 fn execute(&self, params: Params, meta: T);
38}
39
40#[derive(Clone)]
42pub enum RemoteProcedure<T: Metadata> {
43 Method(Arc<RpcMethod<T>>),
45 Notification(Arc<RpcNotification<T>>),
47 Alias(String),
49}
50
51impl<T: Metadata> fmt::Debug for RemoteProcedure<T> {
52 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
53 use self::RemoteProcedure::*;
54 match *self {
55 Method(..) => write!(fmt, "<method>"),
56 Notification(..) => write!(fmt, "<notification>"),
57 Alias(ref alias) => write!(fmt, "alias => {:?}", alias)
58 }
59 }
60}
61
62impl<F: Send + Sync + 'static, X: Send + 'static, I> RpcMethodSimple for F where
63 F: Fn(Params) -> I,
64 X: Future<Item = Value, Error = Error>,
65 I: IntoFuture<Item = Value, Error = Error, Future = X>,
66{
67 type Out = X;
68 fn call(&self, params: Params) -> Self::Out {
69 self(params).into_future()
70 }
71}
72
73impl<F: Send + Sync + 'static> RpcNotificationSimple for F where
74 F: Fn(Params),
75{
76 fn execute(&self, params: Params) {
77 self(params)
78 }
79}
80
81impl<F: Send + Sync + 'static, X: Send + 'static, T, I> RpcMethod<T> for F where
82 T: Metadata,
83 F: Fn(Params, T) -> I,
84 I: IntoFuture<Item = Value, Error = Error, Future = X>,
85 X: Future<Item = Value, Error = Error>,
86{
87 fn call(&self, params: Params, meta: T) -> BoxFuture<Value> {
88 Box::new(self(params, meta).into_future())
89 }
90}
91
92impl<F: Send + Sync + 'static, T> RpcNotification<T> for F where
93 T: Metadata,
94 F: Fn(Params, T),
95{
96 fn execute(&self, params: Params, meta: T) {
97 self(params, meta)
98 }
99}