Skip to content

Commit 001c6b5

Browse files
committed
add builder-clone feature to gate whether Builder implements Clone (and requires ParseCallbacks implementors to implement Clone as well)
1 parent 576cd0f commit 001c6b5

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ cexpr = "0.6"
5151
# This kinda sucks: https://github.com/rust-lang/cargo/issues/1982
5252
clap = { version = "2", optional = true }
5353
clang-sys = { version = "1", features = ["clang_6_0"] }
54-
dyn-clone = "1"
54+
dyn-clone = { version = "1", optional = true }
5555
lazycell = "1"
5656
lazy_static = "1"
5757
peeking_take_while = "0.1.2"
@@ -72,6 +72,8 @@ version = "0.4"
7272

7373
[features]
7474
default = ["logging", "clap", "runtime", "which-rustfmt"]
75+
# Implement the `Clone` trait on `bindgen::Builder`
76+
builder-clone = ["dyn-clone"]
7577
logging = ["env_logger", "log"]
7678
static = ["clang-sys/static"]
7779
runtime = ["clang-sys/runtime"]

src/callbacks.rs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,93 @@ impl Default for MacroParsingBehavior {
2525

2626
/// A trait to allow configuring different kinds of types in different
2727
/// situations.
28+
#[cfg(not(feature = "builder-clone"))]
29+
pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
30+
/// This function will be run on every macro that is identified.
31+
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
32+
MacroParsingBehavior::Default
33+
}
34+
35+
/// The integer kind an integer macro should have, given a name and the
36+
/// value of that macro, or `None` if you want the default to be chosen.
37+
fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {
38+
None
39+
}
40+
41+
/// This will be run on every string macro. The callback cannot influence the further
42+
/// treatment of the macro, but may use the value to generate additional code or configuration.
43+
fn str_macro(&self, _name: &str, _value: &[u8]) {}
44+
45+
/// This will be run on every function-like macro. The callback cannot
46+
/// influence the further treatment of the macro, but may use the value to
47+
/// generate additional code or configuration.
48+
///
49+
/// The first parameter represents the name and argument list (including the
50+
/// parentheses) of the function-like macro. The second parameter represents
51+
/// the expansion of the macro as a sequence of tokens.
52+
fn func_macro(&self, _name: &str, _value: &[&[u8]]) {}
53+
54+
/// This function should return whether, given an enum variant
55+
/// name, and value, this enum variant will forcibly be a constant.
56+
fn enum_variant_behavior(
57+
&self,
58+
_enum_name: Option<&str>,
59+
_original_variant_name: &str,
60+
_variant_value: EnumVariantValue,
61+
) -> Option<EnumVariantCustomBehavior> {
62+
None
63+
}
64+
65+
/// Allows to rename an enum variant, replacing `_original_variant_name`.
66+
fn enum_variant_name(
67+
&self,
68+
_enum_name: Option<&str>,
69+
_original_variant_name: &str,
70+
_variant_value: EnumVariantValue,
71+
) -> Option<String> {
72+
None
73+
}
74+
75+
/// Allows to rename an item, replacing `_original_item_name`.
76+
fn item_name(&self, _original_item_name: &str) -> Option<String> {
77+
None
78+
}
79+
80+
/// This will be called on every file inclusion, with the full path of the included file.
81+
fn include_file(&self, _filename: &str) {}
82+
83+
/// This will be called to determine whether a particular blocklisted type
84+
/// implements a trait or not. This will be used to implement traits on
85+
/// other types containing the blocklisted type.
86+
///
87+
/// * `None`: use the default behavior
88+
/// * `Some(ImplementsTrait::Yes)`: `_name` implements `_derive_trait`
89+
/// * `Some(ImplementsTrait::Manually)`: any type including `_name` can't
90+
/// derive `_derive_trait` but can implemented it manually
91+
/// * `Some(ImplementsTrait::No)`: `_name` doesn't implement `_derive_trait`
92+
fn blocklisted_type_implements_trait(
93+
&self,
94+
_name: &str,
95+
_derive_trait: DeriveTrait,
96+
) -> Option<ImplementsTrait> {
97+
None
98+
}
99+
100+
/// Provide a list of custom derive attributes.
101+
///
102+
/// If no additional attributes are wanted, this function should return an
103+
/// empty `Vec`.
104+
fn add_derives(&self, _name: &str) -> Vec<String> {
105+
vec![]
106+
}
107+
}
108+
109+
/// The same trait as above, but clonable so that it can be included as a
110+
/// field in Builder when the builder-clone feature is enabled such that
111+
/// Builder implements Clone.
112+
#[cfg(feature = "builder-clone")]
28113
pub trait ParseCallbacks:
29-
fmt::Debug + UnwindSafe + dyn_clone::DynClone
114+
fmt::Debug + UnwindSafe + dyn_clone::DynClone
30115
{
31116
/// This function will be run on every macro that is identified.
32117
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
@@ -107,4 +192,5 @@ pub trait ParseCallbacks:
107192
}
108193
}
109194

195+
#[cfg(feature = "builder-clone")]
110196
dyn_clone::clone_trait_object!(ParseCallbacks);

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ impl Default for CodegenConfig {
219219
/// End-users of the crate may need to set the `BINDGEN_EXTRA_CLANG_ARGS` environment variable to
220220
/// add additional arguments. For example, to build against a different sysroot a user could set
221221
/// `BINDGEN_EXTRA_CLANG_ARGS` to `--sysroot=/path/to/sysroot`.
222-
#[derive(Clone, Debug, Default)]
222+
#[cfg_attr(feature = "builder-clone", derive(Clone))]
223+
#[derive(Debug, Default)]
223224
pub struct Builder {
224225
options: BindgenOptions,
225226
input_headers: Vec<String>,
@@ -1663,7 +1664,8 @@ impl Builder {
16631664
}
16641665

16651666
/// Configuration options for generated bindings.
1666-
#[derive(Clone, Debug)]
1667+
#[cfg_attr(feature = "builder-clone", derive(Clone))]
1668+
#[derive(Debug)]
16671669
struct BindgenOptions {
16681670
/// The set of types that have been blocklisted and should not appear
16691671
/// anywhere in the generated code.

0 commit comments

Comments
 (0)