nightly macro bin traitlit

This crate exposes an atribute for implementing traits for integer and float types

4 releases

0.2.2 Jun 23, 2019
0.2.1 Jun 20, 2019
0.2.0 Jun 17, 2019
0.1.0 Jun 16, 2019

#1097 in Procedural macros

MIT license

37KB
1K SLoC

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.

pub trait Zero {
    fn zero() -> Self;
}

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.

#[lit(V = [usize, isize])]
impl Zero for V {
    fn zero() -> Self {
        0
    }
}

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:

#[lit(V = u_ + i_)]
impl Zero for _ {
    fn zero() -> Self {
        0
    }
}

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.

#[lit(V = u_ + i_ + f_)]
impl Zero for V {
    fn zero() -> Self {
        0 as Self
    }
}

To simplify the implementation further, we can combine the trait definition and implementation:

#[lit(V = u_ + i_ + f_)]
trait Zero {
    fn zero() -> Self {
        0 as Self
    }
}

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.

Dependencies

~2MB
~48K SLoC