forked from paritytech/jsonrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic-trait-bounds.rs
139 lines (110 loc) · 2.65 KB
/
generic-trait-bounds.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
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
extern crate serde;
extern crate jsonrpc_core;
#[macro_use]
extern crate jsonrpc_macros;
use serde::{Serialize, de::DeserializeOwned};
use jsonrpc_core::{IoHandler, Error, Result};
use jsonrpc_core::futures::future::{self, FutureResult};
// Two only requires DeserializeOwned
build_rpc_trait! {
pub trait Rpc<Zero, One> where
Two: DeserializeOwned,
Three: Serialize,
{
/// Get Zero type.
#[rpc(name = "getZero")]
fn zero(&self) -> Result<Zero>;
/// 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, Two) -> Result<()>;
/// Adds two numbers and returns a result
#[rpc(name = "getThree")]
fn three(&self) -> Result<Three>;
/// Performs asynchronous operation
#[rpc(name = "beFancy")]
fn call(&self, One) -> FutureResult<(One, u64), Error>;
}
}
build_rpc_trait! {
pub trait Rpc2<> where
Two: DeserializeOwned,
{
/// Adds two numbers and returns a result
#[rpc(name = "setTwo")]
fn set_two(&self, Two) -> Result<()>;
}
}
build_rpc_trait! {
pub trait Rpc3<Zero, One> where
Two: DeserializeOwned,
Three: Serialize,
{
type Metadata;
/// Get Zero type.
#[rpc(name = "getZero")]
fn zero(&self) -> Result<Zero>;
/// 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, Two) -> Result<()>;
/// Adds two numbers and returns a result
#[rpc(name = "getThree")]
fn three(&self) -> Result<Three>;
/// Performs asynchronous operation
#[rpc(name = "beFancy")]
fn call(&self, One) -> FutureResult<(One, u64), Error>;
}
}
struct RpcImpl;
impl Rpc<u8, u64, String, u32> for RpcImpl {
fn zero(&self) -> Result<u8> {
Ok(0)
}
fn one(&self) -> Result<u64> {
Ok(1)
}
fn set_two(&self, x: String) -> Result<()> {
println!("{}", x);
Ok(())
}
fn three(&self) -> Result<u32> {
Ok(3)
}
fn call(&self, num: u64) -> FutureResult<(u64, u64), Error> {
::future::finished((num + 999, num))
}
}
impl Rpc2<String> for RpcImpl {
fn set_two(&self, _: String) -> Result<()> {
unimplemented!()
}
}
impl Rpc3<u8, u64, String, u32> for RpcImpl {
type Metadata = ();
fn zero(&self) -> Result<u8> {
Ok(0)
}
fn one(&self) -> Result<u64> {
Ok(1)
}
fn set_two(&self, x: String) -> Result<()> {
println!("{}", x);
Ok(())
}
fn three(&self) -> Result<u32> {
Ok(3)
}
fn call(&self, num: u64) -> FutureResult<(u64, u64), Error> {
::future::finished((num + 999, num))
}
}
fn main() {
let mut io = IoHandler::new();
io.extend_with(Rpc::to_delegate(RpcImpl));
io.extend_with(Rpc2::to_delegate(RpcImpl));
}