Skip to content

Commit 34f0bac

Browse files
authored
Introduce DeriveInfo (#2355)
This PR introduces a new non-exhaustive `struct` called `DeriveInfo` to be used as the sole argument of `ParseCallbacks::add_derives` with the purpose of being able to extend the information passed to this method in a backwards-compatible manner, meaning that adding new fields to `DeriveInfo` won't be a breaking change when releasing a new version.
1 parent 8fe2308 commit 34f0bac

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@
155155

156156
## Changed
157157

158+
* Replace the `name: &str` argument for `ParseCallbacks::add_derives` by
159+
`info: DeriveInfo`.
160+
158161
## Removed
159162

160163
* The following deprecated methods and their equivalent CLI arguments were

bindgen-integration/build.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
extern crate bindgen;
22
extern crate cc;
33

4-
use bindgen::callbacks::{IntKind, MacroParsingBehavior, ParseCallbacks};
4+
use bindgen::callbacks::{
5+
DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks,
6+
};
57
use bindgen::{Builder, EnumVariation};
68
use std::collections::HashSet;
79
use std::env;
@@ -121,10 +123,10 @@ impl ParseCallbacks for MacroCallback {
121123
}
122124

123125
// Test the "custom derives" capability by adding `PartialEq` to the `Test` struct.
124-
fn add_derives(&self, name: &str) -> Vec<String> {
125-
if name == "Test" {
126+
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
127+
if info.name == "Test" {
126128
vec!["PartialEq".into()]
127-
} else if name == "MyOrderedEnum" {
129+
} else if info.name == "MyOrderedEnum" {
128130
vec!["std::cmp::PartialOrd".into()]
129131
} else {
130132
vec![]

bindgen/callbacks.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,15 @@ pub trait ParseCallbacks: fmt::Debug {
105105
///
106106
/// If no additional attributes are wanted, this function should return an
107107
/// empty `Vec`.
108-
fn add_derives(&self, _name: &str) -> Vec<String> {
108+
fn add_derives(&self, _info: &DeriveInfo<'_>) -> Vec<String> {
109109
vec![]
110110
}
111111
}
112+
113+
/// Relevant information about a type to which new derive attributes will be added using
114+
/// [`ParseCallbacks::add_derives`].
115+
#[non_exhaustive]
116+
pub struct DeriveInfo<'a> {
117+
/// The name of the type.
118+
pub name: &'a str,
119+
}

bindgen/codegen/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -2114,9 +2114,11 @@ impl CodeGenerator for CompInfo {
21142114

21152115
// The custom derives callback may return a list of derive attributes;
21162116
// add them to the end of the list.
2117-
let custom_derives = ctx
2118-
.options()
2119-
.all_callbacks(|cb| cb.add_derives(&canonical_name));
2117+
let custom_derives = ctx.options().all_callbacks(|cb| {
2118+
cb.add_derives(&crate::callbacks::DeriveInfo {
2119+
name: &canonical_name,
2120+
})
2121+
});
21202122
// In most cases this will be a no-op, since custom_derives will be empty.
21212123
derives.extend(custom_derives.iter().map(|s| s.as_str()));
21222124

@@ -3168,8 +3170,9 @@ impl CodeGenerator for Enum {
31683170

31693171
// The custom derives callback may return a list of derive attributes;
31703172
// add them to the end of the list.
3171-
let custom_derives =
3172-
ctx.options().all_callbacks(|cb| cb.add_derives(&name));
3173+
let custom_derives = ctx.options().all_callbacks(|cb| {
3174+
cb.add_derives(&crate::callbacks::DeriveInfo { name: &name })
3175+
});
31733176
// In most cases this will be a no-op, since custom_derives will be empty.
31743177
derives.extend(custom_derives.iter().map(|s| s.as_str()));
31753178

0 commit comments

Comments
 (0)