#traits #alias #helper #utilities

macro trait-set

Support for trait alias feature on stable Rust

3 releases (breaking)

0.3.0 Feb 20, 2022
0.2.0 May 25, 2021
0.1.0 Apr 18, 2021

#2064 in Rust patterns

Download history 18706/week @ 2024-07-21 17670/week @ 2024-07-28 15892/week @ 2024-08-04 18777/week @ 2024-08-11 21655/week @ 2024-08-18 20330/week @ 2024-08-25 19649/week @ 2024-09-01 23134/week @ 2024-09-08 20974/week @ 2024-09-15 22313/week @ 2024-09-22 25032/week @ 2024-09-29 26122/week @ 2024-10-06 22995/week @ 2024-10-13 25366/week @ 2024-10-20 24756/week @ 2024-10-27 20546/week @ 2024-11-03

94,490 downloads per month
Used in 79 crates (18 directly)

MIT license

14KB
120 lines

trait-set: trait aliases on stable Rust

Status: CI

Project info: Docs.rs Latest Version License Rust 1.50+ required

Support for trait aliases on stable Rust.

Description

This crate provide support for trait aliases: a feature that is already supported by Rust compiler, but is not stable yet.

The idea is simple: combine group of traits under a single name. The simplest example will be:

use trait_set::trait_set;

trait_set! {
    pub trait ThreadSafe = Send + Sync;
}

Macro trait_set displayed here is the main entity of the crate: it allows declaring multiple trait aliases, each of them is represented as

[visibility] trait [AliasName][<generics>] = [Element1] + [Element2] + ... + [ElementN];

Example

use trait_set::trait_set;

trait_set! {
    // Simple combination of two traits.
    /// Doc-comments are also supported btw.
    pub trait ThreadSafe = Send + Sync;

    // Generic alias that gets passed to the associated type.
    pub trait ThreadSafeIterator<T> = ThreadSafe + Iterator<Item = T>;

    // Specialized alias for a generic trait.
    pub trait ThreadSafeBytesIterator = ThreadSafeIterator<u8>;

    // Lifetime bounds.
    pub trait StaticDebug = 'static + std::fmt::Debug;

    // Higher-ranked trait bounds.
    pub trait Serde = Serialize + for<'de> Deserialize<'de>;

    // Lifetime as a generic parameter.
    pub trait SerdeLifetimeTemplate<'de> = Serialize + Deserialize<'de>;
    
    // Trait bounds on generic parameters for an alias.
    pub trait GenericIteratorSendableT<T: Send> = Iterator<Item = T>;
}

Motivation

Rust is great, and it becomes even better through time. However, a time gap between proposing a new feature and getting it stabilized is way too big.

Trait aliases is a great example: 20% of functionality will serve the needs of 80%. So, until they are stabilized, this crate hopefully will allow some folks to write more readable code.

Contributing

Feel free to submit a PR!

LICENSE

trait-set library is licensed under the MIT License. See LICENSE for details.

Dependencies

~1.5MB
~37K SLoC