Skip to content

Latest commit

 

History

History
108 lines (82 loc) · 2.53 KB

README.md

File metadata and controls

108 lines (82 loc) · 2.53 KB

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 unimplementable)
  • 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.
    • 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;
        }
    }
}