Skip to content
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

Introduce DeriveInfo #2355

Merged
merged 3 commits into from Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
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
@@ -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
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> {
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
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
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