Help on knowing why two implementations are conflicting

⚓ Rust    📅 2025-12-24    👤 surdeus    👁️ 1      

surdeus

Hi,

I want to have a trait RecursiveAsRef<T> that is implemented for &T, &&T, &&T, &&&T.

pub trait RecursiveAsRef<'a, T> {
    fn to_ref(&'a self) -> &'a T;
}

impl<'a, T> RecursiveAsRef<'a, T> for T {
    fn to_ref(&'a self) -> &'a T {
        self
    }
}

impl<'a, S, T> RecursiveAsRef<'a, T> for &'a S
where
    S: RecursiveAsRef<'a, T>,
{
    fn to_ref(&'a self) -> &'a T {
        (**self).to_ref()
    }
}

(Also on the playground)

This code doesn't compile, saying that the two implementations are conflicting for &_. It also doesn't compile with the new trait solver.

Can someone explain why there is a conflict, and eventually give an example code where there is an ambiguity on the impl to use?
I couldn't find a case where the two implementations are conflicting.

10 posts - 4 participants

Read full topic

🏷️ Rust_feed