https://github.com/aandreba/fast_async_trait
Fast async traits for Rust
https://github.com/aandreba/fast_async_trait
Last synced: about 1 year ago
JSON representation
Fast async traits for Rust
- Host: GitHub
- URL: https://github.com/aandreba/fast_async_trait
- Owner: Aandreba
- License: mit
- Created: 2022-11-26T18:10:18.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-11-26T19:53:37.000Z (over 3 years ago)
- Last Synced: 2025-03-06T00:08:57.736Z (about 1 year ago)
- Language: Rust
- Size: 16.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Fast Async Trait
Library to implement async traits that aren't reliant on boxed futures, relying instead on the `type_alias_impl_trait` nightly feature for it's base implementation.
## Limitations
- Currently, only one lifetime per async function is allowed
```rust
pub trait AsyncTrait {
// allowed
async fn test1 (&self, right: u8);
// allowed
async fn test2 (&self, right: &u8);
// allowed
async fn test3<'b> (&'b self, right: &'b u8);
// compiler error
async fn test4<'b> (&self, right: &'b u8);
}
```
- Async functions with `Self: Rc` and similar aren't currently supported
## Example
```rust
#![feature(type_alias_impl_trait)]
use fast_async_trait::*;
#[async_trait_def]
pub trait AsyncTrait {
type Item;
async fn owned (self) -> Option;
async fn by_ref (&self) -> Option;
async fn by_mut (&mut self) -> Option;
#[inline]
async fn owned_default (self, _n: usize) -> Option where Self: Sized {
return self.owned().await
}
#[inline]
async fn by_ref_default (&self, n: usize) -> Option {
for _ in 0..n {
let _ = self.by_ref().await;
}
return self.by_ref().await
}
#[inline]
async fn by_mut_default (&mut self, n: usize) -> Option {
for _ in 0..n {
let _ = self.by_mut().await;
}
return self.by_mut().await
}
}
#[async_trait_impl]
impl AsyncTrait for (usize, &[u8]) {
type Item = u8;
async fn owned (self) -> Option {
return self.1.get(self.0).copied()
}
#[inline]
async fn by_ref (&self) -> Option {
return self.1.get(self.0).copied()
}
#[inline]
async fn by_mut (&mut self) -> Option {
let v = self.1.get(self.0).copied();
self.0 += 1;
return v;
}
}
```
## Nightly features
The `type_alias_impl_trait` nightly feature is required to be able to add `impl Trait` types (in our case, `impl Future` types) as associated generic types of a trait, which this crate relies on.