Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Commit c0cb883

Browse files
tomusdrwCriesofCarrots
authored andcommitted
Allow one-way bounds on trait generic parameters. (paritytech#336)
* Allow one-way bounds on trait generic parameters. * Remove unused macro.
1 parent 6d73801 commit c0cb883

File tree

2 files changed

+117
-6
lines changed

2 files changed

+117
-6
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
extern crate serde;
2+
extern crate jsonrpc_core;
3+
#[macro_use]
4+
extern crate jsonrpc_macros;
5+
6+
use serde::de::DeserializeOwned;
7+
use jsonrpc_core::{IoHandler, Error, Result};
8+
use jsonrpc_core::futures::future::{self, FutureResult};
9+
10+
// Two only requires DeserializeOwned
11+
build_rpc_trait! {
12+
pub trait Rpc<One> where
13+
Two: DeserializeOwned,
14+
{
15+
/// Get One type.
16+
#[rpc(name = "getOne")]
17+
fn one(&self) -> Result<One>;
18+
19+
/// Adds two numbers and returns a result
20+
#[rpc(name = "setTwo")]
21+
fn set_two(&self, Two) -> Result<()>;
22+
23+
/// Performs asynchronous operation
24+
#[rpc(name = "beFancy")]
25+
fn call(&self, One) -> FutureResult<(One, u64), Error>;
26+
}
27+
}
28+
29+
build_rpc_trait! {
30+
pub trait Rpc2<> where
31+
Two: DeserializeOwned,
32+
{
33+
/// Adds two numbers and returns a result
34+
#[rpc(name = "setTwo")]
35+
fn set_two(&self, Two) -> Result<()>;
36+
}
37+
}
38+
39+
struct RpcImpl;
40+
41+
impl Rpc<u64, String> for RpcImpl {
42+
fn one(&self) -> Result<u64> {
43+
Ok(100)
44+
}
45+
46+
fn set_two(&self, x: String) -> Result<()> {
47+
println!("{}", x);
48+
Ok(())
49+
}
50+
51+
fn call(&self, num: u64) -> FutureResult<(u64, u64), Error> {
52+
::future::finished((num + 999, num))
53+
}
54+
}
55+
56+
impl Rpc2<String> for RpcImpl {
57+
fn set_two(&self, _: String) -> Result<()> {
58+
unimplemented!()
59+
}
60+
}
61+
62+
63+
fn main() {
64+
let mut io = IoHandler::new();
65+
66+
io.extend_with(Rpc::to_delegate(RpcImpl));
67+
io.extend_with(Rpc2::to_delegate(RpcImpl));
68+
}
69+

macros/src/auto_args.rs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,41 @@ macro_rules! metadata {
7272

7373
#[macro_export]
7474
macro_rules! build_rpc_trait {
75+
(
76+
$(#[$t_attr: meta])*
77+
pub trait $name:ident $(<$( $generics:ident ),*>
78+
$(
79+
where
80+
$( $generics2:ident : $bounds:tt $( + $morebounds:tt )* ,)+
81+
)* )*
82+
{
83+
$( $rest: tt )+
84+
}
85+
) => {
86+
build_rpc_trait! {
87+
@WITH_BOUNDS
88+
$(#[$t_attr])*
89+
pub trait $name $(<
90+
// first generic parameters with both bounds
91+
$( $generics ,)*
92+
@BOUNDS
93+
// then specialised ones
94+
$( $( $generics2 : $bounds $( + $morebounds )* )* )*
95+
> )* {
96+
$( $rest )+
97+
}
98+
}
99+
};
100+
75101
// entry-point. todo: make another for traits w/ bounds.
76102
(
103+
@WITH_BOUNDS
77104
$(#[$t_attr: meta])*
78-
pub trait $name: ident $(<$($generics:ident),*>)* {
105+
pub trait $name:ident $(<
106+
$( $simple_generics:ident ,)*
107+
@BOUNDS
108+
$( $generics:ident $(: $bounds:tt $( + $morebounds:tt )* )* ),*
109+
>)* {
79110
$(
80111
$( #[doc=$m_doc:expr] )*
81112
#[ rpc( $($t:tt)* ) ]
@@ -84,7 +115,7 @@ macro_rules! build_rpc_trait {
84115
}
85116
) => {
86117
$(#[$t_attr])*
87-
pub trait $name $(<$($generics,)*>)* : Sized + Send + Sync + 'static {
118+
pub trait $name $(<$( $simple_generics ,)* $( $generics , )*>)* : Sized + Send + Sync + 'static {
88119
$(
89120
$(#[doc=$m_doc])*
90121
fn $m_name ( $($p)* ) -> $result<$out $(, $error)* > ;
@@ -93,7 +124,10 @@ macro_rules! build_rpc_trait {
93124
/// Transform this into an `IoDelegate`, automatically wrapping
94125
/// the parameters.
95126
fn to_delegate<M: $crate::jsonrpc_core::Metadata>(self) -> $crate::IoDelegate<Self, M>
96-
where $($($generics: Send + Sync + 'static + $crate::Serialize + $crate::DeserializeOwned),*)*
127+
where $(
128+
$($simple_generics: Send + Sync + 'static + $crate::Serialize + $crate::DeserializeOwned ,)*
129+
$($generics: Send + Sync + 'static $( + $bounds $( + $morebounds )* )* ),*
130+
)*
97131
{
98132
let mut del = $crate::IoDelegate::new(self.into());
99133
$(
@@ -109,8 +143,13 @@ macro_rules! build_rpc_trait {
109143

110144
// entry-point for trait with metadata methods
111145
(
146+
@WITH_BOUNDS
112147
$(#[$t_attr: meta])*
113-
pub trait $name: ident $(<$($generics:ident),*>)* {
148+
pub trait $name: ident $(<
149+
$( $simple_generics:ident ,)*
150+
@BOUNDS
151+
$($generics:ident $( : $bounds:tt $( + $morebounds:tt )* )* ),*
152+
>)* {
114153
type Metadata;
115154

116155
$(
@@ -133,7 +172,7 @@ macro_rules! build_rpc_trait {
133172
}
134173
) => {
135174
$(#[$t_attr])*
136-
pub trait $name $(<$($generics,)*>)* : Sized + Send + Sync + 'static {
175+
pub trait $name $(<$( $simple_generics ,)* $( $generics , )* >)* : Sized + Send + Sync + 'static {
137176
// Metadata bound differs for traits with subscription methods.
138177
metadata! (
139178
$( $sub_name )*
@@ -154,7 +193,10 @@ macro_rules! build_rpc_trait {
154193
/// Transform this into an `IoDelegate`, automatically wrapping
155194
/// the parameters.
156195
fn to_delegate(self) -> $crate::IoDelegate<Self, Self::Metadata>
157-
where $($($generics: Send + Sync + 'static + $crate::Serialize + $crate::DeserializeOwned),*)*
196+
where $(
197+
$($simple_generics: Send + Sync + 'static + $crate::Serialize + $crate::DeserializeOwned ),*
198+
$($generics: Send + Sync + 'static $( + $bounds $( + $morebounds )* )* ),*
199+
)*
158200
{
159201
let mut del = $crate::IoDelegate::new(self.into());
160202
$(

0 commit comments

Comments
 (0)