Skip to content

Commit

Permalink
Merge pull request #53 from dewert99/async
Browse files Browse the repository at this point in the history
Add support for async functions
  • Loading branch information
dewert99 authored Jan 20, 2024
2 parents d27e6e8 + f1abbfb commit eb0dd3d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
7 changes: 6 additions & 1 deletion ambassador/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,12 @@ fn build_method_invocation(

let generics = method_sig.generics.split_for_impl().1;
let turbofish = generics.as_turbofish();
let post = if original_method.sig.asyncness.is_some() {
quote!(.await)
} else {
quote!()
};

let method_invocation = quote! { #field_ident.#method_ident #turbofish(#argument_list) };
let method_invocation = quote! { #field_ident.#method_ident #turbofish(#argument_list) #post };
method_invocation
}
2 changes: 1 addition & 1 deletion ambassador/tests/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn run_mode(mode: &'static str) {
let mut config = compiletest::Config::default();

config.mode = mode.parse().expect("Invalid mode");
config.target_rustcflags = Some("-L ../target/debug/deps".to_owned());
config.target_rustcflags = Some("-L ../target/debug/deps --edition 2018".to_owned());
config.src_base = PathBuf::from(format!("tests/{}", mode));
config.link_deps(); // Populate config.target_rustcflags with dependencies on the path

Expand Down
51 changes: 51 additions & 0 deletions ambassador/tests/run-pass/async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
extern crate ambassador;

use ambassador::*;

pub struct Base1 {}

#[derive(Delegate)]
#[delegate(Hello, target = "self")]
pub struct Base2 {}

impl Base2 {
async fn hello(&self) {}
}

#[delegatable_trait]
trait Hello {
async fn hello(&self);
}

impl Hello for Base1 {
async fn hello(&self) {}
}

#[derive(Delegate)]
#[delegate(Hello)]
pub struct Dram {
inner: Base1,
}

#[derive(Delegate)]
#[delegate(Hello)]
pub enum Either {
Base1(Base1),
Base2(Base2),
}

struct OpBase(Option<Base1>);

#[delegate_to_methods]
#[delegate(Hello, target_ref = "unwrap_ref")]
impl OpBase {
fn unwrap_ref(&self) -> &Base1 {
self.0.as_ref().unwrap()
}
}

fn main() {
let d = Dram { inner: Base1 {} };

let _ = d.hello();
}

0 comments on commit eb0dd3d

Please sign in to comment.