-
Notifications
You must be signed in to change notification settings - Fork 768
codegen: Deduplicate derive traits added by add_derives() parse callback #3296
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
bindgen-tests/tests/parse_callbacks/add_derives_callback/header_add_derives.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
struct SimpleStruct { | ||
int a; | ||
}; |
106 changes: 106 additions & 0 deletions
106
bindgen-tests/tests/parse_callbacks/add_derives_callback/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use bindgen::callbacks::{DeriveInfo, ParseCallbacks}; | ||
use bindgen::{Bindings, Builder}; | ||
use std::path::{Path, PathBuf}; | ||
|
||
#[derive(Debug)] | ||
struct AddDerivesCallback(Vec<String>); | ||
|
||
impl AddDerivesCallback { | ||
fn new(derives: &[&str]) -> Self { | ||
Self(derives.iter().map(|s| (*s).to_string()).collect()) | ||
} | ||
} | ||
|
||
impl ParseCallbacks for AddDerivesCallback { | ||
fn add_derives(&self, _info: &DeriveInfo<'_>) -> Vec<String> { | ||
self.0.clone() | ||
} | ||
} | ||
|
||
struct WriteAdapter<'a>(&'a mut Vec<u8>); | ||
|
||
impl std::io::Write for WriteAdapter<'_> { | ||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
self.0.extend_from_slice(buf); | ||
Ok(buf.len()) | ||
} | ||
fn flush(&mut self) -> std::io::Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn write_bindings_to_string(bindings: &Bindings) -> String { | ||
let mut output = Vec::<u8>::new(); | ||
bindings | ||
.write(Box::new(WriteAdapter(&mut output))) | ||
.unwrap_or_else(|e| { | ||
panic!("Failed to write generated bindings: {e}") | ||
}); | ||
String::from_utf8(output).unwrap_or_else(|e| { | ||
panic!("Failed to convert generated bindings to string: {e}") | ||
}) | ||
} | ||
|
||
fn make_builder(header_path: &Path, add_derives: &[&str]) -> Builder { | ||
Builder::default() | ||
.header(header_path.display().to_string()) | ||
.derive_debug(true) | ||
.derive_copy(false) | ||
.derive_default(false) | ||
.derive_partialeq(false) | ||
.derive_eq(false) | ||
.derive_partialord(false) | ||
.derive_ord(false) | ||
.derive_hash(false) | ||
.parse_callbacks(Box::new(AddDerivesCallback::new(add_derives))) | ||
} | ||
|
||
/// Tests that adding a derive trait that's already derived automatically | ||
/// does not result in a duplicate derive trait (which would not compile). | ||
#[test] | ||
fn test_add_derives_callback_dedupe() { | ||
let crate_dir = | ||
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); | ||
let header_path = crate_dir.join( | ||
"tests/parse_callbacks/add_derives_callback/header_add_derives.h", | ||
); | ||
|
||
let builder = make_builder(&header_path, &["Debug"]); | ||
let bindings = builder | ||
.generate() | ||
.unwrap_or_else(|e| panic!("Failed to generate bindings: {e}")); | ||
let output = write_bindings_to_string(&bindings); | ||
let output_without_spaces = output.replace(' ', ""); | ||
assert!( | ||
output_without_spaces.contains("#[derive(Debug)]") && | ||
!output_without_spaces.contains("#[derive(Debug,Debug)]"), | ||
"Unexpected bindgen output:\n{}", | ||
output.as_str() | ||
); | ||
} | ||
|
||
/// Tests that adding a derive trait that's not already derived automatically | ||
/// adds it to the end of the derive list. | ||
#[test] | ||
fn test_add_derives_callback() { | ||
let crate_dir = | ||
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); | ||
let header_path = crate_dir.join( | ||
"tests/parse_callbacks/add_derives_callback/header_add_derives.h", | ||
); | ||
|
||
let builder = make_builder(&header_path, &["Default"]); | ||
let bindings = builder | ||
.generate() | ||
.unwrap_or_else(|e| panic!("Failed to generate bindings: {e}")); | ||
let output = write_bindings_to_string(&bindings); | ||
let output_without_spaces = output.replace(' ', ""); | ||
assert!( | ||
output_without_spaces.contains("#[derive(Debug,Default)]"), | ||
"Unexpected bindgen output:\n{}", | ||
output.as_str() | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod add_derives_callback; | ||
mod item_discovery_callback; | ||
|
||
use bindgen::callbacks::*; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be less code to extend bindgen-integration perhaps? But this is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not rewrite if this works :)