wit_bindgen/lib.rs
1//! Bindings generation support for Rust with the Component Model.
2//!
3//! This crate is a bindings generator for [WIT] and the [Component Model].
4//! Users are likely interested in the [`generate!`] macro which actually
5//! generates bindings. Otherwise this crate provides any runtime support
6//! necessary for the macro-generated code.
7//!
8//! [WIT]: https://component-model.bytecodealliance.org/design/wit.html
9//! [Component Model]: https://component-model.bytecodealliance.org/
10
11#![no_std]
12
13/// Generate bindings for an input WIT document.
14///
15/// This macro is the bread-and-butter of the `wit-bindgen` crate. The macro
16/// here will parse [WIT] as input and generate Rust bindings to work with the
17/// `world` that's specified in the [WIT]. For a primer on WIT see [this
18/// documentation][WIT] and for a primer on worlds see [here][worlds].
19///
20/// [WIT]: https://component-model.bytecodealliance.org/design/wit.html
21/// [worlds]: https://component-model.bytecodealliance.org/design/worlds.html
22///
23/// This macro takes as input a [WIT package] as well as a [`world`][worlds]
24/// within that package. It will then generate a Rust function for all `import`s
25/// into the world. If there are any `export`s then a Rust `trait` will be
26/// generated for you to implement. The macro additionally takes a number of
27/// configuration parameters documented below as well.
28///
29/// Basic invocation of the macro can look like:
30///
31/// ```
32/// use wit_bindgen::generate;
33/// # macro_rules! generate { ($($t:tt)*) => () }
34///
35/// generate!();
36/// ```
37///
38/// This will parse a WIT package in the `wit` folder adjacent to your project's
39/// `Cargo.toml` file. Within this WIT package there must be precisely one
40/// `world` and that world will be the one that has bindings generated for it.
41/// All other options remain at their default values (more on this below).
42///
43/// If your WIT package has more than one `world`, or if you want to select a
44/// world from the dependencies, you can specify a world explicitly:
45///
46/// ```
47/// use wit_bindgen::generate;
48/// # macro_rules! generate { ($($t:tt)*) => () }
49///
50/// generate!("my-world");
51/// generate!("wasi:cli/imports");
52/// ```
53///
54/// This form of the macro takes a single string as an argument which is a
55/// "world specifier" to select which world is being generated. As a single
56/// string, such as `"my-world"`, this selects the world named `my-world` in the
57/// package being parsed in the `wit` folder. The longer form specification
58/// `"wasi:cli/imports"` indicates that the `wasi:cli` package, located in the
59/// `wit/deps` folder, will have a world named `imports` and those bindings will
60/// be generated.
61///
62/// If your WIT package is located in a different directory than one called
63/// `wit` then it can be specified with the `in` keyword:
64///
65/// ```
66/// use wit_bindgen::generate;
67/// # macro_rules! generate { ($($t:tt)*) => () }
68///
69/// generate!(in "./my/other/path/to/wit");
70/// generate!("a-world" in "../path/to/wit");
71/// ```
72///
73/// The full-form of the macro, however, takes a braced structure which is a
74/// "bag of options":
75///
76/// ```
77/// use wit_bindgen::generate;
78/// # macro_rules! generate { ($($t:tt)*) => () }
79///
80/// generate!({
81/// world: "my-world",
82/// path: "../path/to/wit",
83/// // ...
84/// });
85/// ```
86///
87/// For documentation on each option, see below.
88///
89/// ## Exploring generated bindings
90///
91/// Once bindings have been generated they can be explored via a number of means
92/// to see what was generated:
93///
94/// * Using `cargo doc` should render all of the generated bindings in addition
95/// to the original comments in the WIT format itself.
96/// * If your IDE supports `rust-analyzer` code completion should be available
97/// to explore and see types.
98/// * The `wit-bindgen` CLI tool, packaged as `wit-bindgen-cli` on crates.io,
99/// can be executed the same as the `generate!` macro and the output can be
100/// read.
101/// * If you're seeing an error, `WIT_BINDGEN_DEBUG=1` can help debug what's
102/// happening (more on this below) by emitting macro output to a file.
103/// * This documentation can be consulted for various constructs as well.
104///
105/// Currently browsing generated code may have road bumps on the way. If you run
106/// into issues or have idea of how to improve the situation please [file an
107/// issue].
108///
109/// [file an issue]: https://github.com/bytecodealliance/wit-bindgen/issues/new
110///
111/// ## Namespacing
112///
113/// In WIT, worlds can import and export `interface`s, functions, and types. Each
114/// `interface` can either be "anonymous" and only named within the context of a
115/// `world` or it can have a "package ID" associated with it. Names in Rust take
116/// into account all the names associated with a WIT `interface`. For example
117/// the package ID `foo:bar/baz` would create a `mod foo` which contains a `mod
118/// bar` which contains a `mod baz`.
119///
120/// WIT imports and exports are additionally separated into their own
121/// namespaces. Imports are generated at the level of the `generate!` macro
122/// where exports are generated under an `exports` namespace.
123///
124/// ## Imports
125///
126/// Imports into a `world` can be types, resources, functions, and interfaces.
127/// Each of these is bound as a Rust type, function, or module. The intent is
128/// that the WIT interfaces map to what is roughly idiomatic Rust for the given
129/// interface.
130///
131/// ### Imports: Top-level functions and types
132///
133/// Imports at the top-level of a world are generated directly where the
134/// `generate!` macro is invoked.
135///
136/// ```
137/// use wit_bindgen::generate;
138///
139/// generate!({
140/// inline: r"
141/// package a:b;
142///
143/// world the-world {
144/// record fahrenheit {
145/// degrees: f32,
146/// }
147///
148/// import what-temperature-is-it: func() -> fahrenheit;
149///
150/// record celsius {
151/// degrees: f32,
152/// }
153///
154/// import convert-to-celsius: func(a: fahrenheit) -> celsius;
155/// }
156/// ",
157/// });
158///
159/// fn test() {
160/// let current_temp = what_temperature_is_it();
161/// println!("current temp in fahrenheit is {}", current_temp.degrees);
162/// let in_celsius: Celsius = convert_to_celsius(current_temp);
163/// println!("current temp in celsius is {}", in_celsius.degrees);
164/// }
165/// ```
166///
167/// ### Imports: Interfaces
168///
169/// Interfaces are placed into submodules where the `generate!` macro is
170/// invoked and are namespaced based on their identifiers.
171///
172/// ```
173/// use wit_bindgen::generate;
174///
175/// generate!({
176/// inline: r"
177/// package my:test;
178///
179/// interface logging {
180/// enum level {
181/// debug,
182/// info,
183/// error,
184/// }
185/// log: func(level: level, msg: string);
186/// }
187///
188/// world the-world {
189/// import logging;
190/// import global-logger: interface {
191/// use logging.{level};
192///
193/// set-current-level: func(level: level);
194/// get-current-level: func() -> level;
195/// }
196/// }
197/// ",
198/// });
199///
200/// // `my` and `test` are from `package my:test;` and `logging` is for the
201/// // interfac name.
202/// use my::test::logging::Level;
203///
204/// fn test() {
205/// let current_level = global_logger::get_current_level();
206/// println!("current logging level is {current_level:?}");
207/// global_logger::set_current_level(Level::Error);
208///
209/// my::test::logging::log(Level::Info, "Hello there!");
210/// }
211/// #
212/// # fn main() {}
213/// ```
214///
215/// ### Imports: Resources
216///
217/// Imported resources generate a type named after the name of the resource.
218/// This type is then used both for borrows as `&T` as well as via ownership as
219/// `T`. Resource methods are bound as methods on the type `T`.
220///
221/// ```
222/// use wit_bindgen::generate;
223///
224/// generate!({
225/// inline: r#"
226/// package my:test;
227///
228/// interface logger {
229/// enum level {
230/// debug,
231/// info,
232/// error,
233/// }
234///
235/// resource logger {
236/// constructor(destination: string);
237/// log: func(level: level, msg: string);
238/// }
239/// }
240///
241/// // Note that while this world does not textually import the above
242/// // `logger` interface it is a transitive dependency via the `use`
243/// // statement so the "elaborated world" imports the logger.
244/// world the-world {
245/// use logger.{logger};
246///
247/// import get-global-logger: func() -> logger;
248/// }
249/// "#,
250/// });
251///
252/// use my::test::logger::Level;
253///
254/// fn test() {
255/// let logger = get_global_logger();
256/// logger.log(Level::Debug, "This is a global message");
257///
258/// let logger2 = Logger::new("/tmp/other.log");
259/// logger2.log(Level::Info, "This is not a global message");
260/// }
261/// #
262/// # fn main() {}
263/// ```
264///
265/// Note in the above example the lack of import of `Logger`. The `use`
266/// statement imported the `Logger` type, an alias of it, from the `logger`
267/// interface into `the-world`. This generated a Rust `type` alias so `Logger`
268/// was available at the top-level.
269///
270/// ## Exports: Basic Usage
271///
272/// A WIT world can not only `import` functionality but can additionally
273/// `export` functionality as well. An `export` represents a contract that the
274/// Rust program must implement to be able to work correctly. The `generate!`
275/// macro's goal is to take care of all the low-level and ABI details for you,
276/// so the end result is that `generate!`, for exports, will generate Rust
277/// `trait`s that you must implement.
278///
279/// A minimal example of this is:
280///
281/// ```
282/// use wit_bindgen::generate;
283///
284/// generate!({
285/// inline: r#"
286/// package my:test;
287///
288/// world my-world {
289/// export hello: func();
290/// }
291/// "#,
292/// });
293///
294/// struct MyComponent;
295///
296/// impl Guest for MyComponent {
297/// fn hello() {}
298/// }
299///
300/// export!(MyComponent);
301/// #
302/// # fn main() {}
303/// ```
304///
305/// Here the `Guest` trait was generated by the `generate!` macro and represents
306/// the functions at the top-level of `my-world`, in this case the function
307/// `hello`. A custom type, here called `MyComponent`, is created and the trait
308/// is implemented for that type.
309///
310/// Additionally a macro is generated by `generate!` (macros generating macros)
311/// called `export!`. The `export!` macro is given a component that implements
312/// the export `trait`s and then it will itself generate all necessary
313/// `#[unsafe(no_mangle)]` functions to implement the ABI required.
314///
315/// ## Exports: Multiple Interfaces
316///
317/// Each `interface` in WIT will generate a `trait` that must be implemented in
318/// addition to the top-level `trait` for the world. All traits are named
319/// `Guest` here and are namespaced appropriately in modules:
320///
321/// ```
322/// use wit_bindgen::generate;
323///
324/// generate!({
325/// inline: r#"
326/// package my:test;
327///
328/// interface a {
329/// func-in-a: func();
330/// second-func-in-a: func();
331/// }
332///
333/// world my-world {
334/// export a;
335/// export b: interface {
336/// func-in-b: func();
337/// }
338/// export c: func();
339/// }
340/// "#,
341/// });
342///
343/// struct MyComponent;
344///
345/// impl Guest for MyComponent {
346/// fn c() {}
347/// }
348///
349/// impl exports::my::test::a::Guest for MyComponent {
350/// fn func_in_a() {}
351/// fn second_func_in_a() {}
352/// }
353///
354/// impl exports::b::Guest for MyComponent {
355/// fn func_in_b() {}
356/// }
357///
358/// export!(MyComponent);
359/// #
360/// # fn main() {}
361/// ```
362///
363/// Here note that there were three `Guest` traits generated for each of the
364/// three groups: two interfaces and one `world`. Also note that traits (and
365/// types) for exports are namespaced in an `exports` module.
366///
367/// Note that when the top-level `world` does not have any exported functions,
368/// or if an interface does not have any functions, then no `trait` is
369/// generated:
370///
371/// ```
372/// use wit_bindgen::generate;
373///
374/// generate!({
375/// inline: r#"
376/// package my:test;
377///
378/// interface a {
379/// type my-type = u32;
380/// }
381///
382/// world my-world {
383/// export b: interface {
384/// use a.{my-type};
385///
386/// foo: func() -> my-type;
387/// }
388/// }
389/// "#,
390/// });
391///
392/// struct MyComponent;
393///
394/// impl exports::b::Guest for MyComponent {
395/// fn foo() -> u32 {
396/// 42
397/// }
398/// }
399///
400/// export!(MyComponent);
401/// #
402/// # fn main() {}
403/// ```
404///
405/// ## Exports: Resources
406///
407/// Exporting a resource is significantly different than importing a resource.
408/// A component defining a resource can create new resources of that type at any
409/// time, for example. Additionally resources can be "dereferenced" into their
410/// underlying values within the component.
411///
412/// Owned resources have a custom type generated and borrowed resources are
413/// generated with a type of the same name suffixed with `Borrow<'_>`, such as
414/// `MyResource` and `MyResourceBorrow<'_>`.
415///
416/// Like `interface`s the methods and functions used with a `resource` are
417/// packaged up into a `trait`.
418///
419/// Specifying a custom resource type is done with an associated type on the
420/// corresponding trait for the resource's containing interface/world:
421///
422/// ```
423/// use wit_bindgen::generate;
424/// use std::cell::{RefCell, Cell};
425///
426/// generate!({
427/// inline: r#"
428/// package my:test;
429///
430/// interface logging {
431/// enum level {
432/// debug,
433/// info,
434/// error,
435/// }
436///
437/// resource logger {
438/// constructor(level: level);
439/// log: func(level: level, msg: string);
440/// level: func() -> level;
441/// set-level: func(level: level);
442/// }
443/// }
444///
445/// world my-world {
446/// export logging;
447/// }
448/// "#,
449/// });
450///
451/// use exports::my::test::logging::{Guest, GuestLogger, Level};
452///
453/// struct MyComponent;
454///
455/// // Note that the `logging` interface has no methods of its own but a trait
456/// // is required to be implemented here to specify the type of `Logger`.
457/// impl Guest for MyComponent {
458/// type Logger = MyLogger;
459/// }
460///
461/// struct MyLogger {
462/// level: Cell<Level>,
463/// contents: RefCell<String>,
464/// }
465///
466/// impl GuestLogger for MyLogger {
467/// fn new(level: Level) -> MyLogger {
468/// MyLogger {
469/// level: Cell::new(level),
470/// contents: RefCell::new(String::new()),
471/// }
472/// }
473///
474/// fn log(&self, level: Level, msg: String) {
475/// if level as u32 <= self.level.get() as u32 {
476/// self.contents.borrow_mut().push_str(&msg);
477/// self.contents.borrow_mut().push_str("\n");
478/// }
479/// }
480///
481/// fn level(&self) -> Level {
482/// self.level.get()
483/// }
484///
485/// fn set_level(&self, level: Level) {
486/// self.level.set(level);
487/// }
488/// }
489///
490/// export!(MyComponent);
491/// #
492/// # fn main() {}
493/// ```
494///
495/// It's important to note that resources in Rust do not get `&mut self` as
496/// methods, but instead are required to be defined with `&self`. This requires
497/// the use of interior mutability such as `Cell` and `RefCell` above from the
498/// `std::cell` module.
499///
500/// ## Exports: The `export!` macro
501///
502/// Components are created by having exported WebAssembly functions with
503/// specific names, and these functions are not created when `generate!` is
504/// invoked. Instead these functions are created afterwards once you've defined
505/// your own type an implemented the various `trait`s for it. The
506/// `#[unsafe(no_mangle)]` functions that will become the component are created
507/// with the generated `export!` macro.
508///
509/// Each call to `generate!` will itself generate a macro called `export!`.
510/// The macro's first argument is the name of a type that implements the traits
511/// generated:
512///
513/// ```
514/// use wit_bindgen::generate;
515///
516/// generate!({
517/// inline: r#"
518/// package my:test;
519///
520/// world my-world {
521/// # export hello: func();
522/// // ...
523/// }
524/// "#,
525/// });
526///
527/// struct MyComponent;
528///
529/// impl Guest for MyComponent {
530/// # fn hello() {}
531/// // ...
532/// }
533///
534/// export!(MyComponent);
535/// #
536/// # fn main() {}
537/// ```
538///
539/// This argument is a Rust type which implements the `Guest` traits generated
540/// by `generate!`. Note that all `Guest` traits must be implemented for the
541/// type provided or an error will be generated.
542///
543/// This macro additionally accepts a second argument. The macro itself needs to
544/// be able to find the module where the `generate!` macro itself was originally
545/// invoked. Currently that can't be done automatically so a path to where
546/// `generate!` was provided can also be passed to the macro. By default, the
547/// argument is set to `self`:
548///
549/// ```
550/// use wit_bindgen::generate;
551///
552/// generate!({
553/// // ...
554/// # inline: r#"
555/// # package my:test;
556/// #
557/// # world my-world {
558/// # export hello: func();
559/// # // ...
560/// # }
561/// # "#,
562/// });
563/// #
564/// # struct MyComponent;
565/// #
566/// # impl Guest for MyComponent {
567/// # fn hello() {}
568/// # // ...
569/// # }
570/// #
571/// export!(MyComponent with_types_in self);
572/// #
573/// # fn main() {}
574/// ```
575///
576/// This indicates that the current module, referred to with `self`, is the one
577/// which had the `generate!` macro expanded.
578///
579/// If, however, the `generate!` macro was run in a different module then that
580/// must be configured:
581///
582/// ```
583/// mod bindings {
584/// wit_bindgen::generate!({
585/// // ...
586/// # inline: r#"
587/// # package my:test;
588/// #
589/// # world my-world {
590/// # export hello: func();
591/// # // ...
592/// # }
593/// # "#,
594/// });
595/// }
596/// #
597/// # struct MyComponent;
598/// #
599/// # impl bindings::Guest for MyComponent {
600/// # fn hello() {}
601/// # // ...
602/// # }
603/// #
604/// bindings::export!(MyComponent with_types_in bindings);
605/// #
606/// # fn main() {}
607/// ```
608///
609/// ## Debugging output to `generate!`
610///
611/// While `wit-bindgen` is tested to the best of our ability there are
612/// inevitably bugs and issues that arise. These can range from bad error
613/// messages to misconfigured invocations to bugs in the macro itself. To assist
614/// with debugging these situations the macro recognizes an environment
615/// variable:
616///
617/// ```shell
618/// export WIT_BINDGEN_DEBUG=1
619/// ```
620///
621/// When set the macro will emit the result of expansion to a file and then
622/// `include!` that file. Any error messages generated by `rustc` should then
623/// point to the generated file and allow you to open it up, read it, and
624/// inspect it. This can often provide better context to the error than rustc
625/// provides by default with macros.
626///
627/// It is not recommended to set this environment variable by default as it will
628/// cause excessive rebuilds of Cargo projects. It's recommended to only use it
629/// as necessary to debug issues.
630///
631/// ## Options to `generate!`
632///
633/// The full list of options that can be passed to the `generate!` macro are as
634/// follows. Note that there are no required options, they all have default
635/// values.
636///
637///
638/// ```
639/// use wit_bindgen::generate;
640/// # macro_rules! generate { ($($t:tt)*) => () }
641///
642/// generate!({
643/// // The name of the world that bindings are being generated for. If this
644/// // is not specified then it's required that the package selected
645/// // below has a single `world` in it.
646/// world: "my-world",
647///
648/// // Path to parse WIT and its dependencies from. Defaults to the `wit`
649/// // folder adjacent to your `Cargo.toml`.
650/// //
651/// // This parameter also supports the form of a list, such as:
652/// // ["../path/to/wit1", "../path/to/wit2"]
653/// // Usually used in testing, our test suite may want to generate code
654/// // from wit files located in multiple paths within a single mod, and we
655/// // don't want to copy these files again.
656/// path: "../path/to/wit",
657///
658/// // Enables passing "inline WIT". If specified this is the default
659/// // package that a world is selected from. Any dependencies that this
660/// // inline WIT refers to must be defined in the `path` option above.
661/// //
662/// // By default this is not specified.
663/// inline: "
664/// world my-world {
665/// import wasi:cli/imports;
666///
667/// export my-run: func()
668/// }
669/// ",
670///
671/// // Additional traits to derive for all defined types. Note that not all
672/// // types may be able to implement these traits, such as resources.
673/// //
674/// // By default this set is empty.
675/// additional_derives: [PartialEq, Eq, Hash, Clone],
676///
677/// // When generating bindings for interfaces that are not defined in the
678/// // same package as `world`, this option can be used to either generate
679/// // those bindings or point to already generated bindings.
680/// // For example, if your world refers to WASI types then the `wasi` crate
681/// // already has generated bindings for all WASI types and structures. In this
682/// // situation the key `with` here can be used to use those types
683/// // elsewhere rather than regenerating types.
684/// // If for example your world refers to some type and you want to use
685/// // your own custom implementation of that type then you can specify
686/// // that here as well. There is a requirement on the remapped (custom)
687/// // type to have the same internal structure and identical to what would
688/// // wit-bindgen generate (including alignment, etc.), since
689/// // lifting/lowering uses its fields directly.
690/// //
691/// // If, however, your world refers to interfaces for which you don't have
692/// // already generated bindings then you can use the special `generate` value
693/// // to have those bindings generated.
694/// //
695/// // The `with` key here works for interfaces and individual types.
696/// //
697/// // When an interface or type is specified here no bindings will be
698/// // generated at all. It's assumed bindings are fully generated
699/// // somewhere else. This is an indicator that any further references to types
700/// // defined in these interfaces should use the upstream paths specified
701/// // here instead.
702/// //
703/// // Any unused keys in this map are considered an error.
704/// with: {
705/// "wasi:io/poll": wasi::io::poll,
706/// "some:package/my-interface": generate,
707/// "some:package/my-interface/my-type": my_crate::types::MyType,
708/// },
709///
710/// // Indicates that all interfaces not present in `with` should be assumed
711/// // to be marked with `generate`.
712/// generate_all,
713///
714/// // An optional list of function names to skip generating bindings for.
715/// // This is only applicable to imports and the name specified is the name
716/// // of the function.
717/// skip: ["foo", "bar", "baz"],
718///
719/// // Configuration of how Rust types are generated.
720/// //
721/// // This option will change how WIT types are mapped to Rust types. There
722/// // are a number of ways this can be done depending on the context. For
723/// // example a Rust `&str` is suitable to pass to an imported function but
724/// // an exported function receives a `String`. These both represent the
725/// // WIT type `string`, however.
726/// //
727/// // Type generation becomes extra-significant when aggregates come into
728/// // play (such as a WIT `record` or `variant`), especially when the
729/// // aggregate is used both in an imported function and exported one.
730/// //
731/// // There are three modes of ownership, documented here, but only one
732/// // can be specified.
733/// //
734/// // The default mode is "Owning" meaning that all Rust types will by
735/// // default contain their owned containers. For example a `record` with
736/// // a `string` will map to a Rust `struct` containing a `String`. This
737/// // maximizes the chance that types can be shared between imports and
738/// // exports but can come at a cost where calling an import may require
739/// // more allocations than necessary.
740/// ownership: Owning,
741///
742/// // Specifies an alternative name for the `export!` macro generated for
743/// // any exports this world has.
744/// //
745/// // Defaults to "export"
746/// export_macro_name: "export",
747///
748/// // Indicates whether the `export!` macro is `pub` or just `pub(crate)`.
749/// //
750/// // This defaults to `false`.
751/// pub_export_macro: false,
752///
753/// // The second mode of ownership is "Borrowing". This mode then
754/// // additionally has a boolean flag indicating whether duplicate types
755/// // should be generated if necessary.
756/// //
757/// // This mode will prefer using borrowed values in Rust to represent WIT
758/// // values where possible. For example if the argument to an imported
759/// // function is a record-with-a-string then in Rust that will generate a
760/// // `struct` with a lifetime parameter storing `&'a str`.
761/// //
762/// // The `duplicate_if_necessary` flag will cause duplicate types to be
763/// // generated when a WIT type is used both in an import and export. In
764/// // this situation one will be called `FooParam` and one will be called
765/// // `FooResult` (where `foo` is the WIT name).
766/// //
767/// // It's generally recommended to not turn this on unless performance
768/// // requires it. Even if so, please feel free to open an issue on the
769/// // `wit-bindgen` repository to help improve the default "Owning" use
770/// // case above if possible.
771/// ownership: Borrowing { duplicate_if_necessary: false },
772///
773/// // The generated `export!` macro, if any, will by default look for
774/// // generated types adjacent to where the `export!` macro is invoked
775/// // through the `self` module. This option can be used to change the
776/// // defaults to look somewhere else instead.
777/// default_bindings_module: "path::to::bindings",
778///
779/// // This will suffix the custom section containing component type
780/// // information with the specified string. This is not required by
781/// // default but if the same world is generated in two different locations
782/// // in the crate then one bindings generation location will need this
783/// // suffix to avoid having the custom sections corrupt each other.
784/// type_section_suffix: "suffix",
785///
786/// // Configures the path to the `wit-bindgen` crate itself. By default
787/// // this is `wit_bindgen` assuming that your crate depends on the
788/// // `wit-bindgen` crate itself.
789/// runtime_path: "path::to::wit_bindgen",
790///
791/// // Configure where the `bitflags` crate is located. By default this
792/// // is `wit_bindgen::bitflags` which already reexports `bitflags` for
793/// // you.
794/// bitflags_path: "path::to::bitflags",
795///
796/// // Indicates that instead of `&str` and `String` the `&[u8]` and
797/// // `Vec<u8>` types should be used. Only intended for cases where
798/// // compiled size is of the utmost concern as this can avoid pulling in
799/// // UTF-8 validation.
800/// raw_strings,
801///
802/// // Emits `#[cfg(feature = "std")]` around `impl Error for ... {}` blocks
803/// // for generated types. This is a niche option that is only here to
804/// // support the standard library itself depending on this crate one day.
805/// std_feature,
806///
807/// // Disable a workaround to force wasm constructors to be run only once
808/// // when exported functions are called.
809/// disable_run_ctors_once_workaround: false,
810///
811/// // Whether to generate unused `record`, `enum`, `variant` types.
812/// // By default, they will not be generated unless they are used as input
813/// // or return value of a function.
814/// generate_unused_types: false,
815///
816/// // A list of "features" which correspond to WIT features to activate
817/// // when parsing WIT files. This enables `@unstable` annotations showing
818/// // up and having bindings generated for them.
819/// //
820/// // By default this is an empty list.
821/// features: ["foo", "bar", "baz"],
822///
823/// // Disables generation of a `#[used]` static to try harder to get the
824/// // custom section describing WIT types linked into the binary when
825/// // used in library-like situations. This is `false` by default with
826/// // `#[used]` statics being emitted.
827/// disable_custom_section_link_helpers: false,
828///
829/// // Write generated code to a .rs file, which allows the compiler to
830/// // emit more useful diagnostics for errors in the generated code. This
831/// // is primarily useful for `wit-bindgen` developers.
832/// //
833/// // This does the same thing as setting `WIT_BINDGEN_DEBUG=1`, except
834/// // that it can be used on a more fine-grained basis (i.e. it only affects
835/// // the specific `generate!` call where it is used.
836/// debug: true,
837///
838/// // Generate async import and/or export bindings.
839/// //
840/// // The resulting bindings will use the component model
841/// // [async ABI](https://github.com/WebAssembly/component-model/blob/main/design/mvp/Async.md).
842/// // This may be specified either as a boolean (e.g. `async: true`, meaning
843/// // all imports and exports should use the async ABI) or as lists of
844/// // specific imports and/or exports as shown here:
845/// async: {
846/// imports: [
847/// "wasi:http/types@0.3.0-draft#[static]body.finish",
848/// "wasi:http/handler@0.3.0-draft#handle",
849/// ],
850/// exports: [
851/// "wasi:http/handler@0.3.0-draft#handle",
852/// ]
853/// }
854/// });
855/// ```
856///
857/// [WIT package]: https://component-model.bytecodealliance.org/design/packages.html
858#[cfg(feature = "macros")]
859pub use wit_bindgen_rust_macro::generate;
860
861// This re-export is no longer needed in new bindings and is only
862// here for compatibility.
863#[doc(hidden)]
864pub use rt::bitflags;
865
866mod pre_wit_bindgen_0_20_0;
867
868#[cfg(docsrs)]
869pub mod examples;
870
871#[doc(hidden)]
872pub mod rt {
873 // Re-export `bitflags` so that we can reference it from macros.
874 pub use wit_bindgen_rt::bitflags;
875
876 #[cfg(target_arch = "wasm32")]
877 pub use wit_bindgen_rt::run_ctors_once;
878
879 pub fn maybe_link_cabi_realloc() {
880 #[cfg(feature = "realloc")]
881 wit_bindgen_rt::maybe_link_cabi_realloc();
882 }
883
884 #[cfg(all(feature = "realloc", not(target_env = "p2")))]
885 pub use wit_bindgen_rt::cabi_realloc;
886
887 #[cfg(feature = "async")]
888 pub use wit_bindgen_rt::async_support;
889
890 pub use crate::pre_wit_bindgen_0_20_0::*;
891}