• Stars
    star
    180
  • Rank 208,716 (Top 5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Procedural macro for recursive async functions

async-recursion macro

Latest version crates.io downloads Build Status Apache/MIT2.0 License

Procedural macro for recursive async functions.

Motivation

Consider the following recursive implementation of the fibonacci numbers:

async fn fib(n : u32) -> u32 {
   match n {
       0 | 1 => 1,
       _ => 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) -> u32 {
  |                          ^^^ 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) -> u32 {
   match n {
      0 | 1 => 1,
      _ => 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.

More Repositories

1

dcc-lsystem

An implementation of a Lindenmayer system
Rust
7
star
2

spaceindex

An rtree implementation in rust.
Rust
5
star
3

async-sea-orm-session

An async-session backend using sea-orm.
Rust
4
star
4

fwatch

A file watching library written in Rust.
Rust
3
star
5

libsts

A rust library for working with Slay the Spire save and run files.
Rust
3
star
6

robbot

A discord bot implemented using my async fork of serenity
Rust
3
star
7

vnq

VN Quotes website
PHP
2
star
8

uniquebot

a bot which kicks you if you are repetitive
Python
2
star
9

pullcaps

A convenient, opinionated, asynchronous client for the PushShift API
Rust
2
star
10

random_graphs

A Rust library for generating random graphs
Rust
1
star
11

aoc2019

Rust
1
star
12

ststracker-base

Rust
1
star
13

vnq_server

Backend GraphQL server for VNQ.
Rust
1
star
14

jff2hs

a tool to convert jflap-TM files to a haskell description
Python
1
star
15

saucy

A helper tool for finding nearby Python virtual environments.
Rust
1
star
16

cargo-files

List all source files in a cargo crate.
Rust
1
star
17

sle_p

Python
1
star
18

teamliquid_streamer_graph

grab teamliquid stream data and present it in JSON format
JavaScript
1
star
19

tikzhelper

A web application for dynamically generating useful tikz code
Python
1
star
20

ststracker-client

Rust
1
star
21

dccweb

The source code behind my (extremely basic) personal website
PHP
1
star
22

webwork-to-canvas

A tool for processing WeBWork data for import into Canvas
TypeScript
1
star
23

ststracker-server

Rust
1
star
24

cargo-derivefmt

Rust
1
star
25

trollchat

An xchat plugin which trolls people as they join a channel
Python
1
star