Blanket implementation of Deref for reference types

⚓ Rust    📅 2026-02-13    👤 surdeus    👁️ 1      

surdeus

Hi, all. I have a question about Deref coercion, specifically about the blanket implementation for reference types.

I am trying to figure out what is going on in impl<T> Deref for &T. The implementation is as follows, link:

impl<T: ?Sized> const Deref for &T {
    type Target = T;

    #[rustc_diagnostic_item = "noop_method_deref"]
    fn deref(&self) -> &T {
        self
    }
}

In the implementation of deref, type of self should be &&T but the return value is of type &T. So I believe there is a type coercion in line 6. That would be in line with the language reference since it specifies "return sites" as potential places for type coercion.

But then, we would need a Deref coercion for a reference which is exactly what is being implemented here (implying fn deref has a circular definition). Am I wrong about the coercion from &&T to &T?

Thanks!

3 posts - 2 participants

Read full topic

🏷️ Rust_feed