From 86f759395294139f924024b50629a74715e6525a Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 18 Nov 2022 15:30:00 -0500 Subject: [PATCH 1/3] Introduce `DeriveInfo` --- CHANGELOG.md | 3 +++ bindgen-integration/build.rs | 10 ++++++---- bindgen/callbacks.rs | 10 +++++++++- bindgen/codegen/mod.rs | 13 ++++++++----- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 237e3801c3..9224c018a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs index 980d40b956..7afe4e9188 100644 --- a/bindgen-integration/build.rs +++ b/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; @@ -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 { - if name == "Test" { + fn add_derives(&self, info: DeriveInfo<'_>) -> Vec { + if info.name == "Test" { vec!["PartialEq".into()] - } else if name == "MyOrderedEnum" { + } else if info.name == "MyOrderedEnum" { vec!["std::cmp::PartialOrd".into()] } else { vec![] diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index fb84c8c331..79e6004315 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -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 { + fn add_derives(&self, _info: DeriveInfo<'_>) -> Vec { 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, +} diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index c7ac59db42..2670be3e08 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -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())); @@ -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())); From 2bab3eaeaa22c1d3b816e4d5c3761d3af30c474a Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 21 Nov 2022 10:22:06 -0500 Subject: [PATCH 2/3] Pass info by reference --- bindgen/callbacks.rs | 2 +- bindgen/codegen/mod.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index 79e6004315..bebd6978aa 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -105,7 +105,7 @@ pub trait ParseCallbacks: fmt::Debug { /// /// If no additional attributes are wanted, this function should return an /// empty `Vec`. - fn add_derives(&self, _info: DeriveInfo<'_>) -> Vec { + fn add_derives(&self, _info: &DeriveInfo<'_>) -> Vec { vec![] } } diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 2670be3e08..e758963ac0 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -2115,7 +2115,7 @@ 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(crate::callbacks::DeriveInfo { + cb.add_derives(&crate::callbacks::DeriveInfo { name: &canonical_name, }) }); @@ -3171,7 +3171,7 @@ 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(crate::callbacks::DeriveInfo { name: &name }) + 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())); From b0d293a7551f07b4ec61b7dbffb651b47e9d1ce1 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 21 Nov 2022 10:29:40 -0500 Subject: [PATCH 3/3] Fix integration build script --- bindgen-integration/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs index 7afe4e9188..0f30ad470f 100644 --- a/bindgen-integration/build.rs +++ b/bindgen-integration/build.rs @@ -123,7 +123,7 @@ impl ParseCallbacks for MacroCallback { } // Test the "custom derives" capability by adding `PartialEq` to the `Test` struct. - fn add_derives(&self, info: DeriveInfo<'_>) -> Vec { + fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec { if info.name == "Test" { vec!["PartialEq".into()] } else if info.name == "MyOrderedEnum" {