trait-aliases

Crates.iotrait-aliases
lib.rstrait-aliases
version0.3.0
created_at2025-12-24 20:09:05.60336+00
updated_at2026-01-17 19:57:20.141855+00
descriptionTrait aliases.
homepage
repositoryhttps://github.com/nekitdev/trait-aliases
max_upload_size
id2003797
size68,995
Nikita Tikhonov (nekitdev)

documentation

https://docs.rs/trait-aliases

README

trait-aliases

License Version Downloads Documentation Test

Trait aliases.

Installation

cargo

You can add trait-aliases as a dependency with the following command:

$ cargo add trait-aliases

Or by directly specifying it in the configuration like so:

[dependencies]
trait-aliases = "0.3.0"

Alternatively, you can add it directly from the source:

[dependencies.trait-aliases]
git = "https://github.com/nekitdev/trait-aliases.git"

Example

Ever felt tired of writing T: Send + Sync + 'static over and over when working with async in multi-threaded scenarios?

Simply define an alias without blanket implementation boilerplate!

use trait_aliases::trait_aliases;

trait_aliases! {
    /// Working in multi-threaded `async` contexts often requires these.
    pub trait SSS = Send + Sync + 'static;
}

This crate will generate the SSS trait with the provided bounds, and implement it for any type satisfying them:

/// Working in multi-threaded `async` contexts often requires these.
pub trait SSS: Send + Sync + 'static {}

/// Blanket implementation of [`SSS`] for all types satisfying its bounds.
impl<__T> SSS for __T where __T: Send + Sync + 'static + ?Sized {}

Attribute

The expansion can be customized via applying the #[trait_alias] attribute:

use trait_aliases::trait_aliases;

trait_aliases! {
    /// Working in multi-threaded `async` contexts often requires these.
    #[trait_alias(
        T,
        doc = "Implemented for any type that is [`Send`], [`Sync`] and `'static`",
        doc = "(meaning it does not contain non-static lifetimes)."
    )]
    pub trait SSS = Send + Sync + 'static;
}

Expands to the following:

/// Working in multi-threaded `async` contexts often requires these.
pub trait SSS: Send + Sync + 'static {}

/// Implemented for any type that is [`Send`], [`Sync`] and `'static`
/// (meaning it does not contain non-static lifetimes).
impl<T> SSS for T where T: Send + Sync + 'static + ?Sized {}

Note

The blanket identifier is essential to correct code generation, therefore any occurrences of the selected identifier will result in compilation errors.

When the identifier is supplied to #[trait_alias], for instance:

use trait_aliases::trait_aliases;

trait_aliases! {
    #[trait_alias(T)]
    trait Convertible<T> = From<T> + Into<T>;
}

will cause compilation to fail with several errors like:

identifier `T` is reserved for blanket implementations

pointing to every occurrence of T within the trait alias definition.

Otherwise, the default __T is used, therefore examples like:

use trait_aliases::trait_aliases;

trait_aliases! {
    trait __T = Sized;
}

fail with the following error:

error: identifier `__T` is reserved for blanket implementations
 --> src/main.rs
  |
  |     trait __T = Sized;
  |           ^^^

Documentation

You can find the documentation here.

Support

If you need support with the library, you can send an email.

Changelog

You can find the changelog here.

Security Policy

You can find the Security Policy of trait-aliases here.

Contributing

If you are interested in contributing to trait-aliases, make sure to take a look at the Contributing Guide, as well as the Code of Conduct.

License

trait-aliases is licensed under the MIT License terms. See License for details.

Commit count: 15

cargo fmt