I have a lot of lengthy and repetitive trait constraint chains in my implementations that I want to replace. Type aliasing for traits looks great but it is still an unstable feature.
I tried using something like this
trait NumericTrait : Float + FromPrimitive + ToPrimitive + RealField + Copy + Clone + Debug + 'static {}
However this leads to issues since NumericTrait
is not explicitly implemented for the types I am concerned with. Rather, all of the base traits are.
I am now thinking macros is the way to go, so I tried the following:
macro_rules! numeric_float_trait {
() => {
Float + FromPrimitive + ToPrimitive + RealField + Copy + Clone + Debug + 'static
};
}
struct SomeStruct;
impl<T> SomeStruct
where
T: numeric_float_trait!()
{
fn do_things(num: T) {
println!("I got this num {:?}", num);
}
}
However the syntax is incorrect and the compiler will not accept it. How do I achieve this desired functionality of essentially pasting a list of traits via a macro?