pub struct Builder { /* private fields */ }
Expand description
Configure and generate Rust bindings for a C/C++ header.
This is the main entry point to the library.
use bindgen::builder;
// Configure and generate bindings.
let bindings = builder().header("path/to/input/header")
.allowlist_type("SomeCoolClass")
.allowlist_function("do_some_cool_thing")
.generate()?;
// Write the generated bindings to an output file.
bindings.write_to_file("path/to/output.rs")?;
§Enums
Bindgen can map C/C++ enums into Rust in different ways. The way bindgen maps enums depends on the pattern passed to several methods:
constified_enum_module()
bitfield_enum()
newtype_enum()
rustified_enum()
rustified_non_exhaustive_enum()
For each C enum, bindgen tries to match the pattern in the following order:
- Constified enum module
- Bitfield enum
- Newtype enum
- Rustified enum
If none of the above patterns match, then bindgen will generate a set of Rust constants.
§Clang arguments
Extra arguments can be passed to with clang:
clang_arg()
: takes a single argumentclang_args()
: takes an iterator of argumentsBINDGEN_EXTRA_CLANG_ARGS
environment variable: whitespace separate environment variable of arguments
Clang arguments specific to your crate should be added via the
clang_arg()
/clang_args()
methods.
End-users of the crate may need to set the BINDGEN_EXTRA_CLANG_ARGS
environment variable to
add additional arguments. For example, to build against a different sysroot a user could set
BINDGEN_EXTRA_CLANG_ARGS
to --sysroot=/path/to/sysroot
.
§Regular expression arguments
Some Builder
methods, such as allowlist_*
and blocklist_*
, allow regular
expressions as arguments. These regular expressions will be enclosed in parentheses and
anchored with ^
and $
. So, if the argument passed is <regex>
, the regular expression to be
stored will be ^(<regex>)$
.
As a consequence, regular expressions passed to bindgen
will try to match the whole name of
an item instead of a section of it, which means that to match any items with the prefix
prefix
, the prefix.*
regular expression must be used.
Certain methods, like Builder::allowlist_function
, use regular expressions over function
names. To match C++ methods, prefix the name of the type where they belong, followed by an
underscore. So, if the type Foo
has a method bar
, it can be matched with the Foo_bar
regular expression.
Additionally, Objective-C interfaces can be matched by prefixing the regular expression with
I
. For example, the IFoo
regular expression matches the Foo
interface, and the IFoo_foo
regular expression matches the foo
method of the Foo
interface.
Releases of bindgen
with a version lesser or equal to 0.62.0
used to accept the wildcard
pattern *
as a valid regular expression. This behavior has been deprecated, and the .*
regular expression must be used instead.
Implementations§
Source§impl Builder
impl Builder
Sourcepub fn command_line_flags(&self) -> Vec<String>
pub fn command_line_flags(&self) -> Vec<String>
Generates the command line flags used to create this Builder
.
Sourcepub fn blocklist_type<T>(self, arg: T) -> Builder
pub fn blocklist_type<T>(self, arg: T) -> Builder
Do not generate any bindings for the given type.
This option is not recursive, meaning that it will only block types whose names explicitly match the argument of this method.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn blocklist_function<T>(self, arg: T) -> Builder
pub fn blocklist_function<T>(self, arg: T) -> Builder
Do not generate any bindings for the given function.
This option is not recursive, meaning that it will only block functions whose names explicitly match the argument of this method.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn blocklist_item<T>(self, arg: T) -> Builder
pub fn blocklist_item<T>(self, arg: T) -> Builder
Do not generate any bindings for the given item, regardless of whether it is a type, function, module, etc.
This option is not recursive, meaning that it will only block items whose names explicitly match the argument of this method.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn blocklist_file<T>(self, arg: T) -> Builder
pub fn blocklist_file<T>(self, arg: T) -> Builder
Do not generate any bindings for the contents of the given file, regardless of whether the contents of the file are types, functions, modules, etc.
This option is not recursive, meaning that it will only block files whose names explicitly match the argument of this method.
This method will use the argument to match the complete path of the file instead of a section of it.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn blocklist_var<T>(self, arg: T) -> Builder
pub fn blocklist_var<T>(self, arg: T) -> Builder
Do not generate any bindings for the given variable.
This option is not recursive, meaning that it will only block variables whose names explicitly match the argument of this method.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn opaque_type<T>(self, arg: T) -> Builder
pub fn opaque_type<T>(self, arg: T) -> Builder
Treat the given type as opaque in the generated bindings.
Opaque in this context means that none of the generated bindings will contain information about the inner representation of the type and the type itself will be represented as a chunk of bytes with the alignment and size of the type.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn with_rustfmt<P>(self, path: P) -> Builder
pub fn with_rustfmt<P>(self, path: P) -> Builder
Set an explicit path to the rustfmt
binary.
This option only comes into effect if rustfmt
is set to be the formatter used by
bindgen
. Check the documentation of the Builder::formatter
method for more
information.
Sourcepub fn depfile<H, D>(self, output_module: H, depfile: D) -> Builder
pub fn depfile<H, D>(self, output_module: H, depfile: D) -> Builder
Add a depfile output which will be written alongside the generated bindings.
Sourcepub fn allowlist_type<T>(self, arg: T) -> Builder
pub fn allowlist_type<T>(self, arg: T) -> Builder
Generate bindings for the given type.
This option is transitive by default. Check the documentation of the
Builder::allowlist_recursively
method for further information.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn allowlist_function<T>(self, arg: T) -> Builder
pub fn allowlist_function<T>(self, arg: T) -> Builder
Generate bindings for the given function.
This option is transitive by default. Check the documentation of the
Builder::allowlist_recursively
method for further information.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn allowlist_var<T>(self, arg: T) -> Builder
pub fn allowlist_var<T>(self, arg: T) -> Builder
Generate bindings for the given variable.
This option is transitive by default. Check the documentation of the
Builder::allowlist_recursively
method for further information.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn allowlist_file<T>(self, arg: T) -> Builder
pub fn allowlist_file<T>(self, arg: T) -> Builder
Generate bindings for the content of the given file.
This option is transitive by default. Check the documentation of the
Builder::allowlist_recursively
method for further information.
This method will use the argument to match the complete path of the file instead of a section of it.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn allowlist_item<T>(self, arg: T) -> Builder
pub fn allowlist_item<T>(self, arg: T) -> Builder
Generate bindings for the given item, regardless of whether it is a type, function, module, etc.
This option is transitive by default. Check the documentation of the
Builder::allowlist_recursively
method for further information.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn default_enum_style(self, arg: EnumVariation) -> Builder
pub fn default_enum_style(self, arg: EnumVariation) -> Builder
Set the default style for generated enum
s.
If this method is not called, the EnumVariation::Consts
style will be used by
default.
To set the style for individual enum
s, use Builder::bitfield_enum
,
Builder::newtype_enum
, Builder::newtype_global_enum
,
Builder::rustified_enum
, Builder::rustified_non_exhaustive_enum
,
Builder::constified_enum_module
or Builder::constified_enum
.
Sourcepub fn bitfield_enum<T>(self, arg: T) -> Builder
pub fn bitfield_enum<T>(self, arg: T) -> Builder
Mark the given enum
as being bitfield-like.
This is similar to the Builder::newtype_enum
style, but with the bitwise
operators implemented.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn newtype_enum<T>(self, arg: T) -> Builder
pub fn newtype_enum<T>(self, arg: T) -> Builder
Mark the given enum
as a newtype.
This means that an integer newtype will be declared to represent the enum
type and its variants will be represented as constants inside of this type’s
impl
block.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn newtype_global_enum<T>(self, arg: T) -> Builder
pub fn newtype_global_enum<T>(self, arg: T) -> Builder
Mark the given enum
as a global newtype.
This is similar to the Builder::newtype_enum
style, but the constants for
each variant are free constants instead of being declared inside an impl
block for the newtype.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn rustified_enum<T>(self, arg: T) -> Builder
pub fn rustified_enum<T>(self, arg: T) -> Builder
Mark the given enum
as a Rust enum
.
This means that each variant of the enum
will be represented as a Rust enum
variant.
Use this with caution, creating an instance of a Rust enum
with an
invalid value will cause undefined behaviour. To avoid this, use the
Builder::newtype_enum
style instead.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn rustified_non_exhaustive_enum<T>(self, arg: T) -> Builder
pub fn rustified_non_exhaustive_enum<T>(self, arg: T) -> Builder
Mark the given enum
as a non-exhaustive Rust enum
.
This is similar to the Builder::rustified_enum
style, but the enum
is
tagged with the #[non_exhaustive]
attribute.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn constified_enum_module<T>(self, arg: T) -> Builder
pub fn constified_enum_module<T>(self, arg: T) -> Builder
Mark the given enum
as a module with a set of integer constants.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn constified_enum<T>(self, arg: T) -> Builder
pub fn constified_enum<T>(self, arg: T) -> Builder
Mark the given enum
as a set of integer constants.
This is similar to the Builder::constified_enum_module
style, but the
constants are generated in the current module instead of in a new module.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn default_macro_constant_type(self, arg: MacroTypeVariation) -> Builder
pub fn default_macro_constant_type(self, arg: MacroTypeVariation) -> Builder
Set the default type signedness to be used for macro constants.
If this method is not called, MacroTypeVariation::Unsigned
is used by default.
To set the type for individual macro constants, use the
ParseCallbacks::int_macro
method.
Sourcepub fn default_alias_style(self, arg: AliasVariation) -> Builder
pub fn default_alias_style(self, arg: AliasVariation) -> Builder
Set the default style of code generation for typedef
s.
If this method is not called, the AliasVariation::TypeAlias
style is used by
default.
To set the style for individual typedefs
s, use Builder::type_alias
,
Builder::new_type_alias
or Builder::new_type_alias_deref
.
Sourcepub fn type_alias<T>(self, arg: T) -> Builder
pub fn type_alias<T>(self, arg: T) -> Builder
Mark the given typedef
as a regular Rust type
alias.
This is the default behavior, meaning that this method only comes into effect
if a style different from AliasVariation::TypeAlias
was passed to the
Builder::default_alias_style
method.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn new_type_alias<T>(self, arg: T) -> Builder
pub fn new_type_alias<T>(self, arg: T) -> Builder
Mark the given typedef
as a Rust newtype by having the aliased
type be wrapped in a struct
with #[repr(transparent)]
.
This method can be used to enforce stricter type checking.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn new_type_alias_deref<T>(self, arg: T) -> Builder
pub fn new_type_alias_deref<T>(self, arg: T) -> Builder
Mark the given typedef
to be generated as a newtype that can be dereferenced.
This is similar to the Builder::new_type_alias
style, but the newtype
implements Deref
and DerefMut
with the aliased type as a target.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn default_non_copy_union_style(self, arg: NonCopyUnionStyle) -> Builder
pub fn default_non_copy_union_style(self, arg: NonCopyUnionStyle) -> Builder
Set the default style of code to generate for union
s with non-Copy
members.
If this method is not called, the NonCopyUnionStyle::BindgenWrapper
style is
used by default.
To set the style for individual union
s, use Builder::bindgen_wrapper_union
or
Builder::manually_drop_union
.
Sourcepub fn bindgen_wrapper_union<T>(self, arg: T) -> Builder
pub fn bindgen_wrapper_union<T>(self, arg: T) -> Builder
Mark the given union
to use a bindgen
-generated wrapper for its members if at
least one them is not Copy
.
This is the default behavior, meaning that this method only comes into effect
if a style different from NonCopyUnionStyle::BindgenWrapper
was passed to
the Builder::default_non_copy_union_style
method.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn manually_drop_union<T>(self, arg: T) -> Builder
pub fn manually_drop_union<T>(self, arg: T) -> Builder
Mark the given union
to use ::core::mem::ManuallyDrop
for its members if
at least one of them is not Copy
.
The ManuallyDrop
type was stabilized in Rust 1.20.0, do not use this option
if your target version is lower than this.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn emit_builtins(self) -> Builder
pub fn emit_builtins(self) -> Builder
Generate Rust bindings for built-in definitions (for example __builtin_va_list
).
Bindings for built-in definitions are not emitted by default.
Sourcepub fn emit_clang_ast(self) -> Builder
pub fn emit_clang_ast(self) -> Builder
Emit the Clang AST to stdout
for debugging purposes.
The Clang AST is not emitted by default.
Sourcepub fn emit_ir(self) -> Builder
pub fn emit_ir(self) -> Builder
Emit the bindgen
internal representation to stdout
for debugging purposes.
This internal representation is not emitted by default.
Sourcepub fn emit_ir_graphviz<T>(self, path: T) -> Builder
pub fn emit_ir_graphviz<T>(self, path: T) -> Builder
Set the path for the file where thebindgen
internal representation will be
emitted as a graph using the graphviz
DOT language.
This graph representation is not emitted by default.
Sourcepub fn enable_cxx_namespaces(self) -> Builder
pub fn enable_cxx_namespaces(self) -> Builder
Emulate C++ namespaces using Rust modules in the generated bindings.
C++ namespaces are not emulated by default.
Sourcepub fn enable_function_attribute_detection(self) -> Builder
pub fn enable_function_attribute_detection(self) -> Builder
Enable detecting function attributes on C functions.
This enables the following features:
- Add
#[must_use]
attributes to Rust items whose C counterparts are marked as so. This feature also requires that the Rust target version supports the attribute. - Set
!
as the return type for Rust functions whose C counterparts are marked as diverging.
This option can be quite slow in some cases (check #1465), so it is disabled by default.
Sourcepub fn disable_name_namespacing(self) -> Builder
pub fn disable_name_namespacing(self) -> Builder
Disable name auto-namespacing.
By default, bindgen
mangles names like foo::bar::Baz
to look like foo_bar_Baz
instead of just Baz
. This method disables that behavior.
Note that this does not change the names used for allowlisting and blocklisting,
which should still be mangled with the namespaces. Additionally, this option may
cause bindgen
to generate duplicate names.
Sourcepub fn disable_nested_struct_naming(self) -> Builder
pub fn disable_nested_struct_naming(self) -> Builder
Disable nested struct
naming.
The following struct
s have different names for C and C++. In C, they are visible
as foo
and bar
. In C++, they are visible as foo
and foo::bar
.
struct foo {
struct bar {
} b;
};
bindgen
tries to avoid duplicate names by default, so it follows the C++ naming
convention and it generates foo
and foo_bar
instead of just foo
and bar
.
This method disables this behavior and it is indented to be used only for headers that were written in C.
Sourcepub fn disable_header_comment(self) -> Builder
pub fn disable_header_comment(self) -> Builder
Do not insert the bindgen
version identifier into the generated bindings.
This identifier is inserted by default.
Sourcepub fn layout_tests(self, doit: bool) -> Builder
pub fn layout_tests(self, doit: bool) -> Builder
Set whether layout tests should be generated.
Layout tests are generated by default.
Sourcepub fn impl_debug(self, doit: bool) -> Builder
pub fn impl_debug(self, doit: bool) -> Builder
Set whether Debug
should be implemented for types that cannot derive it.
This option is disabled by default.
Sourcepub fn impl_partialeq(self, doit: bool) -> Builder
pub fn impl_partialeq(self, doit: bool) -> Builder
Set whether PartialEq
should be implemented for types that cannot derive it.
This option is disabled by default.
Sourcepub fn derive_copy(self, doit: bool) -> Builder
pub fn derive_copy(self, doit: bool) -> Builder
Set whether the Copy
trait should be derived when possible.
Copy
is derived by default.
Sourcepub fn derive_debug(self, doit: bool) -> Builder
pub fn derive_debug(self, doit: bool) -> Builder
Set whether the Debug
trait should be derived when possible.
The Builder::impl_debug
method can be used to implement Debug
for types that
cannot derive it.
Debug
is derived by default.
Sourcepub fn derive_default(self, doit: bool) -> Builder
pub fn derive_default(self, doit: bool) -> Builder
Set whether the Default
trait should be derived when possible.
Default
is not derived by default.
Sourcepub fn derive_hash(self, doit: bool) -> Builder
pub fn derive_hash(self, doit: bool) -> Builder
Set whether the Hash
trait should be derived when possible.
Hash
is not derived by default.
Sourcepub fn derive_partialord(self, doit: bool) -> Builder
pub fn derive_partialord(self, doit: bool) -> Builder
Set whether the PartialOrd
trait should be derived when possible.
Take into account that Ord
cannot be derived for a type that does not implement
PartialOrd
. For this reason, setting this method to false
also sets
automatically Builder::derive_ord
to false
.
PartialOrd
is not derived by default.
Sourcepub fn derive_ord(self, doit: bool) -> Builder
pub fn derive_ord(self, doit: bool) -> Builder
Set whether the Ord
trait should be derived when possible.
Take into account that Ord
cannot be derived for a type that does not implement
PartialOrd
. For this reason, the value set with this method will also be set
automatically for Builder::derive_partialord
.
Ord
is not derived by default.
Sourcepub fn derive_partialeq(self, doit: bool) -> Builder
pub fn derive_partialeq(self, doit: bool) -> Builder
Set whether the PartialEq
trait should be derived when possible.
Take into account that Eq
cannot be derived for a type that does not implement
PartialEq
. For this reason, setting this method to false
also sets
automatically Builder::derive_eq
to false
.
The Builder::impl_partialeq
method can be used to implement PartialEq
for
types that cannot derive it.
PartialEq
is not derived by default.
Sourcepub fn derive_eq(self, doit: bool) -> Builder
pub fn derive_eq(self, doit: bool) -> Builder
Set whether the Eq
trait should be derived when possible.
Take into account that Eq
cannot be derived for a type that does not implement
PartialEq
. For this reason, the value set with this method will also be set
automatically for Builder::derive_partialeq
.
Eq
is not derived by default.
Sourcepub fn use_core(self) -> Builder
pub fn use_core(self) -> Builder
Use core
instead of std
in the generated bindings.
std
is used by default.
Sourcepub fn ctypes_prefix<T>(self, prefix: T) -> Builder
pub fn ctypes_prefix<T>(self, prefix: T) -> Builder
Use the given prefix for the C platform-specific types instead of ::std::os::raw
.
Alternatively, the Builder::use_core
method can be used to set the prefix to
::core::ffi
or ::core::os::raw
.
Sourcepub fn anon_fields_prefix<T>(self, prefix: T) -> Builder
pub fn anon_fields_prefix<T>(self, prefix: T) -> Builder
Use the given prefix for the anonymous fields.
An anonymous field, is a field of a C/C++ type that does not have a name. For example, in the following C code:
struct integer {
struct {
int inner;
};
}
The only field of the integer
struct
is an anonymous field and its Rust
representation will be named using this prefix followed by an integer identifier.
The default prefix is __bindgen_anon_
.
Sourcepub fn time_phases(self, doit: bool) -> Builder
pub fn time_phases(self, doit: bool) -> Builder
Set whether to measure the elapsed time for each one of the bindgen
phases. This
information is printed to stderr
.
The elapsed time is not measured by default.
Sourcepub fn no_convert_floats(self) -> Builder
pub fn no_convert_floats(self) -> Builder
Avoid converting C float types to f32
and f64
.
Sourcepub fn raw_line<T>(self, arg: T) -> Builder
pub fn raw_line<T>(self, arg: T) -> Builder
Add a line of Rust code at the beginning of the generated bindings. The string is passed through without any modification.
Sourcepub fn module_raw_line<T, U>(self, module: T, line: U) -> Builder
pub fn module_raw_line<T, U>(self, module: T, line: U) -> Builder
Add a given line to the beginning of a given module.
This option only comes into effect if the Builder::enable_cxx_namespaces
method
is also being called.
Sourcepub fn header<T>(self, header: T) -> Builder
pub fn header<T>(self, header: T) -> Builder
Add an input C/C++ header to generate bindings for.
This can be used to generate bindings for a single header:
let bindings = bindgen::Builder::default()
.header("input.h")
.generate()
.unwrap();
Or for multiple headers:
let bindings = bindgen::Builder::default()
.header("first.h")
.header("second.h")
.header("third.h")
.generate()
.unwrap();
Sourcepub fn headers<I>(self, headers: I) -> Builder
pub fn headers<I>(self, headers: I) -> Builder
Add input C/C++ header(s) to generate bindings for.
This can be used to generate bindings for a single header:
let bindings = bindgen::Builder::default()
.headers(["input.h"])
.generate()
.unwrap();
Or for multiple headers:
let bindings = bindgen::Builder::default()
.headers(["first.h", "second.h", "third.h"])
.generate()
.unwrap();
Sourcepub fn clang_arg<T>(self, arg: T) -> Builder
pub fn clang_arg<T>(self, arg: T) -> Builder
Add an argument to be passed straight through to Clang.
Sourcepub fn clang_args<I>(self, args: I) -> Builder
pub fn clang_args<I>(self, args: I) -> Builder
Add several arguments to be passed straight through to Clang.
Sourcepub fn header_contents(self, name: &str, contents: &str) -> Builder
pub fn header_contents(self, name: &str, contents: &str) -> Builder
Add contents
as an input C/C++ header named name
.
This can be used to inject additional C/C++ code as an input without having to create additional header files.
Sourcepub fn parse_callbacks(self, cb: Box<dyn ParseCallbacks>) -> Builder
pub fn parse_callbacks(self, cb: Box<dyn ParseCallbacks>) -> Builder
Add a new ParseCallbacks
instance to configure types in different situations.
This can also be used with CargoCallbacks
to emit
cargo:rerun-if-changed=...
for all #include
d header files.
Sourcepub fn ignore_functions(self) -> Builder
pub fn ignore_functions(self) -> Builder
Do not generate any functions.
Functions are generated by default.
Sourcepub fn ignore_methods(self) -> Builder
pub fn ignore_methods(self) -> Builder
Do not generate any methods.
Methods are generated by default.
Sourcepub fn with_codegen_config(self, config: CodegenConfig) -> Builder
pub fn with_codegen_config(self, config: CodegenConfig) -> Builder
Choose what to generate using a CodegenConfig
.
This option overlaps with Builder::ignore_functions
and
Builder::ignore_methods
.
All the items in CodegenConfig
are generated by default.
Sourcepub fn conservative_inline_namespaces(self) -> Builder
pub fn conservative_inline_namespaces(self) -> Builder
Treat inline namespaces conservatively.
This is tricky, because in C++ is technically legal to override an item defined in an inline namespace:
inline namespace foo {
using Bar = int;
}
using Bar = long;
Even though referencing Bar
is a compiler error.
We want to support this (arguably esoteric) use case, but we do not want to make
the rest of bindgen
users pay an usability penalty for that.
To support this, we need to keep all the inline namespaces around, but then using
bindgen
becomes a bit more difficult, because you cannot reference paths like
std::string
(you’d need to use the proper inline namespace).
We could complicate a lot of the logic to detect name collisions and, in the
absence of collisions, generate a pub use inline_ns::*
or something like that.
That is probably something we can do to improve the usability of this option if we realize it is needed way more often. Our guess is that this extra logic is not going to be very useful.
This option is disabled by default.
Sourcepub fn generate_comments(self, doit: bool) -> Builder
pub fn generate_comments(self, doit: bool) -> Builder
Set whether the generated bindings should contain documentation comments.
Documentation comments are included by default.
Note that clang excludes comments from system headers by default, pass
"-fretain-comments-from-system-headers"
to the Builder::clang_arg
method to
include them.
It is also possible to process all comments and not just documentation using the
"-fparse-all-comments"
flag. Check these slides on clang comment parsing for more information
and examples.
Sourcepub fn generate_inline_functions(self, doit: bool) -> Builder
pub fn generate_inline_functions(self, doit: bool) -> Builder
Set whether to generate inline functions.
This option is disabled by default.
Note that they will usually not work. However you can use -fkeep-inline-functions
or -fno-inline-functions
if you are responsible of compiling the library to make
them callable.
Check the Builder::wrap_static_fns
method for an alternative.
Sourcepub fn allowlist_recursively(self, doit: bool) -> Builder
pub fn allowlist_recursively(self, doit: bool) -> Builder
Set whether to recursively allowlist items.
Items are allowlisted recursively by default.
Given that we have explicitly allowlisted the initiate_dance_party
function in
this C header:
typedef struct MoonBoots {
int bouncy_level;
} MoonBoots;
void initiate_dance_party(MoonBoots* boots);
We would normally generate bindings to both the initiate_dance_party
function and
the MoonBoots
type that it transitively references. If false
is passed to this
method, bindgen
will not emit bindings for anything except the explicitly
allowlisted items, meaning that the definition for MoonBoots
would not be
generated. However, the initiate_dance_party
function would still reference
MoonBoots
!
Disabling this feature will almost certainly cause bindgen
to emit bindings
that will not compile! If you disable this feature, then it is your
responsibility to provide definitions for every type that is referenced from an
explicitly allowlisted item. One way to provide the missing definitions is by using
the Builder::raw_line
method, another would be to define them in Rust and then
include!(...)
the bindings immediately afterwards.
Sourcepub fn objc_extern_crate(self, doit: bool) -> Builder
pub fn objc_extern_crate(self, doit: bool) -> Builder
Emit #[macro_use] extern crate objc;
instead of use objc;
in the prologue of
the files generated from objective-c files.
use objc;
is emitted by default.
Sourcepub fn generate_block(self, doit: bool) -> Builder
pub fn generate_block(self, doit: bool) -> Builder
Generate proper block signatures instead of void
pointers.
void
pointers are used by default.
Sourcepub fn generate_cstr(self, doit: bool) -> Builder
pub fn generate_cstr(self, doit: bool) -> Builder
Set whether string constants should be generated as &CStr
instead of &[u8]
.
A minimum Rust target of 1.59 is required for this to have any effect as support
for CStr::from_bytes_with_nul_unchecked
in const
contexts is needed.
This option is disabled by default but will become enabled by default in a future release, so enabling this is recommended.
Sourcepub fn block_extern_crate(self, doit: bool) -> Builder
pub fn block_extern_crate(self, doit: bool) -> Builder
Emit #[macro_use] extern crate block;
instead of use block;
in the prologue of
the files generated from apple block files.
use block;
is emitted by default.
Sourcepub fn trust_clang_mangling(self, doit: bool) -> Builder
pub fn trust_clang_mangling(self, doit: bool) -> Builder
Set whether to use the clang-provided name mangling. This is probably needed for C++ features.
The mangling provided by clang is used by default.
We allow disabling this option because some old libclang
versions seem to return
incorrect results in some cases for non-mangled functions, check #528 for more
information.
Sourcepub fn detect_include_paths(self, doit: bool) -> Builder
pub fn detect_include_paths(self, doit: bool) -> Builder
Set whether to detect include paths using clang_sys
.
clang_sys
is used to detect include paths by default.
Sourcepub fn fit_macro_constants(self, doit: bool) -> Builder
pub fn fit_macro_constants(self, doit: bool) -> Builder
Set whether bindgen
should try to fit macro constants into types smaller than u32
and i32
.
This option is disabled by default.
Sourcepub fn prepend_enum_name(self, doit: bool) -> Builder
pub fn prepend_enum_name(self, doit: bool) -> Builder
Set whether to prepend the enum
name to constant or newtype variants.
The enum
name is prepended by default.
Sourcepub fn rust_target(self, rust_target: RustTarget) -> Builder
pub fn rust_target(self, rust_target: RustTarget) -> Builder
Specify the Rust target version.
The default target is the latest stable Rust version.
Sourcepub fn rust_edition(self, rust_edition: RustEdition) -> Builder
pub fn rust_edition(self, rust_edition: RustEdition) -> Builder
Specify the Rust target edition.
The default edition is the latest edition supported by the chosen Rust target.
Sourcepub fn disable_untagged_union(self) -> Builder
pub fn disable_untagged_union(self) -> Builder
Disable support for native Rust unions, if supported.
The default value of this option is set based on the value passed to
Builder::rust_target
.
Sourcepub fn record_matches(self, doit: bool) -> Builder
pub fn record_matches(self, doit: bool) -> Builder
Set whether we should record which items in our regex sets did match any C items.
Matches are recorded by default.
Sourcepub fn size_t_is_usize(self, is: bool) -> Builder
pub fn size_t_is_usize(self, is: bool) -> Builder
Set whether size_t
should be translated to usize
.
If size_t
is translated to usize
, type definitions for size_t
will not be
emitted.
size_t
is translated to usize
by default.
Sourcepub fn rustfmt_bindings(self, doit: bool) -> Builder
👎Deprecated
pub fn rustfmt_bindings(self, doit: bool) -> Builder
Set whether rustfmt
should be used to format the generated bindings.
rustfmt
is used by default.
This method overlaps in functionality with the more general Builder::formatter
.
Thus, the latter should be preferred.
Sourcepub fn formatter(self, formatter: Formatter) -> Builder
pub fn formatter(self, formatter: Formatter) -> Builder
Set which tool should be used to format the generated bindings.
The default formatter is Formatter::Rustfmt
.
To be able to use prettyplease
as a formatter, the "prettyplease"
feature for
bindgen
must be enabled in the Cargo manifest.
Sourcepub fn rustfmt_configuration_file(self, path: Option<PathBuf>) -> Builder
pub fn rustfmt_configuration_file(self, path: Option<PathBuf>) -> Builder
Set the absolute path to the rustfmt
configuration file.
The default rustfmt
options are used if None
is passed to this method or if
this method is not called at all.
Calling this method will set the Builder::rustfmt_bindings
option to true
and the Builder::formatter
option to Formatter::Rustfmt
.
Sourcepub fn no_partialeq<T>(self, arg: T) -> Builder
pub fn no_partialeq<T>(self, arg: T) -> Builder
Do not derive PartialEq
for a given type.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn no_copy<T>(self, arg: T) -> Builder
pub fn no_copy<T>(self, arg: T) -> Builder
Do not derive Copy
and Clone
for a given type.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn no_debug<T>(self, arg: T) -> Builder
pub fn no_debug<T>(self, arg: T) -> Builder
Do not derive Debug
for a given type.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn no_default<T>(self, arg: T) -> Builder
pub fn no_default<T>(self, arg: T) -> Builder
Do not derive or implement Default
for a given type.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn no_hash<T>(self, arg: T) -> Builder
pub fn no_hash<T>(self, arg: T) -> Builder
Do not derive Hash
for a given type.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn must_use_type<T>(self, arg: T) -> Builder
pub fn must_use_type<T>(self, arg: T) -> Builder
Annotate the given type with the #[must_use]
attribute.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn array_pointers_in_arguments(self, doit: bool) -> Builder
pub fn array_pointers_in_arguments(self, doit: bool) -> Builder
Translate arrays T arr[size]
into array pointers *mut [T; size]
instead of
translating them as *mut T
which is the default.
The same is done for *const
pointers.
Sourcepub fn wasm_import_module_name<T>(self, import_name: T) -> Builder
pub fn wasm_import_module_name<T>(self, import_name: T) -> Builder
Adds the #[link(wasm_import_module = import_name)]
attribute to all the extern
blocks generated by bindgen
.
This attribute is not added by default.
Sourcepub fn dynamic_library_name<T>(self, dynamic_library_name: T) -> Builder
pub fn dynamic_library_name<T>(self, dynamic_library_name: T) -> Builder
Generate bindings for a shared library with the given name.
This option is disabled by default.
Sourcepub fn dynamic_link_require_all(self, req: bool) -> Builder
pub fn dynamic_link_require_all(self, req: bool) -> Builder
Set whether to require successful linkage for all routines in a shared library. This allows us to optimize function calls by being able to safely assume function pointers are valid.
This option only comes into effect if the Builder::dynamic_library_name
option
is set.
This option is disabled by default.
Sourcepub fn respect_cxx_access_specs(self, doit: bool) -> Builder
pub fn respect_cxx_access_specs(self, doit: bool) -> Builder
Set whether to respect the C++ access specifications.
Passing true
to this method will set the visibility of the generated Rust items
as pub
only if the corresponding C++ items are publicly accessible instead of
marking all the items as public, which is the default.
Sourcepub fn translate_enum_integer_types(self, doit: bool) -> Builder
pub fn translate_enum_integer_types(self, doit: bool) -> Builder
Set whether to always translate enum
integer types to native Rust integer types.
Passing true
to this method will result in enum
s having types such as u32
and
i16
instead of c_uint
and c_short
which is the default. The #[repr]
types
of Rust enum
s are always translated to Rust integer types.
Sourcepub fn c_naming(self, doit: bool) -> Builder
pub fn c_naming(self, doit: bool) -> Builder
Set whether to generate types with C style naming.
Passing true
to this method will add prefixes to the generated type names. For
example, instead of a struct
with name A
we will generate a struct
with
struct_A
. Currently applies to struct
s, union
s, and enum
s.
Sourcepub fn explicit_padding(self, doit: bool) -> Builder
pub fn explicit_padding(self, doit: bool) -> Builder
Set whether to always emit explicit padding fields.
This option should be enabled if a struct
needs to be serialized in its native
format (padding bytes and all). This could be required if such struct
will be
written to a file or sent over the network, as anything reading the padding bytes
of a struct may cause undefined behavior.
Padding fields are not emitted by default.
Sourcepub fn vtable_generation(self, doit: bool) -> Builder
pub fn vtable_generation(self, doit: bool) -> Builder
Set whether to enable experimental support to generate virtual table functions.
This option should mostly work, though some edge cases are likely to be broken.
Virtual table generation is disabled by default.
Sourcepub fn sort_semantically(self, doit: bool) -> Builder
pub fn sort_semantically(self, doit: bool) -> Builder
Set whether to sort the generated Rust items in a predefined manner.
Items are not ordered by default.
Sourcepub fn merge_extern_blocks(self, doit: bool) -> Builder
pub fn merge_extern_blocks(self, doit: bool) -> Builder
Merge all extern blocks under the same module into a single one.
Extern blocks are not merged by default.
Sourcepub fn wrap_unsafe_ops(self, doit: bool) -> Builder
pub fn wrap_unsafe_ops(self, doit: bool) -> Builder
Wrap all unsafe operations in unsafe blocks.
Unsafe operations are not wrapped by default.
Sourcepub fn flexarray_dst(self, doit: bool) -> Builder
pub fn flexarray_dst(self, doit: bool) -> Builder
Use DSTs to represent structures with flexible array members.
This option is disabled by default.
Sourcepub fn override_abi<T>(self, abi: Abi, arg: T) -> Builder
pub fn override_abi<T>(self, abi: Abi, arg: T) -> Builder
Override the ABI of a given function.
Regular expressions are supported. Check the regular expression arguments section and the regex crate documentation for further information.
Sourcepub fn wrap_static_fns(self, doit: bool) -> Builder
pub fn wrap_static_fns(self, doit: bool) -> Builder
Set whether to generate wrappers for `static`` functions.
Passing true
to this method will generate a C source file with non-static
functions that call the static
functions found in the input headers and can be
called from Rust once the source file is compiled.
The path of this source file can be set using the Builder::wrap_static_fns_path
method.
Sourcepub fn wrap_static_fns_suffix<T>(self, suffix: T) -> Builder
pub fn wrap_static_fns_suffix<T>(self, suffix: T) -> Builder
Set the suffix added to the wrappers for static
functions.
This option only comes into effect if true
is passed to the
Builder::wrap_static_fns
method.
The default suffix is __extern
.
Sourcepub fn wrap_static_fns_path<T>(self, path: T) -> Builder
pub fn wrap_static_fns_path<T>(self, path: T) -> Builder
Set the path for the source code file that would be created if any wrapper
functions must be generated due to the presence of static
functions.
bindgen
will automatically add the right extension to the header and source code
files.
This option only comes into effect if true
is passed to the
Builder::wrap_static_fns
method.
The default path is temp_dir/bindgen/extern
, where temp_dir
is the path
returned by std::env::temp_dir
.
Sourcepub fn default_visibility(self, visibility: FieldVisibilityKind) -> Builder
pub fn default_visibility(self, visibility: FieldVisibilityKind) -> Builder
Set the default visibility of fields, including bitfields and accessor methods for bitfields.
This option only comes into effect if the Builder::respect_cxx_access_specs
option is disabled.
Sourcepub fn clang_macro_fallback(self) -> Builder
pub fn clang_macro_fallback(self) -> Builder
Use Clang as a fallback for macros that fail to parse using CExpr
.
This uses a workaround to evaluate each macro in a temporary file. Because this results in slower compilation, this option is opt-in.
Sourcepub fn clang_macro_fallback_build_dir<P>(self, path: P) -> Builder
pub fn clang_macro_fallback_build_dir<P>(self, path: P) -> Builder
Set a path to a directory to which .c
and .h.pch
files should be written for the
purpose of using clang to evaluate macros that can’t be easily parsed.
The default location for .h.pch
files is the directory that the corresponding
.h
file is located in. The default for the temporary .c
file used for clang
parsing is the current working directory. Both of these defaults are overridden
by this option.
Source§impl Builder
impl Builder
Sourcepub fn generate(self) -> Result<Bindings, BindgenError>
pub fn generate(self) -> Result<Bindings, BindgenError>
Generate the Rust bindings using the options built up thus far.
Sourcepub fn dump_preprocessed_input(&self) -> Result<(), Error>
pub fn dump_preprocessed_input(&self) -> Result<(), Error>
Preprocess and dump the input header files to disk.
This is useful when debugging bindgen, using C-Reduce, or when filing
issues. The resulting file will be named something like __bindgen.i
or
__bindgen.ii
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Builder
impl !RefUnwindSafe for Builder
impl !Send for Builder
impl !Sync for Builder
impl Unpin for Builder
impl !UnwindSafe for Builder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more