Skip to content

Commit

Permalink
Merge suppress lint on traits with Send variant
Browse files Browse the repository at this point in the history
This is PR #6.
  • Loading branch information
traviscross authored Dec 18, 2023
2 parents 26b9f63 + f193677 commit b118853
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
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

0 comments on commit b118853

Please sign in to comment.