Skip to content

Commit

Permalink
Merge pull request #203 from dtolnay/closurecapture
Browse files Browse the repository at this point in the history
Make closure capture independent of 2018 vs 2021 edition
  • Loading branch information
dtolnay committed Jun 2, 2022
2 parents f7b7fef + d8587c2 commit 9b7fc56
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/expand.rs
Expand Up @@ -355,7 +355,11 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
} else {
let pat = &arg.pat;
let ident = positional_arg(i, pat);
quote!(let #pat = #ident;)
if let Pat::Wild(_) = **pat {
quote!(let #ident = #ident;)
} else {
quote!(let #pat = #ident;)
}
}
}
})
Expand Down
37 changes: 37 additions & 0 deletions tests/test.rs
Expand Up @@ -1402,3 +1402,40 @@ pub mod issue183 {
async fn foo(_n: i32) {}
}
}

// https://github.com/dtolnay/async-trait/issues/199
pub mod issue199 {
use async_trait::async_trait;
use std::cell::Cell;

struct IncrementOnDrop<'a>(&'a Cell<usize>);

impl<'a> Drop for IncrementOnDrop<'a> {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}

#[async_trait(?Send)]
trait Trait {
async fn f(counter: &Cell<usize>, arg: IncrementOnDrop<'_>);
}

struct Struct;

#[async_trait(?Send)]
impl Trait for Struct {
async fn f(counter: &Cell<usize>, _: IncrementOnDrop<'_>) {
assert_eq!(counter.get(), 0); // second arg not dropped yet
}
}

#[test]
fn test() {
let counter = Cell::new(0);
let future = Struct::f(&counter, IncrementOnDrop(&counter));
assert_eq!(counter.get(), 0);
drop(future);
assert_eq!(counter.get(), 1);
}
}

0 comments on commit 9b7fc56

Please sign in to comment.