Skip to content

Commit e0ddca9

Browse files
committed
allow(async_fn_in_trait) on traits with Send variant
1 parent 26b9f63 commit e0ddca9

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

trait-variant/examples/variant.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::future::Future;
1010

1111
use trait_variant::make_variant;
1212

13-
#[make_variant(SendIntFactory: Send)]
14-
trait IntFactory {
13+
#[make_variant(IntFactory: Send)]
14+
pub trait LocalIntFactory {
1515
const NAME: &'static str;
1616

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

27+
#[allow(dead_code)]
28+
fn spawn_task(factory: impl IntFactory + 'static) {
29+
tokio::spawn(async move {
30+
let _int = factory.make(1, "foo").await;
31+
});
32+
}
33+
2734
fn main() {}

trait-variant/src/variant.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,29 @@ pub fn make_variant(
5656
let attrs = parse_macro_input!(attr as Attrs);
5757
let item = parse_macro_input!(item as ItemTrait);
5858

59+
let maybe_allow_async_lint = if attrs
60+
.variant
61+
.bounds
62+
.iter()
63+
.any(|b| b.path.segments.last().unwrap().ident.to_string() == "Send")
64+
{
65+
quote! { #[allow(async_fn_in_trait)] }
66+
} else {
67+
quote! {}
68+
};
69+
5970
let variant = mk_variant(&attrs, &item);
6071
let blanket_impl = mk_blanket_impl(&attrs, &item);
61-
let output = quote! {
72+
73+
quote! {
74+
#maybe_allow_async_lint
6275
#item
76+
6377
#variant
64-
#blanket_impl
65-
};
6678

67-
output.into()
79+
#blanket_impl
80+
}
81+
.into()
6882
}
6983

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

0 commit comments

Comments
 (0)