Skip to content

Commit 979ecce

Browse files
authored
Merge pull request dtolnay#162 from dtolnay/select
Compatibility with tokio::select
2 parents fa92d1f + 4db8933 commit 979ecce

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ quote = "1.0"
1919
syn = { version = "1.0.61", features = ["full", "visit-mut"] }
2020

2121
[dev-dependencies]
22+
futures = "0.3"
2223
rustversion = "1.0"
2324
tracing = "0.1.14"
2425
tracing-attributes = "0.1.14"

src/receiver.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,16 @@ impl VisitMut for ReplaceSelf {
151151

152152
fn visit_item_mut(&mut self, i: &mut Item) {
153153
// Visit `macro_rules!` because locally defined macros can refer to
154-
// `self`. Otherwise, do not recurse into nested items.
154+
// `self`.
155+
//
156+
// Visit `futures::select` and similar select macros, which commonly
157+
// appear syntactically like an item despite expanding to an expression.
158+
//
159+
// Otherwise, do not recurse into nested items.
155160
if let Item::Macro(i) = i {
156-
if i.mac.path.is_ident("macro_rules") {
161+
if i.mac.path.is_ident("macro_rules")
162+
|| i.mac.path.segments.last().unwrap().ident == "select"
163+
{
157164
self.visit_macro_mut(&mut i.mac)
158165
}
159166
}

tests/test.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,3 +1335,29 @@ pub mod issue158 {
13351335
}
13361336
}
13371337
}
1338+
1339+
// https://github.com/dtolnay/async-trait/issues/161
1340+
#[allow(clippy::mut_mut)]
1341+
pub mod issue161 {
1342+
use async_trait::async_trait;
1343+
use futures::future::FutureExt;
1344+
use std::sync::Arc;
1345+
1346+
#[async_trait]
1347+
pub trait Trait {
1348+
async fn f(self: Arc<Self>);
1349+
}
1350+
1351+
pub struct MyStruct(bool);
1352+
1353+
#[async_trait]
1354+
impl Trait for MyStruct {
1355+
async fn f(self: Arc<Self>) {
1356+
futures::select! {
1357+
_ = async {
1358+
println!("{}", self.0);
1359+
}.fuse() => {}
1360+
}
1361+
}
1362+
}
1363+
}

0 commit comments

Comments
 (0)