Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[proc macros]: support generic type params #436

Merged
merged 34 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
faf78f1
PoC support generic type params
niklasad1 Aug 20, 2021
351acaf
more annoying example
niklasad1 Aug 20, 2021
66640e7
add trait bounds for generic params in proc macros
niklasad1 Aug 20, 2021
1f91aac
add compile-time test for complicated trait
niklasad1 Aug 20, 2021
230613e
smarter trait bounds in proc macros
niklasad1 Aug 23, 2021
4ed5b58
add non-working example for now
niklasad1 Aug 24, 2021
f442612
revert nits
niklasad1 Aug 24, 2021
834990e
Update examples/proc_macro.rs
niklasad1 Aug 25, 2021
fbb6f00
Update proc-macros/src/helpers.rs
niklasad1 Aug 25, 2021
6e5bcf1
add messy code but works
niklasad1 Aug 25, 2021
ab0b2ec
cleanup
niklasad1 Aug 25, 2021
63c48bd
add some simple compile check in tests
niklasad1 Aug 25, 2021
c7ce163
Merge branch 'na-proc-macros-generics' of github.com:paritytech/jsonr…
niklasad1 Aug 25, 2021
c843a99
Merge remote-tracking branch 'origin/master' into na-proc-macros-gene…
niklasad1 Aug 25, 2021
f04aeaf
fix doc link
niklasad1 Aug 25, 2021
52f61b2
fix doc link last time
niklasad1 Aug 25, 2021
64b33fb
address grumbles
niklasad1 Aug 26, 2021
c9b00c8
docs
niklasad1 Aug 26, 2021
8bff2e9
Update proc-macros/src/helpers.rs
niklasad1 Aug 26, 2021
fbd9683
Update proc-macros/src/helpers.rs
niklasad1 Aug 26, 2021
9f0b300
Update proc-macros/src/helpers.rs
niklasad1 Aug 26, 2021
3065c04
Update proc-macros/src/helpers.rs
niklasad1 Aug 27, 2021
6971610
Update proc-macros/src/visitor.rs
niklasad1 Aug 27, 2021
6f3fed9
fix nit: | -> ||
niklasad1 Aug 27, 2021
6abda51
Update proc-macros/src/helpers.rs
niklasad1 Aug 27, 2021
d86752a
Update proc-macros/src/helpers.rs
niklasad1 Aug 27, 2021
4c83ecc
Update proc-macros/src/helpers.rs
niklasad1 Aug 27, 2021
6ad7bc2
add issues to introduced TODOs
niklasad1 Aug 27, 2021
f8facb2
generics support where clause on trait
niklasad1 Aug 27, 2021
e197af5
Update proc-macros/src/helpers.rs
niklasad1 Aug 27, 2021
27c3a15
Update proc-macros/src/helpers.rs
niklasad1 Aug 27, 2021
ebd7147
address grumbles
niklasad1 Aug 27, 2021
3626e15
Merge branch 'na-proc-macros-generics' of github.com:paritytech/jsonr…
niklasad1 Aug 27, 2021
ba52568
add more docs
niklasad1 Aug 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions proc-macros/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::visitor::{FindSubTyParams, FindTyParams};
use crate::visitor::{FindAllParams, FindSubscriptionParams};
use proc_macro2::Span;
use proc_macro_crate::{crate_name, FoundCrate};
use quote::quote;
Expand Down Expand Up @@ -74,10 +74,7 @@ pub(crate) fn client_add_trait_bounds(item_trait: &syn::ItemTrait, sub_tys: &[sy
if visitor.input_params.contains(&ty.ident) {
ty.bounds.push(parse_quote!(jsonrpsee::types::Serialize))
}
if visitor.ret_params.contains(&ty.ident) {
ty.bounds.push(parse_quote!(jsonrpsee::types::DeserializeOwned))
}
if visitor.sub_params.contains(&ty.ident) {
if visitor.ret_params.contains(&ty.ident) | visitor.sub_params.contains(&ty.ident) {
ty.bounds.push(parse_quote!(jsonrpsee::types::DeserializeOwned))
}
}
Expand All @@ -103,11 +100,7 @@ pub(crate) fn server_generate_where_clause(
bounds.push(parse_quote!(jsonrpsee::types::DeserializeOwned))
}

if visitor.ret_params.contains(&ty.ident) {
bounds.push(parse_quote!(jsonrpsee::types::Serialize))
}

if visitor.sub_params.contains(&ty.ident) {
if visitor.ret_params.contains(&ty.ident) | visitor.sub_params.contains(&ty.ident) {
bounds.push(parse_quote!(jsonrpsee::types::Serialize))
}

Expand All @@ -134,10 +127,10 @@ pub(crate) fn server_generate_where_clause(
.collect()
}

fn visit_trait(item_trait: &syn::ItemTrait, sub_tys: &[syn::Type]) -> FindTyParams {
fn visit_trait(item_trait: &syn::ItemTrait, sub_tys: &[syn::Type]) -> FindAllParams {
let type_params: HashSet<_> = item_trait.generics.type_params().map(|t| t.ident.clone()).collect();
let sub_tys = FindSubTyParams::new(type_params).visit(sub_tys);
let mut visitor = FindTyParams::new(sub_tys);
let sub_tys = FindSubscriptionParams::new(type_params).visit(sub_tys);
let mut visitor = FindAllParams::new(sub_tys);
visitor.visit_item_trait(item_trait);
visitor
}
19 changes: 10 additions & 9 deletions proc-macros/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ use syn::{
/// Visitor that parses generic type parameters from `syn::Type`.
// Based on https://github.com/serde-rs/serde/blob/master/serde_derive/src/bound.rs.
#[derive(Default, Debug)]
pub(crate) struct FindSubTyParams {
pub(crate) struct FindSubscriptionParams {
pub(crate) generic_sub_params: HashSet<Ident>,
pub(crate) all_type_params: HashSet<Ident>,
}

/// Visitor for the entire `RPC trait`.
pub struct FindTyParams {
pub struct FindAllParams {
pub(crate) trait_generics: HashSet<syn::Ident>,
pub(crate) input_params: HashSet<syn::Ident>,
pub(crate) ret_params: HashSet<syn::Ident>,
Expand All @@ -48,7 +48,7 @@ pub struct FindTyParams {
pub(crate) visiting_fn_arg: bool,
}

impl FindTyParams {
impl FindAllParams {
pub fn new(sub_params: HashSet<syn::Ident>) -> Self {
Self {
trait_generics: HashSet::new(),
Expand All @@ -61,7 +61,7 @@ impl FindTyParams {
}
}

impl<'ast> Visit<'ast> for FindTyParams {
impl<'ast> Visit<'ast> for FindAllParams {
fn visit_type_param(&mut self, ty_param: &'ast syn::TypeParam) {
self.trait_generics.insert(ty_param.ident.clone());
}
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<'ast> Visit<'ast> for FindTyParams {
}
}

impl FindSubTyParams {
impl FindSubscriptionParams {
/// Visit all types and returns all generic [`struct@syn::Ident`]'s that are subscriptions.
pub fn visit(mut self, tys: &[syn::Type]) -> HashSet<Ident> {
for ty in tys {
Expand Down Expand Up @@ -225,8 +225,9 @@ mod tests {

let mut exp = HashSet::new();
exp.insert(id);
let generics = exp.clone();

assert_eq!(exp, FindSubTyParams::new(exp.clone()).visit(&[t]));
assert_eq!(exp, FindSubscriptionParams::new(generics).visit(&[t]));
}

#[test]
Expand All @@ -245,11 +246,11 @@ mod tests {
exp.insert(parse_quote!(B));
exp.insert(parse_quote!(C));

assert_eq!(exp, FindSubTyParams::new(exp.clone()).visit(&[t]));
assert_eq!(exp, FindSubscriptionParams::new(generics).visit(&[t]));
}

#[test]
fn nested() {
fn nested_type() {
let t: Type = parse_quote!(Vec<Foo<A, B>>);

let mut generics: HashSet<syn::Ident> = HashSet::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, what is generics for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️

Expand All @@ -263,6 +264,6 @@ mod tests {
exp.insert(parse_quote!(A));
exp.insert(parse_quote!(B));

assert_eq!(exp, FindSubTyParams::new(exp.clone()).visit(&[t]));
assert_eq!(exp, FindSubscriptionParams::new(generics).visit(&[t]));
}
}