Skip to content

Commit 57575ef

Browse files
authored
Add missing user specified trait bounds in [rpc] macro (#569)
* Add missing user specified trait bounds in [rpc] Fix #567 * Nit * Add a test file that should pass the compilation
1 parent c056be4 commit 57575ef

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

derive/src/to_delegate.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ pub fn generate_where_clause_serialization_predicates(
458458
let mut visitor = FindTyParams::default();
459459
visitor.visit_item_trait(item_trait);
460460

461+
let additional_where_clause = item_trait.generics.where_clause.clone();
462+
461463
item_trait
462464
.generics
463465
.type_params()
@@ -483,6 +485,20 @@ pub fn generate_where_clause_serialization_predicates(
483485
bounds.push(parse_quote!(_jsonrpc_core::serde::de::DeserializeOwned))
484486
}
485487
}
488+
489+
// add the trait bounds specified by the user in where clause.
490+
if let Some(ref where_clause) = additional_where_clause {
491+
for predicate in where_clause.predicates.iter() {
492+
if let syn::WherePredicate::Type(where_ty) = predicate {
493+
if let syn::Type::Path(ref predicate) = where_ty.bounded_ty {
494+
if *predicate == ty_path {
495+
bounds.extend(where_ty.bounds.clone().into_iter());
496+
}
497+
}
498+
}
499+
}
500+
}
501+
486502
syn::WherePredicate::Type(syn::PredicateType {
487503
lifetimes: None,
488504
bounded_ty: syn::Type::Path(ty_path),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use jsonrpc_core::futures::future::{self, FutureResult};
2+
use jsonrpc_core::{Error, IoHandler, Result};
3+
use jsonrpc_derive::rpc;
4+
use std::collections::BTreeMap;
5+
6+
#[rpc]
7+
pub trait Rpc<One, Two>
8+
where
9+
One: Ord,
10+
Two: Ord + Eq,
11+
{
12+
/// Adds two numbers and returns a result
13+
#[rpc(name = "setTwo")]
14+
fn set_two(&self, a: Two) -> Result<BTreeMap<One, ()>>;
15+
16+
/// Performs asynchronous operation
17+
#[rpc(name = "beFancy")]
18+
fn call(&self, a: One) -> FutureResult<(One, Two), Error>;
19+
}
20+
21+
struct RpcImpl;
22+
23+
impl Rpc<u64, String> for RpcImpl {
24+
fn set_two(&self, x: String) -> Result<BTreeMap<u64, ()>> {
25+
println!("{}", x);
26+
Ok(Default::default())
27+
}
28+
29+
fn call(&self, num: u64) -> FutureResult<(u64, String), Error> {
30+
crate::future::finished((num + 999, "hello".into()))
31+
}
32+
}
33+
34+
fn main() {
35+
let mut io = IoHandler::new();
36+
let rpc = RpcImpl;
37+
38+
io.extend_with(rpc.to_delegate())
39+
}

0 commit comments

Comments
 (0)