Skip to content

Commit 60a24c0

Browse files
committed
Implement cli option for custom derive
closes rust-lang#2170
1 parent a2fe04c commit 60a24c0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

bindgen-cli/options.rs

+21
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ where
225225
"Create PartialEq implementation, if it can not be derived \
226226
automatically.",
227227
),
228+
Arg::new("with-derive-custom")
229+
.long("with-derive-custom")
230+
.help("Derive custom traits on any type.")
231+
.value_name("trait")
232+
.multiple_occurrences(true)
233+
.number_of_values(1),
228234
Arg::new("with-derive-default")
229235
.long("with-derive-default")
230236
.help("Derive Default on any type."),
@@ -744,6 +750,21 @@ where
744750
builder = builder.impl_partialeq(true);
745751
}
746752

753+
if let Some(traits) = matches.values_of("with-derive-custom") {
754+
#[derive(Debug)]
755+
struct CustomTraitCallback {
756+
derives: Vec<String>
757+
}
758+
impl bindgen::callbacks::ParseCallbacks for CustomTraitCallback {
759+
fn add_derives(&self, _name: &str) -> Vec<String> {
760+
self.derives.to_owned()
761+
}
762+
}
763+
764+
let derives: Vec<String> = traits.into_iter().map(|t| t.to_string()).collect();
765+
builder = builder.parse_callbacks(Box::new(CustomTraitCallback { derives }));
766+
}
767+
747768
if matches.is_present("with-derive-default") {
748769
builder = builder.derive_default(true);
749770
}

0 commit comments

Comments
 (0)