#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

#90 in Asynchronous

Download history 25866/week @ 2024-11-22 29392/week @ 2024-11-29 31980/week @ 2024-12-06 31267/week @ 2024-12-13 17496/week @ 2024-12-20 20188/week @ 2024-12-27 42771/week @ 2025-01-03 63208/week @ 2025-01-10 57536/week @ 2025-01-17 68321/week @ 2025-01-24 69942/week @ 2025-01-31 67784/week @ 2025-02-07 72221/week @ 2025-02-14 84076/week @ 2025-02-21 91016/week @ 2025-02-28 85402/week @ 2025-03-07

344,098 downloads per month
Used in 166 crates (78 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

~205–640KB
~15K SLoC