forked from paritytech/jsonrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-trait.rs
43 lines (33 loc) · 872 Bytes
/
generic-trait.rs
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
use jsonrpc_core;
use jsonrpc_core::{BoxFuture, IoHandler, Result};
use jsonrpc_derive::rpc;
#[rpc]
pub trait Rpc<One, Two> {
/// Get One type.
#[rpc(name = "getOne")]
fn one(&self) -> Result<One>;
/// Adds two numbers and returns a result
#[rpc(name = "setTwo")]
fn set_two(&self, a: Two) -> Result<()>;
/// Performs asynchronous operation
#[rpc(name = "beFancy")]
fn call(&self, a: One) -> BoxFuture<Result<(One, Two)>>;
}
struct RpcImpl;
impl Rpc<u64, String> for RpcImpl {
fn one(&self) -> Result<u64> {
Ok(100)
}
fn set_two(&self, x: String) -> Result<()> {
println!("{}", x);
Ok(())
}
fn call(&self, num: u64) -> BoxFuture<Result<(u64, String)>> {
Box::pin(jsonrpc_core::futures::future::ready(Ok((num + 999, "hello".into()))))
}
}
fn main() {
let mut io = IoHandler::new();
let rpc = RpcImpl;
io.extend_with(rpc.to_delegate())
}