Skip to content

Commit

Permalink
feat: support async traits
Browse files Browse the repository at this point in the history
  • Loading branch information
makcandrov committed Feb 22, 2024
1 parent 611a100 commit e96da34
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
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
File renamed without changes.
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");
}

0 comments on commit e96da34

Please sign in to comment.