derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

Support adding an arbitrary attribute on top of `Constructor` impl

Open FalkWoldmann opened this issue 1 year ago • 3 comments
trafficstars

Hi,

as discussed in https://github.com/nrxus/faux/issues/58 and brought forward by @nrxus, it would be nice if #[derive(Constructor)] could support arbitrary attributes. That way, we could use it in conjunction with faux.

@nrxus proposed the following approach:

#[cfg_attr(test, faux::create)]
#[derive(Constructor)]
// when in test, make the `impl` block generated by `derive_more::Constructor` forward a custom attribute
#[cfg_attr(test, constructor(forward = "faux::methods"))]
struct Foo {
    bar: Bar,
}

FalkWoldmann avatar Feb 15 '24 17:02 FalkWoldmann

Can you clarify what code this would generate if the test feature is enabled?

Is this request similar to this one? https://github.com/JelteF/derive_more/issues/102

JelteF avatar Mar 15 '24 08:03 JelteF

Basically, we would have something like this as a result:

#[cfg_attr(test, faux::methods)]
impl Foo {
    fn new(bar: Bar) -> Self {
        Self { bar }
    }
}

FalkWoldmann avatar Mar 15 '24 14:03 FalkWoldmann

Okay, so if I understand correctly, you want to put an attribute on the generated impl. That does seem useful. forward really isn't the right name though, because that already has a different meaning within derive_more. Something like attr seems a better choice for the name. Feel free to create a PR that implements this.

JelteF avatar Mar 16 '24 00:03 JelteF