#async-trait #traits #impl #async

macro no-std trait-variant

Utilities for working with impl traits in Rust

5 releases

0.1.2 Mar 18, 2024
0.1.1 Jan 5, 2024
0.1.0 Dec 21, 2023
0.0.1 Dec 18, 2023
0.0.0 Dec 15, 2023

#115 in Asynchronous

Download history 28681/week @ 2024-08-18 29932/week @ 2024-08-25 28166/week @ 2024-09-01 25205/week @ 2024-09-08 24829/week @ 2024-09-15 34211/week @ 2024-09-22 28528/week @ 2024-09-29 16001/week @ 2024-10-06 15041/week @ 2024-10-13 15495/week @ 2024-10-20 13820/week @ 2024-10-27 22582/week @ 2024-11-03 23597/week @ 2024-11-10 24496/week @ 2024-11-17 24245/week @ 2024-11-24 30970/week @ 2024-12-01

105,625 downloads per month
Used in 94 crates (49 directly)

MIT/Apache

15KB
224 lines

Latest Version Documentation GHA Status License

Utilities for working with impl Traits in Rust.

trait_variant

trait_variant generates a specialized version of a base trait that uses async fn and/or -> impl Trait.

For example, if you want a Sendable version of your trait, you'd write:

#[trait_variant::make(IntFactory: Send)]
trait LocalIntFactory {
    async fn make(&self) -> i32;
    fn stream(&self) -> impl Iterator<Item = i32>;
    fn call(&self) -> u32;
}

The trait_variant::make would generate an additional trait called IntFactory:

use core::future::Future;

trait IntFactory: Send {
   fn make(&self) -> impl Future<Output = i32> + Send;
   fn stream(&self) -> impl Iterator<Item = i32> + Send;
   fn call(&self) -> u32;
}

Implementers can choose to implement either LocalIntFactory or IntFactory as appropriate.

For more details, see the docs for trait_variant::make.

License and usage notes

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Dependencies

~245–700KB
~17K SLoC