#async #static #trait #dyn #alloc #box #async-trait

macro async-trait-static

async fn in trait for no_std

5 releases

0.1.4 Jan 12, 2021
0.1.3 Mar 21, 2020
0.1.2 Mar 21, 2020
0.1.1 Mar 18, 2020
0.1.0 Mar 16, 2020

MIT license

13KB
250 lines

Async trait methods for no_std

Features like async-trait, avoid using Box and dyn. You can use async keywork in trait without alloc.

Thanks to crate async-trait, some code from these.

WARNING: This crate use some unstable even incomplete feature. You will get some warning from compiler.

If you want to use crate, please add #![feature(type_alias_impl_trait)] and #![feature(generic_associated_types)] to you crate's root file.

This crate support async in trait through #[async_trait] and sup

Support syntax

  • async in trait. #[async_trait].
  • impl trait as return in trait. #[ritit].

Features Status

  • Self
    • Self by reference.
    • Self by value.
    • Self by mut reference.
    • no Self.
    • any type of Self. (need test)
    • Self by mut value. (It seems unuse)
  • Any number of arguments, any return value.
    • Arguments.
      • As value.
      • As reference without lifetime.
    • Return value expect reference (return reference at Lifetime return). (need test)
  • Lifetime parameters. (need test)
    • Lifetime arguments.
    • Lifetime return.
  • Associated types support. (need test)
  • Having async and non-async functions in the same trait.
  • support default async fn implementations in trait.
  • Generic type parameters.
    • Generic arguments.
    • Generic return. (need implement)
    • impl trait in arguments. (need implement)

Usage

Please enable feature type_alias_impl_trait and generic_associated_types;

async_trait

#![feature(type_alias_impl_trait)]
#![feature(generic_associated_types)]

use async_trait_static::async_trait;

async fn hello() -> u8 {
    1
}

#[async_trait]
trait AsyncFnTrait {
    async fn run(&self);
}

struct AsyncStruct;

#[async_trait]
impl AsyncFnTrait for AsyncStruct {
    async fn run(&self) {
        hello().await;
    }
}

ritit

#![feature(type_alias_impl_trait)]
#![feature(generic_associated_types)]

use async_trait_static::ritit;

#[ritit]
trait AsyncFnTrait {
    fn run<T: Clone>(&self, t: T) -> impl core::future::Future<Output = ()>;
    fn deff(&self) -> impl core::future::Future<Output = u8> {
        async move  { 1 }
    }
}

struct AsyncStruct;

impl AsyncStruct {
    async fn hello(&self) -> u8 {
        1
    }
}

#[ritit]
impl AsyncFnTrait for AsyncStruct {
    fn run<T: Clone>(&self, t: T) -> impl core::future::Future<Output = ()> {
        async move {
            self.hello().await;
        }
    }
}

Dependencies

~0.4–0.8MB
~21K SLoC