Making trait implementations transitive

⚓ Rust    📅 2025-06-01    👤 surdeus    👁️ 4      

surdeus

Warning

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

Suppose I have something like this:

trait Extend<T> {
    const EXTRA: usize;
}

trait ExtendBase: Extend<Base> {}

Now I'd like to make Extend<Extend<Base>> have the trait Extend<Base> as well. E.g. for some functions I'd have to write

fn bla<T: ExtendBase, U: Extend<T> + ExtendBase>

whereas I'd prefer to write

fn bla<T: ExtendBase, U: Extend<T>>

because "naturally" something like this should work:

impl<T, U> ExtendBase for U
where
    T: ExtendBase,
    U: Extend<T>,
{const EXTRA = T::EXTRA + <U as Extend<T>>::EXTRA}

Is it possible to do something like this?

2 posts - 2 participants

Read full topic

🏷️ rust_feed