Skip to content

infinilabs/impl-trait-utils

 
 

Repository files navigation

Latest Version Documentation GHA Status License

Utilities for working with impl Traits in Rust.

Important

This fork of rust-lang/impl-trait-utils includes the merged PRs rust-lang#20 and rust-lang#30. Once these PRs are merged and the official release is available, you should switch to using that release.

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.

If a non-Send variant of the trait is not needed, the name of the new variant can simply be omitted. E.g., this generates a single (rather than an additional) trait whose definition matches that in the expansion above:

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

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.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 100.0%