`Impl AsRef<T> for T` in the core

Continuing the discussion from [Solved] Function taking slice of objects as well as slice of references to objects:

In the example of using AsRef the solution needed to manually implement impl AsRef<Age> for Age:

impl AsRef<Age> for Age {
    fn as_ref(&self) -> &Age {
        self
    }
}

Why this is needed? Can it no be done automatically by the core?

Similar implementation was done in the core mod.rs - source

Borrow has a blanket impl like that.

I think AsRef intentionally doesn't have a blanket impl so crates can have their own blanket impls for their own types without coherence issues.

Thank you for your response.

Going deeper I found a closed PR with the same idea Make AsRef and AsMut reflexive by clarfonthey · Pull Request #39397 · rust-lang/rust · GitHub

Also the topic is an example of Baby Steps

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.