How to avoid repetitive long generic constraints in Rust

Michael Ilyin

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?

starblue

You can add a blanket implementation:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i avoid to rewrite the same long expression with generic type in rust?

How to avoid repetitive XAML?

C# Avoid Repetitive code and use Generic

How to avoid repetitive codes in cypress

How to avoid repetitive JUnit tests

Rust Constraints for Generic Type Reference

Rust constraints for generic types Item

How to avoid repetitive naming in python packages?

How to avoid repetitive function parameters in clojure?

How to avoid repetitive code in redux (ducks approach)?

Postgres how to avoid repetitive if then in select statement

How to avoid repetitive syscalls and moves in MIPS Assembly?

How to avoid long switches

SQL Query repetitive `AND` query, how do I avoid it?

How to avoid repetitive conditions in authorization policies in .NET Core

How to define WINDOWING function in Spark SQL query to avoid repetitive code

How to avoid multiple node processes doing repetitive things?

How can I avoid repetitive commands, such as cd in a target rule?

How to avoid repetitive CASE statements in SQL WHILE loop doing INSERT

How do I avoid repetitive code in this event handler?

List in Python help needed: how to Avoid calling repetitive code

In Rust, how can I make this code less repetitive?

Avoid repetitive xml code

Loop to avoid repetitive code

avoid repetitive operations with Pandas

Avoid repetitive answer in python

Is there any way to implement a trait with the same struct of different generic constraints in Rust?

How to avoid impl repetition in Rust?

How to avoid excessive cloning in Rust?