Skip to content

Add source annotation to express explicit derives for a type. #1453

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

Merged
merged 1 commit into from
Nov 30, 2018

Conversation

flowbish
Copy link
Contributor

This allows for explicit selection of which traits are derived for a
type, e.g. deriving custom traits on particular types that need it.

Example usage:

/// <div rustbindgen derive="Clone"></div>
/// <div rustbindgen derive="MyDerivableTrait"></div>
struct foo { ...  };

generates into

 #[derive(Clone,MyDerivableTrait)]
 struct foo { ... }

...as a bit more of an explanation, this is a patch that I've been running bindgen with for a few months to have the ability to run custom derives on types, and more generally be explicit in exactly which derives are applied to a type.

I'm not sure how you feel about PRs out of the blue like this, and if you're looking for a more designed solution then I totally understand I would love to help! (e.g. a command-line or Builder way of specifying derives as well.)

This allows for explicit selection of which traits are derived for a
type, e.g. deriving custom traits on particular types that need it.

Example usage:

```C++
/// <div rustbindgen derive="Clone"></div>
/// <div rustbindgen derive="MyDerivableTrait"></div>
struct foo { ...  };
```

generates into

```Rust
 #[derive(Clone,MyDerivableTrait)]
 struct foo { ... }
```
@highfive
Copy link

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this looks relatively simple / straight-forward. I'm fine for this if it solves a use-case, given it's pretty much a one-liner.

@@ -1764,6 +1764,8 @@ impl CodeGenerator for CompInfo {
derives.push("Eq");
}

derives.extend(item.annotations().derives().iter().map(String::as_str));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe they should be deduplicated?

@emilio
Copy link
Contributor

emilio commented Nov 30, 2018

Thanks for the patch, and sorry for the slight lag!

@bors-servo r+

  • (I hope bors isn't dead yet)

@emilio
Copy link
Contributor

emilio commented Nov 30, 2018

Blerg, looks like it is :)

@emilio emilio merged commit 4013431 into rust-lang:master Nov 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants