-
Notifications
You must be signed in to change notification settings - Fork 741
Allow custom derives and attributes #1089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This seems like a good feature to have. I'd prefer not using As far as CLI syntax: |
@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 |
Good catch. I guess that it does make sense to have an escape hatch for more complicated cases in |
What do you think about the attribute approach then? (allowing any attributes to be appended) |
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 |
@fitzgen Maybe could use some other delimiter then, like "=>" or whatever? |
Or, actually, could as well put the regexp first and start at "#" then like |
Yeah, I think that works |
This would be nice for serde derives |
Looking for this as well, willing to work on it -- has any work since been done? |
Added a build script that automatically generates rust bindings for the C structures used by the NE driver. Currently, rust does not allow to implement a destructor and to derive the `Copy` trait for a structure. By default, bindgen derives `Copy` for the generated structures. Although the behavior can be altered by using the Builder's `no_copy()` method, this will also prevent deriving `Clone` (which in turn makes the binding unusable). According to these sources (https://users.rust-lang.org/t/how-to-derive-custom-traits-for-struct-definitions-generated-by-bindgen/39710, rust-lang/rust-bindgen#1089), adding custom derives for the generated bindings is not currently supported. Update: as a workaround (for `ne_user_memory_region`), the bindgen generated struct has been kept and the CLI's MemoryRegion now contains a method for converting between its own format and the driver struct format. This is used for converting to the driver struct representation, when issuing a `NE_SET_USER_MEMORY_REGION` ioctl. The conversion between the two mentioned types is done by implementing the `From` trait for UserMemoryRegion ( which also automatically provides an implementation for `.into()`, for constructing a UserMemoryRegion instance from a MemoryRegion one). Signed-off-by: Gabriel Bercaru <[email protected]>
Added a build script that automatically generates rust bindings for the C structures used by the NE driver. Currently, rust does not allow to implement a destructor and to derive the `Copy` trait for a structure. By default, bindgen derives `Copy` for the generated structures. Although the behavior can be altered by using the Builder's `no_copy()` method, this will also prevent deriving `Clone` (which in turn makes the binding unusable). According to these sources (https://users.rust-lang.org/t/how-to-derive-custom-traits-for-struct-definitions-generated-by-bindgen/39710, rust-lang/rust-bindgen#1089), adding custom derives for the generated bindings is not currently supported. Update: as a workaround (for `ne_user_memory_region`), the bindgen generated struct has been kept and the CLI's MemoryRegion now contains a method for converting between its own format and the driver struct format. This is used for converting to the driver struct representation, when issuing a `NE_SET_USER_MEMORY_REGION` ioctl. The conversion between the two mentioned types is done by implementing the `From` trait for UserMemoryRegion ( which also automatically provides an implementation for `.into()`, for constructing a UserMemoryRegion instance from a MemoryRegion one). Signed-off-by: Gabriel Bercaru <[email protected]>
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. |
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? |
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. |
I'm closing this issue as the enums support was fixed in #2117 |
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?The text was updated successfully, but these errors were encountered: