Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make closure capture independent of 2018 vs 2021 edition #203

Merged
merged 2 commits into from Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}