Pre-RFC: Deriving traits for struct from its field


#1

Let’s imagine the following struct:

struct Type1 {
    a: Type2,
    b: Type3,
}

If Type2 implements some trait Foo, sometimes we want Type1 to implement this trait as well (so Type1 will be “proxying” Type2), AFAIK currently doing so will require a fair bit of boilerplate, especially for traits with a lot of methods.

It would be cool to be able to write something like #[derive(Foo<a>)]. Of course Foo and its constraints should not have methods which return Self. (maybe some other restrictions as well?)

Introducing such trait will not only reduce boilerplate in some cases, but also will help to migrate from OOP paradigm, as it can be seen as emulation of OOP inheritance.


#2

See the postponed RFC 1406 for a generalized delegation pattern.


#3

Thank you! I’ve tried to look for something similar, but unsuccessfully.