Skip to content

Commit fcdbef4

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 fcdbef4

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

Cargo.toml

+3-1
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

+88-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ impl Default for MacroParsingBehavior {
2525

2626
/// A trait to allow configuring different kinds of types in different
2727
/// situations.
28-
pub trait ParseCallbacks:
29-
fmt::Debug + UnwindSafe + dyn_clone::DynClone
28+
#[cfg(not(feature = "builder-clone"))]
29+
pub trait ParseCallbacks: fmt::Debug + UnwindSafe
3030
{
3131
/// This function will be run on every macro that is identified.
3232
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
@@ -107,4 +107,90 @@ pub trait ParseCallbacks:
107107
}
108108
}
109109

110+
/// The same trait as above, but clonable so that it can be included as a
111+
/// field in Builder when the builder-clone feature is enabled such that
112+
/// Builder implements Clone.
113+
#[cfg(feature = "builder-clone")]
114+
pub trait ParseCallbacks: fmt::Debug + UnwindSafe + dyn_clone::DynClone
115+
{
116+
/// This function will be run on every macro that is identified.
117+
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
118+
MacroParsingBehavior::Default
119+
}
120+
121+
/// The integer kind an integer macro should have, given a name and the
122+
/// value of that macro, or `None` if you want the default to be chosen.
123+
fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {
124+
None
125+
}
126+
127+
/// This will be run on every string macro. The callback cannot influence the further
128+
/// treatment of the macro, but may use the value to generate additional code or configuration.
129+
fn str_macro(&self, _name: &str, _value: &[u8]) {}
130+
131+
/// This will be run on every function-like macro. The callback cannot
132+
/// influence the further treatment of the macro, but may use the value to
133+
/// generate additional code or configuration.
134+
///
135+
/// The first parameter represents the name and argument list (including the
136+
/// parentheses) of the function-like macro. The second parameter represents
137+
/// the expansion of the macro as a sequence of tokens.
138+
fn func_macro(&self, _name: &str, _value: &[&[u8]]) {}
139+
140+
/// This function should return whether, given an enum variant
141+
/// name, and value, this enum variant will forcibly be a constant.
142+
fn enum_variant_behavior(
143+
&self,
144+
_enum_name: Option<&str>,
145+
_original_variant_name: &str,
146+
_variant_value: EnumVariantValue,
147+
) -> Option<EnumVariantCustomBehavior> {
148+
None
149+
}
150+
151+
/// Allows to rename an enum variant, replacing `_original_variant_name`.
152+
fn enum_variant_name(
153+
&self,
154+
_enum_name: Option<&str>,
155+
_original_variant_name: &str,
156+
_variant_value: EnumVariantValue,
157+
) -> Option<String> {
158+
None
159+
}
160+
161+
/// Allows to rename an item, replacing `_original_item_name`.
162+
fn item_name(&self, _original_item_name: &str) -> Option<String> {
163+
None
164+
}
165+
166+
/// This will be called on every file inclusion, with the full path of the included file.
167+
fn include_file(&self, _filename: &str) {}
168+
169+
/// This will be called to determine whether a particular blocklisted type
170+
/// implements a trait or not. This will be used to implement traits on
171+
/// other types containing the blocklisted type.
172+
///
173+
/// * `None`: use the default behavior
174+
/// * `Some(ImplementsTrait::Yes)`: `_name` implements `_derive_trait`
175+
/// * `Some(ImplementsTrait::Manually)`: any type including `_name` can't
176+
/// derive `_derive_trait` but can implemented it manually
177+
/// * `Some(ImplementsTrait::No)`: `_name` doesn't implement `_derive_trait`
178+
fn blocklisted_type_implements_trait(
179+
&self,
180+
_name: &str,
181+
_derive_trait: DeriveTrait,
182+
) -> Option<ImplementsTrait> {
183+
None
184+
}
185+
186+
/// Provide a list of custom derive attributes.
187+
///
188+
/// If no additional attributes are wanted, this function should return an
189+
/// empty `Vec`.
190+
fn add_derives(&self, _name: &str) -> Vec<String> {
191+
vec![]
192+
}
193+
}
194+
195+
#[cfg(feature = "builder-clone")]
110196
dyn_clone::clone_trait_object!(ParseCallbacks);

src/lib.rs

+4-2
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)