Skip to content

Commit 9b7fc56

Browse files
authored
Merge pull request #203 from dtolnay/closurecapture
Make closure capture independent of 2018 vs 2021 edition
2 parents f7b7fef + d8587c2 commit 9b7fc56

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/expand.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,11 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
355355
} else {
356356
let pat = &arg.pat;
357357
let ident = positional_arg(i, pat);
358-
quote!(let #pat = #ident;)
358+
if let Pat::Wild(_) = **pat {
359+
quote!(let #ident = #ident;)
360+
} else {
361+
quote!(let #pat = #ident;)
362+
}
359363
}
360364
}
361365
})

tests/test.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,3 +1402,40 @@ pub mod issue183 {
14021402
async fn foo(_n: i32) {}
14031403
}
14041404
}
1405+
1406+
// https://github.com/dtolnay/async-trait/issues/199
1407+
pub mod issue199 {
1408+
use async_trait::async_trait;
1409+
use std::cell::Cell;
1410+
1411+
struct IncrementOnDrop<'a>(&'a Cell<usize>);
1412+
1413+
impl<'a> Drop for IncrementOnDrop<'a> {
1414+
fn drop(&mut self) {
1415+
self.0.set(self.0.get() + 1);
1416+
}
1417+
}
1418+
1419+
#[async_trait(?Send)]
1420+
trait Trait {
1421+
async fn f(counter: &Cell<usize>, arg: IncrementOnDrop<'_>);
1422+
}
1423+
1424+
struct Struct;
1425+
1426+
#[async_trait(?Send)]
1427+
impl Trait for Struct {
1428+
async fn f(counter: &Cell<usize>, _: IncrementOnDrop<'_>) {
1429+
assert_eq!(counter.get(), 0); // second arg not dropped yet
1430+
}
1431+
}
1432+
1433+
#[test]
1434+
fn test() {
1435+
let counter = Cell::new(0);
1436+
let future = Struct::f(&counter, IncrementOnDrop(&counter));
1437+
assert_eq!(counter.get(), 0);
1438+
drop(future);
1439+
assert_eq!(counter.get(), 1);
1440+
}
1441+
}

0 commit comments

Comments
 (0)