Skip to content
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: 9 additions & 2 deletions trait-variant/examples/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::future::Future;

use trait_variant::make_variant;

#[make_variant(SendIntFactory: Send)]
trait IntFactory {
#[make_variant(IntFactory: Send)]
pub trait LocalIntFactory {
const NAME: &'static str;

type MyFut<'a>: Future
Expand All @@ -24,4 +24,11 @@ trait IntFactory {
fn another_async(&self, input: Result<(), &str>) -> Self::MyFut<'_>;
}

#[allow(dead_code)]
fn spawn_task(factory: impl IntFactory + 'static) {
tokio::spawn(async move {
let _int = factory.make(1, "foo").await;
});
}

fn main() {}
22 changes: 18 additions & 4 deletions trait-variant/src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,29 @@ pub fn make_variant(
let attrs = parse_macro_input!(attr as Attrs);
let item = parse_macro_input!(item as ItemTrait);

let maybe_allow_async_lint = if attrs
.variant
.bounds
.iter()
.any(|b| b.path.segments.last().unwrap().ident == "Send")
{
quote! { #[allow(async_fn_in_trait)] }
} else {
quote! {}
};

let variant = mk_variant(&attrs, &item);
let blanket_impl = mk_blanket_impl(&attrs, &item);
let output = quote! {

quote! {
#maybe_allow_async_lint
#item

#variant
#blanket_impl
};

output.into()
#blanket_impl
}
.into()
}

fn mk_variant(attrs: &Attrs, tr: &ItemTrait) -> TokenStream {
Expand Down