solana_jsonrpc_core/
calls.rs

1use std::fmt;
2use std::sync::Arc;
3use types::{Params, Value, Error};
4use futures::{Future, IntoFuture};
5use BoxFuture;
6
7/// Metadata trait
8pub 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
14/// Asynchronous Method
15pub trait RpcMethodSimple: Send + Sync + 'static {
16	/// Output future
17	type Out: Future<Item = Value, Error = Error> + Send;
18	/// Call method
19	fn call(&self, params: Params) -> Self::Out;
20}
21
22/// Asynchronous Method with Metadata
23pub trait RpcMethod<T: Metadata>: Send + Sync + 'static {
24	/// Call method
25	fn call(&self, params: Params, meta: T) -> BoxFuture<Value>;
26}
27
28/// Notification
29pub trait RpcNotificationSimple: Send + Sync + 'static {
30	/// Execute notification
31	fn execute(&self, params: Params);
32}
33
34/// Notification with Metadata
35pub trait RpcNotification<T: Metadata>: Send + Sync + 'static {
36	/// Execute notification
37	fn execute(&self, params: Params, meta: T);
38}
39
40/// Possible Remote Procedures with Metadata
41#[derive(Clone)]
42pub enum RemoteProcedure<T: Metadata> {
43	/// A method call
44	Method(Arc<RpcMethod<T>>),
45	/// A notification
46	Notification(Arc<RpcNotification<T>>),
47	/// An alias to other method,
48	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}