async-recursion 0.1.1

Recursion for async functions
Documentation

Consider the following recursive implementation of the fibonacci numbers:

async fn fib(n : u32) -> u64 {
   match n {
       0     => panic!("zero is not a valid argument to fib()!"),
       1 | 2 => 1,
       3     => 2,
       _ => fib(n-1).await + fib(n-2).await
   }
}

The compiler helpfully tells us that:

error[E0733]: recursion in an `async fn` requires boxing
--> src/main.rs:1:26
 |
1 | async fn fib(n : u32) -> u64 {
 |                          ^^^ recursive `async fn`
 |
 = note: a recursive `async fn` must be rewritten to return a boxed `dyn Future`.

This crate provides an attribute macro to automatically convert an async recursive function to one returning a boxed Future.

Example

use async_recursion::async_recursion;

#[async_recursion]
async fn fib(n : u32) -> u64 {
   match n {
       0     => panic!("zero is not a valid argument to fib()!"),
       1 | 2 => 1,
       3     => 2,
       _ => fib(n-1).await + fib(n-2).await
   }
}

Limitations

Currently the macro doesn't consider lifetimes at all; this is something I plan to work on in the future.

License

Licensed under either of