1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
//! [![github]](https://github.com/wvwwvwwv/async-trait-fn) [![crates-io]](https://crates.io/crates/async-trait-fn) [![docs-rs]](https://docs.rs/async-trait-fn)
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
//! # async-trait-fn
//!
//! This is a fork of the widely acclaimed [async-trait](https://github.com/dtolnay/async-trait)
//! crate. This crate adds two experimental attributes to
//! [async-trait](https://github.com/dtolnay/async-trait) that can be applied to asynchronous trait
//! methods and associated functions to avoid heap memory allocation.
//!
//! ### `unboxed`
//!
//! An `async fn` without a default implementation may get transformed into a
//! method that returns `impl Future + Send + 'async_trait` when
//! `#[macro@unboxed]` is marked on both the trait and the impl blocks.
//! `#[macro@unboxed]` requires the following unstable language features:
//! `associated_type_bounds` and `type_alias_impl_trait`.
//!
//! ```ignore
//! #![feature(associated_type_bounds, type_alias_impl_trait)]
//! # use async_trait_fn::async_trait;
//!
//! #[async_trait]
//! pub trait MyFastTrait {
//! /// `cnt_fast` returns an instance of a concrete `Future` type.
//! #[unboxed]
//! async fn cnt_fast(&self) -> usize;
//!
//! // presumably other methods
//! }
//!
//! struct MyType(usize);
//!
//! #[async_trait]
//! impl MyFastTrait for MyType {
//! #[unboxed]
//! async fn cnt_fast(&self) -> usize {
//! self.0
//! }
//! }
//!
//! let value = MyType(1);
//! let unboxed_future = value.cnt_fast();
//! ```
//!
//! The feature is not generally applicable due to a
//! [bug](https://github.com/rust-lang/rust/issues/95719) in the Rust type
//! system.
//!
//! ### `unboxed_simple`
//!
//! `unboxed_simple` is identical to `unboxed` except that all the lifetime
//! bounds in the type and parameters are substituted with a single lifetime.
#![doc(html_root_url = "https://docs.rs/async-trait/0.1.72")]
#![allow(
clippy::default_trait_access,
clippy::doc_markdown,
clippy::explicit_auto_deref,
clippy::if_not_else,
clippy::items_after_statements,
clippy::match_like_matches_macro,
clippy::module_name_repetitions,
clippy::shadow_unrelated,
clippy::similar_names,
clippy::too_many_lines
)]
extern crate proc_macro;
mod args;
mod bound;
mod expand;
mod lifetime;
mod parse;
mod receiver;
mod verbatim;
use crate::args::Args;
use crate::expand::expand;
use crate::parse::Item;
use proc_macro::TokenStream;
use quote::quote;
use syn::parse_macro_input;
#[proc_macro_attribute]
pub fn async_trait(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as Args);
let mut item = parse_macro_input!(input as Item);
expand(&mut item, args.local);
TokenStream::from(quote!(#item))
}
#[proc_macro_attribute]
pub fn unboxed(_args: TokenStream, input: TokenStream) -> TokenStream {
input
}
#[proc_macro_attribute]
pub fn unboxed_simple(_args: TokenStream, input: TokenStream) -> TokenStream {
input
}