Crates.io | trait-set |
lib.rs | trait-set |
version | 0.3.0 |
source | src |
created_at | 2021-04-18 18:30:40.244063 |
updated_at | 2022-02-20 07:00:42.213943 |
description | Support for trait alias feature on stable Rust. |
homepage | |
repository | https://github.com/popzxc/trait-set |
max_upload_size | |
id | 386261 |
size | 22,654 |
trait-set
: trait aliases on stable RustSupport for trait aliases on stable Rust.
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];
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>;
}
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.
Feel free to submit a PR!
trait-set
library is licensed under the MIT License. See LICENSE for details.