Skip to content

Commit

Permalink
Introduce DeriveInfo (#2355)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pvdrz authored Nov 21, 2022
1 parent 8fe2308 commit 34f0bac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@

## Changed

* Replace the `name: &str` argument for `ParseCallbacks::add_derives` by
`info: DeriveInfo`.

## Removed

* The following deprecated methods and their equivalent CLI arguments were
Expand Down
10 changes: 6 additions & 4 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
extern crate bindgen;
extern crate cc;

use bindgen::callbacks::{IntKind, MacroParsingBehavior, ParseCallbacks};
use bindgen::callbacks::{
DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks,
};
use bindgen::{Builder, EnumVariation};
use std::collections::HashSet;
use std::env;
Expand Down Expand Up @@ -121,10 +123,10 @@ impl ParseCallbacks for MacroCallback {
}

// Test the "custom derives" capability by adding `PartialEq` to the `Test` struct.
fn add_derives(&self, name: &str) -> Vec<String> {
if name == "Test" {
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
if info.name == "Test" {
vec!["PartialEq".into()]
} else if name == "MyOrderedEnum" {
} else if info.name == "MyOrderedEnum" {
vec!["std::cmp::PartialOrd".into()]
} else {
vec![]
Expand Down
10 changes: 9 additions & 1 deletion bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ pub trait ParseCallbacks: fmt::Debug {
///
/// If no additional attributes are wanted, this function should return an
/// empty `Vec`.
fn add_derives(&self, _name: &str) -> Vec<String> {
fn add_derives(&self, _info: &DeriveInfo<'_>) -> Vec<String> {
vec![]
}
}

/// Relevant information about a type to which new derive attributes will be added using
/// [`ParseCallbacks::add_derives`].
#[non_exhaustive]
pub struct DeriveInfo<'a> {
/// The name of the type.
pub name: &'a str,
}
13 changes: 8 additions & 5 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2114,9 +2114,11 @@ impl CodeGenerator for CompInfo {

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

Expand Down Expand Up @@ -3168,8 +3170,9 @@ impl CodeGenerator for Enum {

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

Expand Down

0 comments on commit 34f0bac

Please sign in to comment.