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

feat: support async traits #98

Merged
merged 1 commit into from
Feb 27, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,29 +706,31 @@ fn gen_method_item(
// Generate the body of the function. This mainly depends on the self type,
// but also on the proxy type.
let fn_name = &sig.ident;
let await_token = sig.asyncness.map(|_| quote! { .await });

let body = match self_arg {
// Fn proxy types get a special treatment
_ if proxy_type.is_fn() => {
quote! { ({self})(#args) }
quote! { ({self})(#args) #await_token }
}

// No receiver
SelfType::None => {
// The proxy type is a reference, smart pointer or Box.
quote! { #proxy_ty_param::#fn_name #generic_types(#args) }
quote! { #proxy_ty_param::#fn_name #generic_types(#args) #await_token }
}

// Receiver `self` (by value)
SelfType::Value => {
// The proxy type is a Box.
quote! { #proxy_ty_param::#fn_name #generic_types(*self, #args) }
quote! { #proxy_ty_param::#fn_name #generic_types(*self, #args) #await_token }
}

// `&self` or `&mut self` receiver
SelfType::Ref | SelfType::Mut => {
// The proxy type could be anything in the `Ref` case, and `&mut`
// or Box in the `Mut` case.
quote! { #proxy_ty_param::#fn_name #generic_types(self, #args) }
quote! { #proxy_ty_param::#fn_name #generic_types(self, #args) #await_token }
}
};

Expand Down
6 changes: 6 additions & 0 deletions tests/since_1.75/compile-pass/async_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[auto_impl::auto_impl(&, &mut, Arc, Box, Rc)]
trait AsyncTrait {
async fn foo(&self);
}

fn main() {}
11 changes: 9 additions & 2 deletions tests/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ fn ui_compile_fail() {

#[rustversion::since(1.51)]
#[test]
fn ui_recent_compile_pass() {
fn ui_since_1_51_compile_pass() {
let t = TestCases::new();
t.pass("tests/recent/compile-pass/*.rs");
t.pass("tests/since_1.51/compile-pass/*.rs");
}

#[rustversion::since(1.75)]
#[test]
fn ui_since_1_75_compile_pass() {
let t = TestCases::new();
t.pass("tests/since_1.75/compile-pass/*.rs");
}
Loading