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