Misunderstanding on Traits' Bounds in Where Clause

⚓ Rust    📅 2025-11-27    👤 surdeus    👁️ 5      

surdeus

Hi everyone!
I think I am fundamentally misunderstanding how where clauses on traits work.

Here is a minimum reproducible example:

pub trait PositiveCalculatorAgent<T: PositiveCalculator> {}

pub trait PositiveCalculator: Calculator
where
    Self::Output: Positive,
{
}

pub trait Calculator {
    type Output;
}

pub trait Positive {}

*Note: in my real-world problem, Calculator and Positive's equivalents are from an external crate and the where clause is complex and specifies a bunch of constraints

I would have expected the PositiveCalculator trait to mean something like:

"anything that implements this trait must implement Calculator and in particular, the associated Output type of that implementation must implement Positive"

Instead, though, I get this error on T: PositiveCalculator:

  • The trait bound <T as Calculator>::Output: traits::Positive is not satisfied
  • The trait traits::Positive is not implemented for <T as Calculator>::Output
  • Required by a bound in PositiveCalculator
  • Consider further restricting the associated type: where <T as Calculator>::Output: traits::Positive

Following the given advice and adding the where clause to PositiveCalculator does work. However, the error doesn't make any sense to me. Why does a trait bound on PositiveCalculator require that trait bound to be re-expressed in PositiveCalculatorAgent? Shouldn't T being bound by PositiveCalculator mean that all instances of T must already satisfy the where clause?

I general I've found that this construct requires me to respecify the where clause every time the PositiveCalculator trait is used. The point is that PositiveCalculator should package up these specific requirements, not force them to be continually re-expressed.

What is going on here? Is there a way to do achieve what I'm trying to do?

4 posts - 4 participants

Read full topic

🏷️ Rust_feed