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

Add support for traits with generic parameters #19

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions trait-variant/examples/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ fn spawn_task(factory: impl IntFactory + 'static) {
});
}

#[trait_variant::make(GenericTrait: Send)]
pub trait LocalGenericTrait<'x, S: Sync, Y, const X: usize>
where
Y: Sync,
{
const CONST: usize = 3;
type F;

async fn take(&self, s: S);
}

fn main() {}
66 changes: 55 additions & 11 deletions trait-variant/src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use std::iter;

use proc_macro2::TokenStream;
use quote::quote;
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream},
parse_macro_input,
punctuated::Punctuated,
token::Plus,
Error, FnArg, Generics, Ident, ItemTrait, Pat, PatType, Result, ReturnType, Signature, Token,
TraitBound, TraitItem, TraitItemConst, TraitItemFn, TraitItemType, Type, TypeImplTrait,
TypeParamBound,
token::{Comma, Plus},
Error, FnArg, GenericParam, Generics, Ident, ItemTrait, Lifetime, Pat, PatType, Result,
ReturnType, Signature, Token, TraitBound, TraitItem, TraitItemConst, TraitItemFn,
TraitItemType, Type, TypeImplTrait, TypeParamBound,
};

struct Attrs {
Expand Down Expand Up @@ -162,16 +162,60 @@ fn transform_item(item: &TraitItem, bounds: &Vec<TypeParamBound>) -> TraitItem {

fn mk_blanket_impl(attrs: &Attrs, tr: &ItemTrait) -> TokenStream {
let orig = &tr.ident;
let generics = &tr.generics.params;
let mut generic_names = tr
.generics
.params
.iter()
.map(|generic| match generic {
GenericParam::Lifetime(lt) => GenericParamName::Lifetime(&lt.lifetime),
GenericParam::Type(ty) => GenericParamName::Type(&ty.ident),
GenericParam::Const(co) => GenericParamName::Const(&co.ident),
})
.collect::<Punctuated<_, Comma>>();
let trailing_comma = if !generic_names.is_empty() {
generic_names.push_punct(Comma::default());
quote! { , }
} else {
quote! {}
};
let variant = &attrs.variant.name;
let items = tr.items.iter().map(|item| blanket_impl_item(item, variant));
let items = tr
.items
.iter()
.map(|item| blanket_impl_item(item, variant, &generic_names));
let where_clauses = tr.generics.where_clause.as_ref().map(|wh| &wh.predicates);
quote! {
impl<T> #orig for T where T: #variant {
impl<#generics #trailing_comma TraitVariantBlanketType> #orig<#generic_names>
for TraitVariantBlanketType
where TraitVariantBlanketType: #variant<#generic_names>, #where_clauses
{
#(#items)*
}
}
}

fn blanket_impl_item(item: &TraitItem, variant: &Ident) -> TokenStream {
enum GenericParamName<'s> {
Lifetime(&'s Lifetime),
Type(&'s Ident),
Const(&'s Ident),
}

impl ToTokens for GenericParamName<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
GenericParamName::Lifetime(lt) => lt.to_tokens(tokens),
GenericParamName::Type(ty) => ty.to_tokens(tokens),
GenericParamName::Const(co) => co.to_tokens(tokens),
}
}
}

fn blanket_impl_item(
item: &TraitItem,
variant: &Ident,
generic_names: &Punctuated<GenericParamName<'_>, Comma>,
) -> TokenStream {
// impl<T> IntFactory for T where T: SendIntFactory {
// const NAME: &'static str = <Self as SendIntFactory>::NAME;
// type MyFut<'a> = <Self as SendIntFactory>::MyFut<'a> where Self: 'a;
Expand All @@ -187,7 +231,7 @@ fn blanket_impl_item(item: &TraitItem, variant: &Ident) -> TokenStream {
..
}) => {
quote! {
const #ident #generics: #ty = <Self as #variant>::#ident;
const #ident #generics: #ty = <Self as #variant<#generic_names>>::#ident;
}
}
TraitItem::Fn(TraitItemFn { sig, .. }) => {
Expand All @@ -207,7 +251,7 @@ fn blanket_impl_item(item: &TraitItem, variant: &Ident) -> TokenStream {
};
quote! {
#sig {
<Self as #variant>::#ident(#(#args),*)#maybe_await
<Self as #variant<#generic_names>>::#ident(#(#args),*)#maybe_await
}
}
}
Expand All @@ -222,7 +266,7 @@ fn blanket_impl_item(item: &TraitItem, variant: &Ident) -> TokenStream {
..
}) => {
quote! {
type #ident<#params> = <Self as #variant>::#ident<#params> #where_clause;
type #ident<#params> = <Self as #variant<#generic_names>>::#ident<#params> #where_clause;
}
}
_ => Error::new_spanned(item, "unsupported item type").into_compile_error(),
Expand Down