This is a proposal to support a #[trait_common]
attribute for a trait
instance method:
trait T {
#[trait_common]
fn common(&self) -> Arc<Common>;
}
struct Common {
}
impl Common {
fn f(&self) {
}
}
What does this do? It does this:
trait T {
fn common(&self) -> Arc<Common>;
fn f(&self) {
self.common().f();
}
}
This allows a trait
to share a common implementation with multiple implementors by providing accessors for all of the common fields and methods.
I didn't show an example showing how fields are delegated to. I'm not sure if allowing to access fields from self.common()
is as trivial as accessing methods. Implementing just inexistent methods should be easier from compiler architecture.