# Is there a good reason not to automatically implement AsRef\<T\> for Deref\<Target=T\>?

**URL:** <https://internals.rust-lang.org/t/is-there-a-good-reason-not-to-automatically-implement-asref-t-for-deref-target-t/15575>\
**Category:** language design\
**Created:** [November 9, 2021, 12:09am UTC](https://internals.rust-lang.org/t/is-there-a-good-reason-not-to-automatically-implement-asref-t-for-deref-target-t/15575 "2021-11-09T00:09:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fire](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@Fire](https://internals.rust-lang.org/u/Fire)\
**Post date:** [November 9, 2021, 12:09am UTC](https://internals.rust-lang.org/t/is-there-a-good-reason-not-to-automatically-implement-asref-t-for-deref-target-t/15575/1 "2021-11-09T00:09:45Z")

</div>

Something like:

```rust
impl<T> AsRef<T> for dyn Deref<Target=T>
where
    T: ?Sized
{
    fn as_ref(&self) -> &T {
        self.deref()
    }
}

```

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [November 9, 2021, 12:33am UTC](https://internals.rust-lang.org/t/is-there-a-good-reason-not-to-automatically-implement-asref-t-for-deref-target-t/15575/2 "2021-11-09T00:33:46Z")

</div>

`dyn Deref` trait objects seem weird. Definitely uncommon, not too useful. I don't see the point in implementing `AsRef` for them. If you're instead referring to a blanket impl like

```rust
impl<S, T> AsRef<T> for S
where
    T: ?Sized
    S: ?Sized + Deref<Target=T> 
{
    fn as_ref(&self) -> &T {
        self.deref()
    }
}

```

then: introduction of such an impl would be a breaking change AFAICT, and probably also in conclict with other existing blanket implementations, as it would - I think - imply `AsRef<T> for &T` generically, which is not a thing either. (Because of overlap with the existing implementation of `AsRef` for `&T`.)

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [November 9, 2021, 1:01am UTC](https://internals.rust-lang.org/t/is-there-a-good-reason-not-to-automatically-implement-asref-t-for-deref-target-t/15575/3 "2021-11-09T01:01:46Z")

</div>

The implementation for `dyn Deref<Target=T>` specifically is also backwards incompatible, because [you can implement `AsRef<LocalType>` for `dyn Deref<Target=LocalType>`.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5e33f491a9185aba390f3f71917d40e1)

(If you're thinking "but neither the type nor the trait is local", the actual orphan rules are [more nuanced.](https://rust-lang.github.io/rfcs/2451-re-rebalancing-coherence.html#concrete-orphan-rules))

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [February 7, 2022, 1:02am UTC](https://internals.rust-lang.org/t/is-there-a-good-reason-not-to-automatically-implement-asref-t-for-deref-target-t/15575/4 "2022-02-07T01:02:14Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
