cheep-crator-2/vendor/async-recursion
Nixon Enraght-Moony 842725426c Vendor everything 2022-07-19 12:14:16 +01:00
..
src Vendor everything 2022-07-19 12:14:16 +01:00
tests Vendor everything 2022-07-19 12:14:16 +01:00
.cargo-checksum.json Vendor everything 2022-07-19 12:14:16 +01:00
Cargo.toml Vendor everything 2022-07-19 12:14:16 +01:00
LICENSE-APACHE Vendor everything 2022-07-19 12:14:16 +01:00
LICENSE-MIT Vendor everything 2022-07-19 12:14:16 +01:00
README.md Vendor everything 2022-07-19 12:14:16 +01:00

README.md

async-recursion macro

Build Status

Procedural macro for recursive async functions.

Motivation

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`.
 = note: consider using the `async_recursion` crate: https://crates.io/crates/async_recursion

This crate provides an attribute macro to automatically convert an async 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
   }
}

?Send Option

The returned future has a Send bound to make sure it can be sent between threads. If this is undesirable you can mark that the bound should be left out like so:

#[async_recursion(?Send)]
async fn example() {
   // ...
}

In detail:

  • #[async_recursion] modifies your function to return a BoxFuture, and
  • #[async_recursion(?Send)] modifies your function to return a LocalBoxFuture.

License

Licensed under either of

at your option.