#traits #async #impl #async-trait #send #i32 #u32 #future

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

#102 in Asynchronous

Download history 67874/week @ 2025-01-29 69548/week @ 2025-02-05 66949/week @ 2025-02-12 82781/week @ 2025-02-19 89021/week @ 2025-02-26 85223/week @ 2025-03-05 86875/week @ 2025-03-12 89529/week @ 2025-03-19 88294/week @ 2025-03-26 110966/week @ 2025-04-02 99120/week @ 2025-04-09 85237/week @ 2025-04-16 89505/week @ 2025-04-23 82464/week @ 2025-04-30 85360/week @ 2025-05-07 77545/week @ 2025-05-14

349,533 downloads per month
Used in 212 crates (89 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

~175–590KB
~14K SLoC