Skip to content

implement trait delegation delegatable_trait and delegate_trait(target=field)#98

Open
HuStmpHrrr wants to merge 1 commit into
Kobzol:mainfrom
HuStmpHrrr:feature/delegate-trait
Open

implement trait delegation delegatable_trait and delegate_trait(target=field)#98
HuStmpHrrr wants to merge 1 commit into
Kobzol:mainfrom
HuStmpHrrr:feature/delegate-trait

Conversation

@HuStmpHrrr

@HuStmpHrrr HuStmpHrrr commented Apr 10, 2026

Copy link
Copy Markdown

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 to self.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.

@HuStmpHrrr
HuStmpHrrr force-pushed the feature/delegate-trait branch from 6cebc42 to 257c054 Compare April 11, 2026 04:40
@Kobzol

Kobzol commented Apr 13, 2026

Copy link
Copy Markdown
Owner

Hi, could you say a bit more about your use-case that you're trying to solve here? rust-delegate is designed for delegation that can be done in an unobtrusive way without having to annotate traits. For this other approach, https://crates.io/crates/ambassador or https://docs.rs/delegation/latest/delegation/ is likely a better approach.

@HuStmpHrrr

HuStmpHrrr commented Apr 14, 2026

Copy link
Copy Markdown
Author

Hi, I think ambassador is somewhat similar but still not doing what I want. delegation has gone to far for my taste. I am looking for something that is closer to OOP. I am trying to do overriding. In other words, I would like to only delegate the unspecified, required functions, by only specifying what I would like to override. It's like classes in OOP, only overridden functions are changed. other functions are untouched. at this moment, no crate does what I want.

for my actual application, please see this trait

https://github.com/yaspar-org/yaspar-ir/blob/5d0c8c78bccb1366be2d261ccbd033513ba2afd2/src/raw/alg/rec.rs#L81

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.

@Kobzol

Kobzol commented Apr 27, 2026

Copy link
Copy Markdown
Owner

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 rust-delegate does, and is more about overriding trait implementations than delegation. Maybe it would make sense as a separate crate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants