rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Allow custom derives and attributes

Open RReverser opened this issue 7 years ago • 14 comments

Several times I found myself in need to derive traits that are not autogenerated by bindgen, and, unfortunately, Rust doesn't allow to put a derive directive (or any attribute) at distance from the original type, even in the same file.

Would it make sense to provide ability to add custom derives or even any attributes to generated types via ParseCallbacks or some other mechanism?

RReverser avatar Oct 18 '17 17:10 RReverser

This seems like a good feature to have.

I'd prefer not using ParseCallbacks if possible, because avoiding them allows easier testing, and I believe that custom derives is specific enough that it doesn't need the open ended programmatic access that ParseCallbacks provides.

As far as CLI syntax: --custome-derive '<trait>=<regex>'?

fitzgen avatar Oct 23 '17 16:10 fitzgen

@fitzgen Thing is, some derives still accept options via extra attributes (for example, https://github.com/kwohlfahrt/enum-tryfrom), so I'm not sure if it's worth locking up to just trait name. Maybe accept a raw "header" string to prepend to the type definition? Or at least attribute value, and then "derive" is just one of attribute types like --prepend-attr 'derive(Default)=<regex>'.

RReverser avatar Oct 23 '17 17:10 RReverser

Good catch. I guess that it does make sense to have an escape hatch for more complicated cases in ParseCallbacks -- but I'd still prefer the simple versions to be supported on the CLI for easier testing (and in general it is best when things are usable in either scenario).

fitzgen avatar Oct 23 '17 17:10 fitzgen

What do you think about the attribute approach then? (allowing any attributes to be appended) Should be still CLI compatible while allowing these more complex cases.

RReverser avatar Oct 23 '17 17:10 RReverser

Sorry I totally thought throught that stuff in my head and then failed to leave any comment about it :-p

The problem is that we would need to parse arbitrary attributes to know which = we are splitting on between the attr and the regex. This is because of attrs like #[serde(rename = "blah")]. I guess we could use https://dtolnay.github.io/syn/syn/fn.parse_outer_attr.html, but that signature doesn't seem to allow trailing bits or return where the parsing ended...

fitzgen avatar Oct 23 '17 17:10 fitzgen

@fitzgen Maybe could use some other delimiter then, like "=>" or whatever?

RReverser avatar Oct 23 '17 17:10 RReverser

Or, actually, could as well put the regexp first and start at "#" then like regex#[derive(Default)] or regex=#[derive(Default)] or even just regex=derive(Default) avoiding '#' completely.

RReverser avatar Oct 23 '17 17:10 RReverser

Yeah, I think that works

fitzgen avatar Oct 23 '17 17:10 fitzgen

This would be nice for serde derives

joelgallant avatar Feb 15 '19 05:02 joelgallant

Looking for this as well, willing to work on it -- has any work since been done?

meme avatar Oct 04 '19 22:10 meme

I don't think there has been much progress here, but I don't think this should be particularly hard to implement, happy to help.

emilio avatar Apr 06 '21 09:04 emilio

I want my derive to apply to all structs, which would simplify the implementation for me. Perhaps I can implement that and leave it open to be extended to a regex?

tbodt avatar Apr 06 '21 17:04 tbodt

It would be nice, if it would work with enums too. If I'm right then the current implementation of #2059 does only work with structs, or does it?

trobanga avatar Nov 07 '21 18:11 trobanga

It would be nice, if it would work with enums too. If I'm right then the current implementation of #2059 does only work with structs, or does it?

I agree, it doesn't seem to work.

ericseppanen avatar Nov 07 '21 21:11 ericseppanen

I'm closing this issue as the enums support was fixed in #2117

pvdrz avatar Sep 20 '22 20:09 pvdrz