5个版本
| 0.1.4 | 2021年1月12日 |
|---|---|
| 0.1.3 | 2020年3月21日 |
| 0.1.2 | 2020年3月21日 |
| 0.1.1 | 2020年3月18日 |
| 0.1.0 | 2020年3月16日 |
#606 在 过程宏
每月 25 次下载
14KB
250 行
为no_std提供async trait方法
具有如async-trait等特性,避免使用Box和dyn。您可以在不使用alloc的情况下在trait中使用async关键字。
感谢crate async-trait,其中包含一些代码。
警告:此crate使用一些不稳定的甚至不完整的功能。您可能会从编译器中获得一些警告。
如果您想使用此crate,请在您crate的根文件中添加#![feature)][和#![feature)]。
此crate通过#[async_trait]和#[ritit]支持在trait中使用async。
支持的语法
- 在trait中使用
async。#[async_trait]。 - 将
impl trait作为trait的返回值。#[ritit]。
特性状态
-
Self- 通过引用传递
Self。 - 通过值传递
Self。 - 通过可变引用传递
Self。 - 没有
Self。 - 任何类型的
Self。 (需要测试) -
Self通过mut值。 (似乎不常用)
- 通过引用传递
- 任意数量的参数,任意返回值。
- 参数。
- 作为值。
- 作为无生命周期的引用。
- 返回值期望非引用(在
Lifetime return处返回引用)。 (需要测试)
- 参数。
- 生命周期参数。 (需要测试)
- 生命周期参数。
- 生命周期返回。
- 关联类型支持。 (需要测试)
- 在同一个trait中拥有异步和非异步函数。
- 支持在trait中实现默认的
async fn实现。 - 泛型类型参数。
- 泛型参数。
- 泛型返回。 (需要实现)
-
impl trait在参数中。 (需要实现)
用法
请启用功能type_alias_impl_trait和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;
}
}
}
依赖
~1.5MB
~36K SLoC