Skip to main content

Bindgen

Struct Bindgen 

Source
pub struct Bindgen { /* private fields */ }
Expand description

Builder for generating Windows API bindings.

This is the fluent alternative to bindgen.

§Example

windows_bindgen::Bindgen::new()
    .output("src/bindings.rs")
    .filter("GetTickCount")
    .write();

Implementations§

Source§

impl Bindgen

Source

pub fn new() -> Self

Creates a new builder with default options.

Source

pub fn input(&mut self, input: impl AsRef<Path>) -> &mut Self

Adds a .winmd file or directory.

Explicit inputs replace the bundled metadata. Call Bindgen::input_default to include the bundled metadata alongside them.

Source

pub fn input_default(&mut self) -> &mut Self

Adds the default Windows metadata.

Included automatically when no explicit inputs are supplied.

Source

pub fn input_bytes(&mut self, input: &[u8]) -> &mut Self

Adds a .winmd file from memory.

Uses the same default metadata behavior as Bindgen::input.

Source

pub fn input_byte_sets<I, B>(&mut self, inputs: I) -> &mut Self
where I: IntoIterator<Item = B>, B: AsRef<[u8]>,

Adds .winmd files from memory.

Source

pub fn inputs<I, S>(&mut self, inputs: I) -> &mut Self
where I: IntoIterator<Item = S>, S: AsRef<Path>,

Adds .winmd files or directories.

Source

pub fn output(&mut self, output: impl AsRef<Path>) -> &mut Self

Sets the generated Rust file.

Defaults to bindings.rs in the runtime OUT_DIR directory. An explicit path is required when OUT_DIR is unset or empty, and always when generating a package.

Source

pub fn filter(&mut self, filter: &str) -> &mut Self

Adds a filter rule to include or exclude APIs.

See the crate documentation for an example table. The same syntax applies to Bindgen::filters, Bindgen::filter_file, and bindgen’s --filter arguments.

§Names and groups

Select a function, constant, type, or namespace. Bare names match across namespaces; qualify a name to restrict it to one namespace. Dots and :: both separate path segments. Namespace selections include descendant namespaces. The bundled Win32 metadata uses Windows.Win32, not the subsystem namespaces used by the umbrella crates’ Cargo features.

Groups abbreviate separate selections and may nest: Windows::Foundation::{Uri, AsyncStatus}. Trailing commas are allowed. * wildcards are not supported.

§Members and dependencies

A bare type selects all its members. Use Type::Member or Type::{Member, OtherMember} for a subset. Members may be methods, properties, events, or enum variants. Use native member or overload names, not renamed Rust methods. Property and event names select their accessor pairs; requesting an event’s add_ method also selects its remove_ method. Class::CreateInstance selects a WinRT class’s default constructor.

Signature dependencies are included automatically, but interfaces reached only through signatures can be name-only declarations. Select their methods explicitly to call them. Type::{} requests the type without requesting its methods or enum variants; it does not remove methods selected elsewhere. In minimal mode, a COM interface selected this way can use Bindgen::implement to emit its implementation trait and vtable without caller wrappers.

§Combining rules

Prefix a selection with ! to exclude it. More specific type/namespace rules take precedence; exclusions win ties regardless of argument order. Member and enum-variant selections narrow whole-type selections, also regardless of order. Excluded members and variants win over included ones, except that implementation traits require the interface’s complete method ABI. Named Bindgen::implement selectors reject method-level inclusions or exclusions on matching interfaces. Select the whole interface or use Interface::{} instead. Bindgen::implement_all allows member filters, but emits the complete method ABI for implemented interfaces.

Chained members (Type::Read::Write), trailing separators (Type::), and empty entries inside a group (Type::{Read,,Write}) are rejected. Use Type::{Read, Write} for several members or Type::{} for a name-only selection.

Source

pub fn filter_file(&mut self, input: impl AsRef<Path>) -> &mut Self

Adds filter rules from a text file.

Uses Bindgen::filter syntax, with whitespace separating filters outside groups. Groups may span lines. Lines beginning with // after optional whitespace are comments; inline comments require a separate // token followed by whitespace or the end of the line.

Source

pub fn filter_files<I, S>(&mut self, inputs: I) -> &mut Self
where I: IntoIterator<Item = S>, S: AsRef<Path>,

Adds filter rules from text files.

Source

pub fn filters<I, S>(&mut self, filters: I) -> &mut Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Add multiple filter rules to include or exclude APIs.

Uses the same syntax as Bindgen::filter.

Source

pub fn derive(&mut self, derive: &str) -> &mut Self

Specifies extra derives as Type=Trait,OtherTrait, for example SYSTEMTIME=Debug.

The type may be a bare or dotted qualified name and must be included in the generated bindings. Later entries for the same type replace earlier lists.

Source

pub fn derives<I, S>(&mut self, derives: I) -> &mut Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Specifies extra derives for multiple types, using the syntax of Bindgen::derive.

Source

pub fn rustfmt(&mut self, rustfmt: &str) -> &mut Self

Set comma-separated rustfmt configuration options, such as max_width=120.

These are passed to rustfmt --config, not used as an executable path. The default is newline_style=Unix; supplied options override matching defaults. Formatting is best effort: failures leave the output unformatted without a warning.

Source

pub fn modules(&mut self) -> &mut Self

Nest items in one Rust module per metadata namespace instead of a single flat list.

Use this when the selected APIs define the same name in more than one namespace.

Source

pub fn package(&mut self) -> &mut Self

Generate bindings as a package with one file per namespace.

Source

pub fn implement_all(&mut self) -> &mut Self

Includes implementation traits for every native and WinRT interface in scope.

Unlike Bindgen::implement, allows method-level filters. Implemented interfaces still emit their complete method ABI.

Source

pub fn implement(&mut self, name: &str) -> &mut Self

Includes implementation traits for a native or WinRT interface or namespace prefix.

Use a dotted qualified name (Windows.Win32.ISequentialStream) or a namespace prefix. Unlike filters, bare interface names are not resolved. The interface must be included in the generated bindings, directly or as a dependency; this does not select it. Required base interface implementations are included unless excluded or supplied by a referenced crate.

Method-level inclusions or exclusions on matching interfaces or their required bases cause Bindgen::write to panic. Select the whole interface or use Interface::{} instead.

Source

pub fn implements<I, S>(&mut self, names: I) -> &mut Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Includes implementation traits for multiple native or WinRT interfaces or namespace prefixes.

Source

pub fn compose(&mut self, name: &str) -> &mut Self

Selects a composable WinRT class as a composition target.

The class and its composable factory methods must also be selected by a filter.

Source

pub fn composes<I, S>(&mut self, names: I) -> &mut Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Selects multiple composable WinRT classes as composition targets.

Source

pub fn sys(&mut self) -> &mut Self

Generate raw or sys-style Rust bindings using windows-link.

Mutually exclusive with Bindgen::full.

Source

pub fn full(&mut self) -> &mut Self

Generate full-fidelity Rust bindings.

Adds per-class wrapper methods, inherited interface forwarders, handle ergonomics, free-function wrappers, and implementation traits for interfaces not marked ExclusiveTo. Use Bindgen::implement or Bindgen::implement_all for ExclusiveTo interfaces; an explicit selection replaces the full-mode default.

Mutually exclusive with Bindgen::sys.

Source

pub fn extern_fns(&mut self) -> &mut Self

Generate extern declarations rather than link! macros in any binding style.

DLL and entry-point selection are left to the caller’s import libraries, including ordinals.

Source

pub fn dead_code(&mut self) -> &mut Self

Use pub(crate) where possible to surface unused bindings as dead-code warnings.

Interface and vtable types remain public for trait implementations.

Source

pub fn write(&self)

Generate the bindings.

Without an explicit output, writes OUT_DIR/bindings.rs. Use distinct explicit paths for multiple output files. Does not emit Cargo dependency-tracking directives.

Resolves declarations on demand, including dependencies of selected APIs. Errors in unused declarations may not be reported.

Panics if input processing, code generation, or writing the output fails.

Trait Implementations§

Source§

impl Default for Bindgen

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Forget for Bindgen

§

impl Freeze for Bindgen

§

impl Move for Bindgen

§

impl RefUnwindSafe for Bindgen

§

impl Send for Bindgen
where RawVecInner: Send,

§

impl Sync for Bindgen
where RawVecInner: Sync,

§

impl Unpin for Bindgen
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for Bindgen

§

impl UnwindSafe for Bindgen
where RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.