Overspecification of Traits in Rust

⚓ Rust    📅 2025-10-24    👤 surdeus    👁️ 2      

surdeus

So, I recently developed the following Trait:

pub trait Currency {
    type CurrencyType: PartialEq
        + PartialOrd
        + std::ops::Add
        + std::ops::Sub
        + std::ops::Mul<f32>
        + std::ops::Mul<f64>;

    fn zero() -> Self;
}

The idea behind this trait is that it forces any type that implements the trait to also implement certain other traits. The thing is, after doing this, I find myself needing to write the following lines within implementations of generics that force their generics to implement the trait Currency, such as the following (it doesn't include all the traits that I am forcing, I now):

impl<T: Currency> Fee<T> for ShortSeller<T>
where
    T: Currency<CurrencyType = T>,
    T::CurrencyType: std::ops::Sub<Output = T::CurrencyType>
        + std::ops::Mul<f64, Output = T>
        + PartialOrd
        + Copy,

Why do I need to define those same dependencis again? It makes it a bit cumbersome to need to include double traits all the time. It is probably related to the fact that I don't need all the traits in the second code, but I would like to know why cannot we avoid the second declaration, what is left unclear.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed