Skip to content
Open
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
38 changes: 35 additions & 3 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,46 @@ impl ToTokens for AsyncItem {
}

pub fn expand(item: &mut AsyncItem, args: &RecursionArgs) {
let return_type: Option<Box<Type>> = match &item.0.sig.output {
ReturnType::Default => None,
ReturnType::Type(_, typ) => Some(typ.clone()),
};
transform_sig(&mut item.0.sig, args);
transform_block(&mut item.0.block);
transform_block(&mut item.0.block, return_type);
}

fn transform_block(block: &mut Block) {
// Input:
// { <statements> }
//
// Output:
// {
// Box::pin({
// let __ret: Ret = async move { <statements >};
// __ret
// })
// }
fn transform_block(block: &mut Block, return_type: Option<Box<Type>>) {
let brace = block.brace_token;

let bound = match return_type {
Some(typ) => {
quote!(
let __ret: #typ = #block;
__ret
)
}
None => {
quote!(
let __ret: () = #block;
__ret
)
}
};

*block = parse_quote!({
Box::pin(async move #block)
Box::pin(async move {
#bound
})
});
block.brace_token = brace;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/dyn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use async_recursion::async_recursion;

#[async_recursion]
#[allow(dead_code)]
async fn dyn_coercion() -> Box<dyn core::fmt::Debug> {
Box::new("hello world!")
}

#[async_recursion]
async fn no_return_type() {
()
}
2 changes: 1 addition & 1 deletion tests/struct_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn struct_method_with_generic_parameter_works() {
block_on(async move {
let e = Empty {};
assert_eq!(
e.generic_parameter::<*const u64>(&(0 as *const u64)).await,
e.generic_parameter::<*const u64>(&std::ptr::null()).await,
0
);
})
Expand Down