traitlit
This crate defines an attribute called lit
. This attribute converts a single implementation of a trait to seperate implementations for the specified types.
Let's say I want to define a Zero
trait using lit.
Normaly this would be achived by either manually implementing the trait for the desired types, or by creating a macro to produce a specific set of implementations. Using lit
we can simply implement it as follows.
This generates two implementations, one for usize
and one for isize
. This is very repetitive, as most such traits needs to be implemented by every type in a specific category. To this end lit
allows you to write u_
instead of u8
, u16
, u32
, u64
, u128
, and usize
, aswell as i_
for the corosponding signed types. lit
also accepts f_
which corrosponds to f32
and f64
. With this the previous example becomes:
The current implementation would however not work for float types, as 0
is not a float literal. This can be easily remedied using type casting.
To simplify the implementation further, we can combine the trait definition and implementation:
This will however assume that all implementations in the trait are part of the individual implementations rather than a default. If you wish to have defaults in your trait, keep the trait and implementation seperate.
You may have noticed the V
in the annotaion and the impl
block. You can write any valid identifier instead of V
, and all instances of that identifier will be substituted sutebly.