Crate async_trait_fn

source ·
Expand description

githubcrates-iodocs-rs

async-trait-fn

This is a fork of the widely acclaimed async-trait crate. This crate adds two experimental attributes to 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.

#![feature(associated_type_bounds, type_alias_impl_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 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.

Attribute Macros