简体   繁体   中英

How to avoid repetitive long generic constraints in Rust

I'm trying to make my own implementation of big integers (just for education). The implementation is generic by data type:

struct LongNum<T>
where T: Integer + MulAssign + CheckedMul + CheckedAdd + Copy + From<u8>
{
    values: Vec<T>,
    powers: Vec<u8>,
    radix: u8,
}

The problem is that I need to repeat this verbose constraint for T in all impls. It's too cumbersome.

I can make my own trait combining these constraints, like this:

trait LongNumValue: Integer + MulAssign + CheckedMul + CheckedAdd + Copy + From<u8> {}

struct LongNum<T: LongNumValue>
{
    values: Vec<T>,
    powers: Vec<u8>,
    radix: u8,
}

But in this case I have to add impls for this LongNumValue trait to all types which can be used in LongNum:

impl LongNumValue for u8 {}
impl LongNumValue for u16 {}
impl LongNumValue for u32 {}
...

This means that if I don't add some type to this list of impls, the user of my crate will be unable to use this type for LongNum, even if this type is passes all constraints.

Is there any way to avoid writing long repetitive costraints without adding unnecessary restrictions to user?

You can add a blanket implementation :

impl<T> LongNumValue for T 
where
    T: Integer + MulAssign + CheckedMul + CheckedAdd + Copy + From<u8> {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM