My program has a bunch of functions that operate on generic integer. They are usually of the following form:
use num::{FromPrimitive, Integer, ToPrimitive};
use std::cmp::Ord;
use std::ops::{Add, Mul};
fn function<'a, I>(n: &'a I) -> I
where
I: Integer + Clone + FromPrimitive + ToPrimitive,
for<'b> &'b I: Mul<Output = I> + Add<Output = I> + Ord,
{
}
I want to alias the generic type requirements:
I: Integer + Clone + FromPrimitive + ToPrimitive,
for<'b> &'b I: Mul<Output = I> + Add<Output = I> + Ord,
so that I won't need to rewrite them every time. Initially, I thought macros would help but it looks like they don't work like in C so I looked for another way.
I found a way to do it for the first requirement. One has to apply the default implementation on the defined trait over any type T.
trait GInteger: Integer + Clone + FromPrimitive + ToPrimitive {}
impl<T: Integer + Clone + FromPrimitive + ToPrimitive> GInteger for T {}
Then I can simply write:
I: GInteger
instead of
I: Integer + Clone + FromPrimitive + ToPrimitive,
How can I alias the second requirement? Is it possible?
for<'b> &'b I: Mul<Output = I> + Add<Output = I> + Ord,