Here is a little crate I've developed today because I wasn't satisfied with existing options for derive aliases
crate: derive_aliases
docs.rs: crates.io: Rust Package Registry
github: GitHub - nik-rev/derive-aliases: Very elegant #[derive] aliases for Rust 💖
Advantages it offers over the other options (alternatives are listed in README.md)
- When you hover over aliases, you get documentation for them. I.e. what it expands to
- Error messages are better, e.g. if you mispell an alias
Copy
asCop
you get a suggestion: "Did you mean: Copy". it also shows a list of all available aliases - These aliases are defined in a custom, very small DSL. This DSL is in a separate file. This means you can import derive aliases from other files, and share them across multiple crates
- Arguably, syntax is more intuitive, I think
..Alias
makes more sense thanAlias!
. In another derive alias crate, you had to write#[derive(...)]
to define each alias. This is not needed here - If you have 2 aliases that share some derives, the derives will be merged. It won't be a compile error! This is really useful if you have some pre-requisite traits. For example, you might alias
FastHash
tozerocopy::ByteHash
which will also deriveIntoBytes
andAsBytes
, which are required. You might want an aliasFastEq
that deriveszerocopy::ByteEq
and those 2 pre-requisite traits. With other crates, you won't be able to do this. With my crate, you can have both of them at once and the derives will be merged! - At compile-time, I parse all the derive aliases into a
Map<Derive Alias => List of derives it expands to>
. This is done once across the compilation session. I really wanted the performance of myderive
macro to be fast, because I'm using it hundreds+ times. hence I don't even pull any dependencies such asquote
orsyn
. I manually parseTokenStream