Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation on the macro itself #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
35 changes: 35 additions & 0 deletions macro_docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Example
```rust
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,
_ => fib(n-1).await + fib(n-2).await
}
}
```

## ?Send Option

By default 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:

```
# use async_recursion::async_recursion;

#[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`].

[`BoxFuture`]: https://docs.rs/futures/0.3.4/futures/future/type.BoxFuture.html
[`LocalBoxFuture`]: https://docs.rs/futures/0.3.4/futures/future/type.LocalBoxFuture.html
2 changes: 1 addition & 1 deletion src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl ToTokens for AsyncItem {
}

pub fn expand(item: &mut AsyncItem, args: &RecursionArgs) {
transform_sig(&mut item.0.sig, &args);
transform_sig(&mut item.0.sig, args);
transform_block(&mut item.0.block);
}

Expand Down
46 changes: 12 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,55 +34,27 @@
//! ```
//!
//! This crate provides an attribute macro to automatically convert an async function
//! to one returning a boxed Future.
//! to one returning a boxed [`Future`](core::future::Future).
//!
//! ## Example
//!
//! ```rust
//! 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
//!
//! By default the returned future has a `Send` bound to make sure that it can be sent between threads. If this is not desired you can mark that you would like that that bound to be left out like so:
//!
//! ```rust
//! # use async_recursion::async_recursion;
//! #[async_recursion(?Send)]
//! async fn example() {}
//! ```
//!
//! In other words, `#[async_recursion]` modifies your function to return a [`BoxFuture`] and `#[async_recursion(?Send)]` modifies your function to return a [`LocalBoxFuture`].
//!
//! [`BoxFuture`]: https://docs.rs/futures/0.3.4/futures/future/type.BoxFuture.html
//! [`LocalBoxFuture`]: https://docs.rs/futures/0.3.4/futures/future/type.LocalBoxFuture.html
#![doc = include_str!("../macro_docs.md")]
//!
//! ## Installation
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! async-recursion = "0.2"
//! async-recursion = "0.3"
//! ```
//!
//! ### License
//!
//! Licensed under either of
//! * Apache License, Version 2.0
//! ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
//! ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
//! * MIT license
//! ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
//! ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
//!
//! at your option.

extern crate proc_macro;
Expand All @@ -97,6 +69,12 @@ use proc_macro::TokenStream;
use quote::quote;
use syn::parse_macro_input;

/// Procedural macro for recursive macro functions.
///
/// This is an attribute macro to automatically convert an async function to one
/// returning a boxed [`Future`](core::future::Future).
///
#[doc = include_str!("../macro_docs.md")]
#[proc_macro_attribute]
pub fn async_recursion(args: TokenStream, input: TokenStream) -> TokenStream {
let mut item = parse_macro_input!(input as AsyncItem);
Expand Down