Coder Social home page Coder Social logo

async-trait-static's Introduction

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;
        }
    }
}

async-trait-static's People

Contributors

tiannian avatar

Watchers

Rohan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.