derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

`derive(AsRef)` should also derive identity impl.

Open wdanilo opened this issue 4 years ago • 2 comments

Hi! I believe that:

#[derive(AsRef)]
struct Foo { t:i32 }

should generate identity AsRef impl in addition to what it generates now:

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

Making these instances by hand is cumbersome and they should be generated automatically.

wdanilo avatar Apr 10 '20 21:04 wdanilo

I'm not entirely sure when you would actually want this, but if you can give some useful example I'm definitely open to accepting PRs for this. Should be behind a attribute flag though, e.g. as_ref(Self)

JelteF avatar Dec 22 '23 00:12 JelteF

@JelteF @wdanilo I think that this is quite a particular pattern, not enforced neither in std, nor being useful in a common case.

Now, having the ability to specify #[as_ref(Self)] should solve the @wdanilo's situation good enough, I think.

tyranron avatar Dec 22 '23 12:12 tyranron