1 //! # Embedding API for the Component Model
2 //!
3 //! This module contains the embedding API for the [Component Model] in
4 //! Wasmtime. This module requires the `component-model` feature to be enabled,
5 //! which is enabled by default. The embedding API here is mirrored after the
6 //! core wasm embedding API at the crate root and is intended to have the same
7 //! look-and-feel while handling concepts of the component model.
8 //!
9 //! [Component Model]: https://component-model.bytecodealliance.org
10 //!
11 //! The component model is a broad topic which can't be explained here fully, so
12 //! it's recommended to read over individual items' documentation to see more
13 //! about the capabilities of the embedding API. At a high-level, however,
14 //! perhaps the most interesting items in this module are:
15 //!
16 //! * [`Component`] - a compiled component ready to be instantiated. Similar to
17 //!   a [`Module`](crate::Module) for core wasm.
18 //!
19 //! * [`Linker`] - a component-style location for defining host functions. This
20 //!   is not the same as [`wasmtime::Linker`](crate::Linker) for core wasm
21 //!   modules.
22 //!
23 //! * [`bindgen!`] - a macro to generate Rust bindings for a [WIT] [world]. This
24 //!   maps all WIT types into Rust automatically and generates traits for
25 //!   embedders to implement.
26 //!
27 //! [WIT]: https://component-model.bytecodealliance.org/design/wit.html
28 //! [world]: https://component-model.bytecodealliance.org/design/worlds.html
29 //!
30 //! Embedders of the component model will typically start by defining their API
31 //! in [WIT]. This describes what will be available to guests and what needs to
32 //! be provided to the embedder by the guest. This [`world`][world] that was
33 //! created is then fed into [`bindgen!`] to generate types and traits for the
34 //! embedder to use. The embedder then implements these traits, adds
35 //! functionality via the generated `add_to_linker` method (see [`bindgen!`] for
36 //! more info), and then instantiates/executes a component.
37 //!
38 //! It's recommended to read over the [documentation for the Component
39 //! Model][Component Model] to get an overview about how to build components
40 //! from various languages.
41 //!
42 //! ## Example Usage
43 //!
44 //! Imagine you have the following WIT package definition in a file called world.wit
45 //! along with a component (my_component.wasm) that targets `my-world`:
46 //!
47 //! ```text,ignore
48 //! package component:my-package;
49 //!
50 //! world my-world {
51 //!     import name: func() -> string;
52 //!     export greet: func() -> string;
53 //! }
54 //! ```
55 //!
56 //! You can instantiate and call the component like so:
57 //!
58 //! ```
59 //! fn main() -> wasmtime::Result<()> {
60 //!     #   if true { return Ok(()) }
61 //!     // Instantiate the engine and store
62 //!     let engine = wasmtime::Engine::default();
63 //!     let mut store = wasmtime::Store::new(&engine, ());
64 //!
65 //!     // Load the component from disk
66 //!     let bytes = std::fs::read("my_component.wasm")?;
67 //!     let component = wasmtime::component::Component::new(&engine, bytes)?;
68 //!
69 //!     // Configure the linker
70 //!     let mut linker = wasmtime::component::Linker::new(&engine);
71 //!     // The component expects one import `name` that
72 //!     // takes no params and returns a string
73 //!     linker
74 //!         .root()
75 //!         .func_wrap("name", |_store, _params: ()| {
76 //!             Ok((String::from("Alice"),))
77 //!         })?;
78 //!
79 //!     // Instantiate the component
80 //!     let instance = linker.instantiate(&mut store, &component)?;
81 //!
82 //!     // Call the `greet` function
83 //!     let func = instance.get_func(&mut store, "greet").expect("greet export not found");
84 //!     let mut result = [wasmtime::component::Val::String("".into())];
85 //!     func.call(&mut store, &[], &mut result)?;
86 //!
87 //!     // This should print out `Greeting: [String("Hello, Alice!")]`
88 //!     println!("Greeting: {:?}", result);
89 //!
90 //!     Ok(())
91 //! }
92 //! ```
93 //!
94 //! Manually configuring the linker and calling untyped component exports is
95 //! a bit tedious and error prone. The [`bindgen!`] macro can be used to
96 //! generate bindings eliminating much of this boilerplate.
97 //!
98 //! See the docs for [`bindgen!`] for more information on how to use it.
99 
100 // rustdoc appears to lie about a warning above, so squelch it for now.
101 #![allow(rustdoc::redundant_explicit_links)]
102 
103 mod component;
104 mod func;
105 mod instance;
106 mod linker;
107 mod matching;
108 mod resource_table;
109 mod resources;
110 mod storage;
111 mod store;
112 pub mod types;
113 mod values;
114 pub use self::component::{Component, ComponentExportIndex};
115 pub use self::func::{
116     ComponentNamedList, ComponentType, Func, Lift, Lower, TypedFunc, WasmList, WasmStr,
117 };
118 pub use self::instance::{Instance, InstanceExportLookup, InstancePre};
119 pub use self::linker::{Linker, LinkerInstance};
120 pub use self::resource_table::{ResourceTable, ResourceTableError};
121 pub use self::resources::{Resource, ResourceAny};
122 pub use self::types::{ResourceType, Type};
123 pub use self::values::Val;
124 
125 pub(crate) use self::resources::HostResourceData;
126 
127 // These items are expected to be used by an eventual
128 // `#[derive(ComponentType)]`, they are not part of Wasmtime's API stability
129 // guarantees
130 #[doc(hidden)]
131 pub mod __internal {
132     pub use super::func::{
133         bad_type_info, format_flags, lower_payload, typecheck_enum, typecheck_flags,
134         typecheck_record, typecheck_variant, ComponentVariant, LiftContext, LowerContext, Options,
135     };
136     pub use super::matching::InstanceType;
137     pub use crate::map_maybe_uninit;
138     pub use crate::store::StoreOpaque;
139     pub use crate::MaybeUninitExt;
140     pub use alloc::boxed::Box;
141     pub use alloc::string::String;
142     pub use alloc::vec::Vec;
143     pub use anyhow;
144     #[cfg(feature = "async")]
145     pub use async_trait::async_trait;
146     pub use core::mem::transmute;
147     pub use wasmtime_environ;
148     pub use wasmtime_environ::component::{CanonicalAbiInfo, ComponentTypes, InterfaceType};
149 }
150 
151 pub(crate) use self::store::ComponentStoreData;
152 
153 /// Generate bindings for a [WIT world].
154 ///
155 /// [WIT world]: https://component-model.bytecodealliance.org/design/worlds.html
156 /// [WIT package]: https://component-model.bytecodealliance.org/design/packages.html
157 ///
158 /// This macro ingests a [WIT world] and will generate all the necessary
159 /// bindings for instantiating components that ascribe to the `world`. This
160 /// provides a higher-level representation of working with a component than the
161 /// raw [`Instance`] type which must be manually-type-checked and manually have
162 /// its imports provided via the [`Linker`] type.
163 ///
164 /// # Examples
165 ///
166 /// Examples for this macro can be found in the [`bindgen_examples`] module
167 /// documentation. That module has a submodule-per-example which includes the
168 /// source code, with WIT, used to generate the structures along with the
169 /// generated code itself in documentation.
170 ///
171 /// # Debugging and Exploring
172 ///
173 /// If you need to debug the output of `bindgen!` you can try using the
174 /// `WASMTIME_DEBUG_BINDGEN=1` environment variable. This will write the
175 /// generated code to a file on disk so rustc can produce better error messages
176 /// against the actual generated source instead of the macro invocation itself.
177 /// This additionally can enable opening up the generated code in an editor and
178 /// exploring it (through an error message).
179 ///
180 /// The generated bindings can additionally be explored with `cargo doc` to see
181 /// what's generated. It's also recommended to browse the [`bindgen_examples`]
182 /// for example generated structures and example generated code.
183 ///
184 /// # Syntax
185 ///
186 /// This procedural macro accepts a few different syntaxes. The primary purpose
187 /// of this macro is to locate a WIT package, parse it, and then extract a
188 /// `world` from the parsed package. There are then codegen-specific options to
189 /// the bindings themselves which can additionally be specified.
190 ///
191 /// Usage of this macro looks like:
192 ///
193 /// ```rust
194 /// # macro_rules! bindgen { ($($t:tt)*) => () }
195 /// // Parse the `wit/` folder adjacent to this crate's `Cargo.toml` and look
196 /// // for a single `world` in it. There must be exactly one for this to
197 /// // succeed.
198 /// bindgen!();
199 ///
200 /// // Parse the `wit/` folder adjacent to this crate's `Cargo.toml` and look
201 /// // for the world `foo` contained in it.
202 /// bindgen!("foo");
203 ///
204 /// // Parse the folder `other/wit/folder` adjacent to `Cargo.toml`.
205 /// bindgen!(in "other/wit/folder");
206 /// bindgen!("foo" in "other/wit/folder");
207 ///
208 /// // Parse the file `foo.wit` as a single-file WIT package with no
209 /// // dependencies.
210 /// bindgen!("foo" in "foo.wit");
211 ///
212 /// // Specify a suite of options to the bindings generation, documented below
213 /// bindgen!({
214 ///     world: "foo",
215 ///     path: "other/path/to/wit",
216 ///     // ...
217 /// });
218 /// ```
219 ///
220 /// # Options Reference
221 ///
222 /// This is an example listing of all options that this macro supports along
223 /// with documentation for each option and example syntax for each option.
224 ///
225 /// ```rust
226 /// # macro_rules! bindgen { ($($t:tt)*) => () }
227 /// bindgen!({
228 ///     world: "foo", // not needed if `path` has one `world`
229 ///
230 ///     // same as in `bindgen!(in "other/wit/folder")
231 ///     path: "other/wit/folder",
232 ///
233 ///     // Instead of `path` the WIT document can be provided inline if
234 ///     // desired.
235 ///     inline: "
236 ///         package my:inline;
237 ///
238 ///         world foo {
239 ///             // ...
240 ///         }
241 ///     ",
242 ///
243 ///     // Add calls to `tracing::span!` before each import or export is called
244 ///     // to log most arguments and return values. By default values
245 ///     // containing lists are excluded; enable `verbose_tracing` to include
246 ///     // them.
247 ///     //
248 ///     // This option defaults to `false`.
249 ///     tracing: true,
250 ///
251 ///     // Include all arguments and return values in the tracing output,
252 ///     // including values containing lists, which may be very large.
253 ///     //
254 ///     // This option defaults to `false`.
255 ///     verbose_tracing: false,
256 ///
257 ///     // Imports will be async functions through #[async_trait] and exports
258 ///     // are also invoked as async functions. Requires `Config::async_support`
259 ///     // to be `true`.
260 ///     //
261 ///     // Note that this is only async for the host as the guest will still
262 ///     // appear as if it's invoking blocking functions.
263 ///     //
264 ///     // This option defaults to `false`.
265 ///     async: true,
266 ///
267 ///     // Alternative mode of async configuration where this still implies
268 ///     // async instantiation happens, for example, but more control is
269 ///     // provided over which imports are async and which aren't.
270 ///     //
271 ///     // Note that in this mode all exports are still async.
272 ///     async: {
273 ///         // All imports are async except for functions with these names
274 ///         except_imports: ["foo", "bar"],
275 ///
276 ///         // All imports are synchronous except for functions with these names
277 ///         //
278 ///         // Note that this key cannot be specified with `except_imports`,
279 ///         // only one or the other is accepted.
280 ///         only_imports: ["foo", "bar"],
281 ///     },
282 ///
283 ///     // This option is used to indicate whether imports can trap.
284 ///     //
285 ///     // Imports that may trap have their return types wrapped in
286 ///     // `wasmtime::Result<T>` where the `Err` variant indicates that a
287 ///     // trap will be raised in the guest.
288 ///     //
289 ///     // By default imports cannot trap and the return value is the return
290 ///     // value from the WIT bindings itself. This value can be set to `true`
291 ///     // to indicate that any import can trap. This value can also be set to
292 ///     // an array-of-strings to indicate that only a set list of imports
293 ///     // can trap.
294 ///     trappable_imports: false,             // no imports can trap (default)
295 ///     // trappable_imports: true,           // all imports can trap
296 ///     // trappable_imports: ["foo", "bar"], // only these can trap
297 ///
298 ///     // This can be used to translate WIT return values of the form
299 ///     // `result<T, error-type>` into `Result<T, RustErrorType>` in Rust.
300 ///     // Users must define `RustErrorType` and the `Host` trait for the
301 ///     // interface which defines `error-type` will have a method
302 ///     // called `convert_error_type` which converts `RustErrorType`
303 ///     // into `wasmtime::Result<ErrorType>`. This conversion can either
304 ///     // return the raw WIT error (`ErrorType` here) or a trap.
305 ///     //
306 ///     // By default this option is not specified. This option only takes
307 ///     // effect when `trappable_imports` is set for some imports.
308 ///     trappable_error_type: {
309 ///         "wasi:io/streams/stream-error" => RustErrorType,
310 ///     },
311 ///
312 ///     // All generated bindgen types are "owned" meaning types like `String`
313 ///     // are used instead of `&str`, for example. This is the default and
314 ///     // ensures that the same type used in both imports and exports uses the
315 ///     // same generated type.
316 ///     ownership: Owning,
317 ///
318 ///     // Alternative to `Owning` above where borrowed types attempt to be used
319 ///     // instead. The `duplicate_if_necessary` configures whether duplicate
320 ///     // Rust types will be generated for the same WIT type if necessary, for
321 ///     // example when a type is used both as an import and an export.
322 ///     ownership: Borrowing {
323 ///         duplicate_if_necessary: true
324 ///     },
325 ///
326 ///     // Restrict the code generated to what's needed for the interface
327 ///     // imports in the inlined WIT document fragment.
328 ///     interfaces: "
329 ///         import wasi:cli/command;
330 ///     ",
331 ///
332 ///     // Remap imported interfaces or resources to types defined in Rust
333 ///     // elsewhere. Using this option will prevent any code from being
334 ///     // generated for interfaces mentioned here. Resources named here will
335 ///     // not have a type generated to represent the resource.
336 ///     //
337 ///     // Interfaces mapped with this option should be previously generated
338 ///     // with an invocation of this macro. Resources need to be mapped to a
339 ///     // Rust type name.
340 ///     with: {
341 ///         // This can be used to indicate that entire interfaces have
342 ///         // bindings generated elsewhere with a path pointing to the
343 ///         // bindinges-generated module.
344 ///         "wasi:random/random": wasmtime_wasi::bindings::random::random,
345 ///
346 ///         // Similarly entire packages can also be specified.
347 ///         "wasi:cli": wasmtime_wasi::bindings::cli,
348 ///
349 ///         // Or, if applicable, entire namespaces can additionally be mapped.
350 ///         "wasi": wasmtime_wasi::bindings,
351 ///
352 ///         // Versions are supported if multiple versions are in play:
353 ///         "wasi:http/types@0.2.0": wasmtime_wasi_http::bindings::http::types,
354 ///         "wasi:[email protected]": wasmtime_wasi_http::bindings::http,
355 ///
356 ///         // The `with` key can also be used to specify the `T` used in
357 ///         // import bindings of `Resource<T>`. This can be done to configure
358 ///         // which typed resource shows up in generated bindings and can be
359 ///         // useful when working with the typed methods of `ResourceTable`.
360 ///         "wasi:filesystem/types/descriptor": MyDescriptorType,
361 ///     },
362 ///
363 ///     // Additional derive attributes to include on generated types (structs or enums).
364 ///     //
365 ///     // These are deduplicated and attached in a deterministic order.
366 ///     additional_derives: [
367 ///         Hash,
368 ///         serde::Deserialize,
369 ///         serde::Serialize,
370 ///     ],
371 ///
372 ///     // A list of WIT "features" to enable when parsing the WIT document that
373 ///     // this bindgen macro matches. WIT features are all disabled by default
374 ///     // and must be opted-in-to if source level features are used.
375 ///     //
376 ///     // This option defaults to an empty array.
377 ///     features: ["foo", "bar", "baz"],
378 ///
379 ///     // An niche configuration option to require that the `T` in `Store<T>`
380 ///     // is always `Send` in the generated bindings. Typically not needed
381 ///     // but if synchronous bindings depend on asynchronous bindings using
382 ///     // the `with` key then this may be required.
383 ///     require_store_data_send: false,
384 ///
385 ///     // If the `wasmtime` crate is depended on at a nonstandard location
386 ///     // or is renamed then this is the path to the root of the `wasmtime`
387 ///     // crate. Much of the generated code needs to refer to `wasmtime` so
388 ///     // this should be used if the `wasmtime` name is not wasmtime itself.
389 ///     //
390 ///     // By default this is `wasmtime`.
391 ///     wasmtime_crate: path::to::wasmtime,
392 ///
393 ///     // This is an in-source alternative to using `WASMTIME_DEBUG_BINDGEN`.
394 ///     //
395 ///     // Note that if this option is specified then the compiler will always
396 ///     // recompile your bindings. Cargo records the start time of when rustc
397 ///     // is spawned by this will write a file during compilation. To Cargo
398 ///     // that looks like a file was modified after `rustc` was spawned,
399 ///     // so Cargo will always think your project is "dirty" and thus always
400 ///     // recompile it. Recompiling will then overwrite the file again,
401 ///     // starting the cycle anew. This is only recommended for debugging.
402 ///     //
403 ///     // This option defaults to false.
404 ///     include_generated_code_from_file: false,
405 /// });
406 /// ```
407 pub use wasmtime_component_macro::bindgen;
408 
409 /// Derive macro to generate implementations of the [`ComponentType`] trait.
410 ///
411 /// This derive macro can be applied to `struct` and `enum` definitions and is
412 /// used to bind either a `record`, `enum`, or `variant` in the component model.
413 ///
414 /// Note you might be looking for [`bindgen!`] rather than this macro as that
415 /// will generate the entire type for you rather than just a trait
416 /// implementation.
417 ///
418 /// This macro supports a `#[component]` attribute which is used to customize
419 /// how the type is bound to the component model. A top-level `#[component]`
420 /// attribute is required to specify either `record`, `enum`, or `variant`.
421 ///
422 /// ## Records
423 ///
424 /// `record`s in the component model correspond to `struct`s in Rust. An example
425 /// is:
426 ///
427 /// ```rust
428 /// use wasmtime::component::ComponentType;
429 ///
430 /// #[derive(ComponentType)]
431 /// #[component(record)]
432 /// struct Color {
433 ///     r: u8,
434 ///     g: u8,
435 ///     b: u8,
436 /// }
437 /// ```
438 ///
439 /// which corresponds to the WIT type:
440 ///
441 /// ```wit
442 /// record color {
443 ///     r: u8,
444 ///     g: u8,
445 ///     b: u8,
446 /// }
447 /// ```
448 ///
449 /// Note that the name `Color` here does not need to match the name in WIT.
450 /// That's purely used as a name in Rust of what to refer to. The field names
451 /// must match that in WIT, however. Field names can be customized with the
452 /// `#[component]` attribute though.
453 ///
454 /// ```rust
455 /// use wasmtime::component::ComponentType;
456 ///
457 /// #[derive(ComponentType)]
458 /// #[component(record)]
459 /// struct VerboseColor {
460 ///     #[component(name = "r")]
461 ///     red: u8,
462 ///     #[component(name = "g")]
463 ///     green: u8,
464 ///     #[component(name = "b")]
465 ///     blue: u8,
466 /// }
467 /// ```
468 ///
469 /// Also note that field ordering is significant at this time and must match
470 /// WIT.
471 ///
472 /// ## Variants
473 ///
474 /// `variant`s in the component model correspond to a subset of shapes of a Rust
475 /// `enum`. Variants in the component model have a single optional payload type
476 /// which means that not all Rust `enum`s correspond to component model
477 /// `variant`s. An example variant is:
478 ///
479 /// ```rust
480 /// use wasmtime::component::ComponentType;
481 ///
482 /// #[derive(ComponentType)]
483 /// #[component(variant)]
484 /// enum Filter {
485 ///     #[component(name = "none")]
486 ///     None,
487 ///     #[component(name = "all")]
488 ///     All,
489 ///     #[component(name = "some")]
490 ///     Some(Vec<String>),
491 /// }
492 /// ```
493 ///
494 /// which corresponds to the WIT type:
495 ///
496 /// ```wit
497 /// variant filter {
498 ///     none,
499 ///     all,
500 ///     some(list<string>),
501 /// }
502 /// ```
503 ///
504 /// The `variant` style of derive allows an optional payload on Rust `enum`
505 /// variants but it must be a single unnamed field. Variants of the form `Foo(T,
506 /// U)` or `Foo { name: T }` are not supported at this time.
507 ///
508 /// Note that the order of variants in Rust must match the order of variants in
509 /// WIT. Additionally it's likely that `#[component(name = "...")]` is required
510 /// on all Rust `enum` variants because the name currently defaults to the Rust
511 /// name which is typically UpperCamelCase whereas WIT uses kebab-case.
512 ///
513 /// ## Enums
514 ///
515 /// `enum`s in the component model correspond to C-like `enum`s in Rust. Note
516 /// that a component model `enum` does not allow any payloads so the Rust `enum`
517 /// must additionally have no payloads.
518 ///
519 /// ```rust
520 /// use wasmtime::component::ComponentType;
521 ///
522 /// #[derive(ComponentType)]
523 /// #[component(enum)]
524 /// #[repr(u8)]
525 /// enum Setting {
526 ///     #[component(name = "yes")]
527 ///     Yes,
528 ///     #[component(name = "no")]
529 ///     No,
530 ///     #[component(name = "auto")]
531 ///     Auto,
532 /// }
533 /// ```
534 ///
535 /// which corresponds to the WIT type:
536 ///
537 /// ```wit
538 /// enum setting {
539 ///     yes,
540 ///     no,
541 ///     auto,
542 /// }
543 /// ```
544 ///
545 /// Note that the order of variants in Rust must match the order of variants in
546 /// WIT. Additionally it's likely that `#[component(name = "...")]` is required
547 /// on all Rust `enum` variants because the name currently defaults to the Rust
548 /// name which is typically UpperCamelCase whereas WIT uses kebab-case.
549 pub use wasmtime_component_macro::ComponentType;
550 
551 /// A derive macro for generating implementations of the [`Lift`] trait.
552 ///
553 /// This macro will likely be applied in conjunction with the
554 /// [`#[derive(ComponentType)]`](macro@ComponentType) macro along the lines
555 /// of `#[derive(ComponentType, Lift)]`. This trait enables reading values from
556 /// WebAssembly.
557 ///
558 /// Note you might be looking for [`bindgen!`] rather than this macro as that
559 /// will generate the entire type for you rather than just a trait
560 /// implementation.
561 ///
562 /// At this time this derive macro has no configuration.
563 ///
564 /// ## Examples
565 ///
566 /// ```rust
567 /// use wasmtime::component::{ComponentType, Lift};
568 ///
569 /// #[derive(ComponentType, Lift)]
570 /// #[component(record)]
571 /// struct Color {
572 ///     r: u8,
573 ///     g: u8,
574 ///     b: u8,
575 /// }
576 /// ```
577 pub use wasmtime_component_macro::Lift;
578 
579 /// A derive macro for generating implementations of the [`Lower`] trait.
580 ///
581 /// This macro will likely be applied in conjunction with the
582 /// [`#[derive(ComponentType)]`](macro@ComponentType) macro along the lines
583 /// of `#[derive(ComponentType, Lower)]`. This trait enables passing values to
584 /// WebAssembly.
585 ///
586 /// Note you might be looking for [`bindgen!`] rather than this macro as that
587 /// will generate the entire type for you rather than just a trait
588 /// implementation.
589 ///
590 /// At this time this derive macro has no configuration.
591 ///
592 /// ## Examples
593 ///
594 /// ```rust
595 /// use wasmtime::component::{ComponentType, Lower};
596 ///
597 /// #[derive(ComponentType, Lower)]
598 /// #[component(record)]
599 /// struct Color {
600 ///     r: u8,
601 ///     g: u8,
602 ///     b: u8,
603 /// }
604 /// ```
605 pub use wasmtime_component_macro::Lower;
606 
607 /// A macro to generate a Rust type corresponding to WIT `flags`
608 ///
609 /// This macro generates a type that implements the [`ComponentType`], [`Lift`],
610 /// and [`Lower`] traits. The generated Rust type corresponds to the `flags`
611 /// type in WIT.
612 ///
613 /// Example usage of this looks like:
614 ///
615 /// ```rust
616 /// use wasmtime::component::flags;
617 ///
618 /// flags! {
619 ///     Permissions {
620 ///         #[component(name = "read")]
621 ///         const READ;
622 ///         #[component(name = "write")]
623 ///         const WRITE;
624 ///         #[component(name = "execute")]
625 ///         const EXECUTE;
626 ///     }
627 /// }
628 ///
629 /// fn validate_permissions(permissions: &mut Permissions) {
630 ///     if permissions.contains(Permissions::EXECUTE | Permissions::WRITE) {
631 ///         panic!("cannot enable both writable and executable at the same time");
632 ///     }
633 ///
634 ///     if permissions.contains(Permissions::READ) {
635 ///         panic!("permissions must at least contain read");
636 ///     }
637 /// }
638 /// ```
639 ///
640 /// which corresponds to the WIT type:
641 ///
642 /// ```wit
643 /// flags permissions {
644 ///     read,
645 ///     write,
646 ///     execute,
647 /// }
648 /// ```
649 ///
650 /// This generates a structure which is similar to/inspired by the [`bitflags`
651 /// crate](https://crates.io/crates/bitflags). The `Permissions` structure
652 /// generated implements the [`PartialEq`], [`Eq`], [`Debug`], [`BitOr`],
653 /// [`BitOrAssign`], [`BitAnd`], [`BitAndAssign`], [`BitXor`], [`BitXorAssign`],
654 /// and [`Not`] traits - in addition to the Wasmtime-specific component ones
655 /// [`ComponentType`], [`Lift`], and [`Lower`].
656 ///
657 /// [`BitOr`]: std::ops::BitOr
658 /// [`BitOrAssign`]: std::ops::BitOrAssign
659 /// [`BitAnd`]: std::ops::BitAnd
660 /// [`BitAndAssign`]: std::ops::BitAndAssign
661 /// [`BitXor`]: std::ops::BitXor
662 /// [`BitXorAssign`]: std::ops::BitXorAssign
663 /// [`Not`]: std::ops::Not
664 pub use wasmtime_component_macro::flags;
665 
666 #[cfg(any(docsrs, test, doctest))]
667 pub mod bindgen_examples;
668 
669 // NB: needed for the links in the docs above to work in all `cargo doc`
670 // configurations and avoid errors.
671 #[cfg(not(any(docsrs, test, doctest)))]
672 #[doc(hidden)]
673 pub mod bindgen_examples {}
674