Trait Bounds Puzzle (simplified)

⚓ rust    📅 2025-05-06    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 59 days ago. The information described in this article may have changed.

Hello.

In some ways this is a continuation of the thread: Trait bounds puzzle. That particular problem appears to be unsolvable. However this simplified version (without any generic type params) seems like it might yield more easily.

As the bounds are currently formulated, it either gives an "impl has stricter requirements than trait" error or a "the trait bound is not satisfied" error.

It seems like there is a situation where you "pick only 2" from [blanket impls, multiple interacting traits, method bounds]

trait TraitOne {}

trait TraitTwo {
    fn bounded(&mut self) -> usize
        where Self: TraitOne;
}

impl TraitOne for u64 {}
impl TraitTwo for u64 {
    fn bounded(&mut self) -> usize
        where Self: TraitOne
    { 0 }
}

impl<T> TraitOne for &mut T where T: TraitOne {}

impl<T> TraitTwo for &mut T where T: TraitTwo {
    fn bounded(&mut self) -> usize
        where Self: TraitOne
    {
        (**self).bounded()
    }
}

fn main() { }

Is there some clever way to do this, short of creating TraitThree with a dependency on both of the other traits?

Thank you.

2 posts - 2 participants

Read full topic

🏷️ rust_feed