Skip to content
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
feat: support async traits
  • Loading branch information
makcandrov committed Feb 22, 2024
commit e96da349e43a72242ded9e1027fb804ce54bc24e
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");
}