implement trait delegation delegatable_trait and delegate_trait(target=field)#98
implement trait delegation delegatable_trait and delegate_trait(target=field)#98HuStmpHrrr wants to merge 1 commit into
Conversation
6cebc42 to
257c054
Compare
|
Hi, could you say a bit more about your use-case that you're trying to solve here? |
|
Hi, I think for my actual application, please see this trait this trait is very long, but really most cases are trivial. I bumped into delegate last week and created this PR: https://github.com/yaspar-org/yaspar-ir/pull/63/changes I figured that delegate can do a stretch. if you think this is not the right solution, I am all ears though. |
|
The implementation needs to have some way of referring to the delegated trait; using just its name does not work in general, e.g.: mod foo {
use delegate::delegatable_trait;
#[delegatable_trait]
pub trait Greet {
fn hello(&self) -> String;
fn hello2(&self) -> String;
fn goodbye(&self) -> String;
}
}
use foo::Greet as Greet2;breaks. I examined how the macro works, and I think that this is quite far from what |
see the new added test for an example use.
I added two macros,
#[delegatable_trait]and#[delegate_trait].#[delegatable_trait]tags trait, the impl of which can be applied#[delegate_trait]. For an impl of a tagged trait,#[delegate_trait(target=X)]delegates all unimplemented required functions toself.X.f(...). In particular, if a function has a default body, the function it kept as is, and not delegated. This behavior mimics inheritance in OOP.