Skip to content

cleanup #1

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

Merged
merged 3 commits into from
Dec 13, 2023
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
178 changes: 96 additions & 82 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ members = [
"trait_transformer_test",
]

resolver = "2"

[workspace.package]
license = "MIT OR Apache 2.0"

Expand Down
104 changes: 2 additions & 102 deletions trait_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,112 +6,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
parse::{Parse, ParseStream},
parse_macro_input, parse_quote,
punctuated::Punctuated,
token::Comma,
Ident, ItemTrait, Path, PredicateType, Result, ReturnType, Token, TraitBound,
TraitBoundModifier, TraitItem, Type, WherePredicate,
};

struct Attrs {
traits: Punctuated<Transform, Comma>,
}

impl Parse for Attrs {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self {
traits: input.parse_terminated(Transform::parse, Token![,])?,
})
}
}

struct Transform {
subtrait_name: Ident,
#[allow(dead_code)]
colon: Token![:],
subtrait: Path,
}

impl Parse for Transform {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self {
subtrait_name: input.parse()?,
colon: input.parse()?,
subtrait: input.parse()?,
})
}
}
mod transformer;

#[proc_macro_attribute]
pub fn trait_transformer(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let attrs = parse_macro_input!(attr as Attrs);
let item = parse_macro_input!(item as ItemTrait);

let transformed_trait = transform_trait(&attrs, &item);
let output = quote! {
#item
#transformed_trait
};

output.into()
}

fn transform_trait(attrs: &Attrs, tr: &ItemTrait) -> TokenStream {
let traits = attrs
.traits
.iter()
.map(|attr| {
let subtrait = &attr.subtrait;
let fn_bounds = tr.items.iter().filter_map(|item| {
match item {
TraitItem::Fn(item_fn) => {
let is_async = item_fn.sig.asyncness.is_some();
let returns_impl_trait =
if let ReturnType::Type(_, ty) = &item_fn.sig.output {
matches!(**ty, Type::ImplTrait(_))
} else {
false
};

if is_async || returns_impl_trait {
let name = &item_fn.sig.ident;
return Some(quote! { #name(): #subtrait });
}
}
_ => (),
}
None
});

let tr_ident = &tr.ident;
let supertrait = syn::TypeParamBound::Verbatim(quote! {
#tr_ident<#(#fn_bounds),*>
});

ItemTrait {
attrs: Vec::new(),
ident: attr.subtrait_name.clone(),
items: Vec::new(),
supertraits: Punctuated::from_iter(vec![
supertrait,
syn::TypeParamBound::Trait(TraitBound {
paren_token: None,
modifier: TraitBoundModifier::None,
lifetimes: None,
path: attr.subtrait.clone(),
}),
].into_iter()),
..tr.clone()
}
})
.collect::<Vec<_>>();

quote! { #(#traits)* }
transformer::trait_transformer(attr, item)
}
Loading