Skip to main content

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