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 #![cfg_attr(docsrs, doc(cfg(feature = "component-model")))] 101 // rustdoc appears to lie about a warning above, so squelch it for now. 102 #![allow(rustdoc::redundant_explicit_links)] 103 104 mod component; 105 mod func; 106 mod instance; 107 mod linker; 108 mod matching; 109 mod resource_table; 110 mod resources; 111 mod storage; 112 mod store; 113 pub mod types; 114 mod values; 115 pub use self::component::Component; 116 pub use self::func::{ 117 ComponentNamedList, ComponentType, Func, Lift, Lower, TypedFunc, WasmList, WasmStr, 118 }; 119 pub use self::instance::{ExportInstance, Exports, Instance, InstancePre}; 120 pub use self::linker::{Linker, LinkerInstance}; 121 pub use self::resource_table::{ResourceTable, ResourceTableError}; 122 pub use self::resources::{Resource, ResourceAny}; 123 pub use self::types::{ResourceType, Type}; 124 pub use self::values::Val; 125 126 pub(crate) use self::resources::HostResourceData; 127 128 // These items are expected to be used by an eventual 129 // `#[derive(ComponentType)]`, they are not part of Wasmtime's API stability 130 // guarantees 131 #[doc(hidden)] 132 pub mod __internal { 133 pub use super::func::{ 134 bad_type_info, format_flags, lower_payload, typecheck_enum, typecheck_flags, 135 typecheck_record, typecheck_variant, ComponentVariant, LiftContext, LowerContext, 136 MaybeUninitExt, Options, 137 }; 138 pub use super::matching::InstanceType; 139 pub use crate::map_maybe_uninit; 140 pub use crate::store::StoreOpaque; 141 pub use anyhow; 142 #[cfg(feature = "async")] 143 pub use async_trait::async_trait; 144 pub use wasmtime_environ; 145 pub use wasmtime_environ::component::{CanonicalAbiInfo, ComponentTypes, InterfaceType}; 146 } 147 148 pub(crate) use self::store::ComponentStoreData; 149 150 /// Generate bindings for a [WIT world]. 151 /// 152 /// [WIT world]: https://component-model.bytecodealliance.org/design/worlds.html 153 /// [WIT package]: https://component-model.bytecodealliance.org/design/packages.html 154 /// 155 /// This macro ingests a [WIT world] and will generate all the necessary 156 /// bindings for instantiating components that ascribe to the `world`. This 157 /// provides a higher-level representation of working with a component than the 158 /// raw [`Instance`] type which must be manually-type-checked and manually have 159 /// its imports provided via the [`Linker`] type. 160 /// 161 /// The most basic usage of this macro is: 162 /// 163 /// ```rust,ignore 164 /// wasmtime::component::bindgen!(); 165 /// ``` 166 /// 167 /// This will parse your projects [WIT package] in a `wit` directory adjacent to 168 /// your crate's `Cargo.toml`. All of the `*.wit` files in that directory are 169 /// parsed and then the single `world` found will be used for bindings. 170 /// 171 /// For example if your project contained: 172 /// 173 /// ```text,ignore 174 /// // wit/my-component.wit 175 /// 176 /// package my:project; 177 /// 178 /// world hello-world { 179 /// import name: func() -> string; 180 /// export greet: func(); 181 /// } 182 /// ``` 183 /// 184 /// Then you can interact with the generated bindings like so: 185 /// 186 /// ```rust 187 /// use wasmtime::component::*; 188 /// use wasmtime::{Config, Engine, Store}; 189 /// 190 /// # const _: () = { macro_rules! bindgen { () => () } 191 /// bindgen!(); 192 /// # }; 193 /// # bindgen!({ 194 /// # inline: r#" 195 /// # package my:project; 196 /// # world hello-world { 197 /// # import name: func() -> string; 198 /// # export greet: func(); 199 /// # } 200 /// # "#, 201 /// # }); 202 /// 203 /// struct MyState { 204 /// name: String, 205 /// } 206 /// 207 /// // Imports into the world, like the `name` import for this world, are 208 /// // satisfied through traits. 209 /// impl HelloWorldImports for MyState { 210 /// // Note the `Result` return value here where `Ok` is returned back to 211 /// // the component and `Err` will raise a trap. 212 /// fn name(&mut self) -> String { 213 /// self.name.clone() 214 /// } 215 /// } 216 /// 217 /// fn main() -> wasmtime::Result<()> { 218 /// # if true { return Ok(()) } 219 /// // Configure an `Engine` and compile the `Component` that is being run for 220 /// // the application. 221 /// let mut config = Config::new(); 222 /// config.wasm_component_model(true); 223 /// let engine = Engine::new(&config)?; 224 /// let component = Component::from_file(&engine, "./your-component.wasm")?; 225 /// 226 /// // Instantiation of bindings always happens through a `Linker`. 227 /// // Configuration of the linker is done through a generated `add_to_linker` 228 /// // method on the bindings structure. 229 /// // 230 /// // Note that the closure provided here is a projection from `T` in 231 /// // `Store<T>` to `&mut U` where `U` implements the `HelloWorldImports` 232 /// // trait. In this case the `T`, `MyState`, is stored directly in the 233 /// // structure so no projection is necessary here. 234 /// let mut linker = Linker::new(&engine); 235 /// HelloWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?; 236 /// 237 /// // As with the core wasm API of Wasmtime instantiation occurs within a 238 /// // `Store`. The bindings structure contains an `instantiate` method which 239 /// // takes the store, component, and linker. This returns the `bindings` 240 /// // structure which is an instance of `HelloWorld` and supports typed access 241 /// // to the exports of the component. 242 /// let mut store = Store::new( 243 /// &engine, 244 /// MyState { 245 /// name: "me".to_string(), 246 /// }, 247 /// ); 248 /// let (bindings, _) = HelloWorld::instantiate(&mut store, &component, &linker)?; 249 /// 250 /// // Here our `greet` function doesn't take any parameters for the component, 251 /// // but in the Wasmtime embedding API the first argument is always a `Store`. 252 /// bindings.call_greet(&mut store)?; 253 /// Ok(()) 254 /// } 255 /// ``` 256 /// 257 /// The function signatures within generated traits and on generated exports 258 /// match the component-model signatures as specified in the WIT `world` item. 259 /// Note that WIT also has support for importing and exports interfaces within 260 /// worlds, which can be bound here as well: 261 /// 262 /// For example this WIT input 263 /// 264 /// ```text,ignore 265 /// // wit/my-component.wit 266 /// 267 /// package my:project; 268 /// 269 /// interface host { 270 /// gen-random-integer: func() -> u32; 271 /// sha256: func(bytes: list<u8>) -> string; 272 /// } 273 /// 274 /// world hello-world { 275 /// import host; 276 /// 277 /// export demo: interface { 278 /// run: func(); 279 /// } 280 /// } 281 /// ``` 282 /// 283 /// Then you can interact with the generated bindings like so: 284 /// 285 /// ```rust 286 /// use wasmtime::component::*; 287 /// use wasmtime::{Config, Engine, Store}; 288 /// use my::project::host::Host; 289 /// 290 /// # const _: () = { macro_rules! bindgen { () => () } 291 /// bindgen!(); 292 /// # }; 293 /// # bindgen!({ 294 /// # inline: r#" 295 /// # package my:project; 296 /// # 297 /// # interface host { 298 /// # gen-random-integer: func() -> u32; 299 /// # sha256: func(bytes: list<u8>) -> string; 300 /// # } 301 /// # 302 /// # world hello-world { 303 /// # import host; 304 /// # 305 /// # export demo: interface { 306 /// # run: func(); 307 /// # } 308 /// # } 309 /// # "#, 310 /// # }); 311 /// 312 /// struct MyState { 313 /// // ... 314 /// } 315 /// 316 /// // Note that the trait here is per-interface and within a submodule now. 317 /// impl Host for MyState { 318 /// fn gen_random_integer(&mut self) -> u32 { 319 /// # panic!(); 320 /// # #[cfg(FALSE)] 321 /// rand::thread_rng().gen() 322 /// } 323 /// 324 /// fn sha256(&mut self, bytes: Vec<u8>) -> String { 325 /// // ... 326 /// # panic!() 327 /// } 328 /// } 329 /// 330 /// fn main() -> wasmtime::Result<()> { 331 /// # if true { return Ok(()) } 332 /// let mut config = Config::new(); 333 /// config.wasm_component_model(true); 334 /// let engine = Engine::new(&config)?; 335 /// let component = Component::from_file(&engine, "./your-component.wasm")?; 336 /// 337 /// let mut linker = Linker::new(&engine); 338 /// HelloWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?; 339 /// 340 /// let mut store = Store::new( 341 /// &engine, 342 /// MyState { /* ... */ }, 343 /// ); 344 /// let (bindings, _) = HelloWorld::instantiate(&mut store, &component, &linker)?; 345 /// 346 /// // Note that the `demo` method returns a `&Demo` through which we can 347 /// // run the methods on that interface. 348 /// bindings.demo().call_run(&mut store)?; 349 /// Ok(()) 350 /// } 351 /// ``` 352 /// 353 /// The generated bindings can additionally be explored more fully with `cargo 354 /// doc` to see what types and traits and such are generated. 355 /// 356 /// # Syntax 357 /// 358 /// This procedural macro accepts a few different syntaxes. The primary purpose 359 /// of this macro is to locate a WIT package, parse it, and then extract a 360 /// `world` from the parsed package. There are then codegen-specific options to 361 /// the bindings themselves which can additionally be specified. 362 /// 363 /// Usage of this macro looks like: 364 /// 365 /// ```rust,ignore 366 /// // Parse the `wit/` folder adjacent to this crate's `Cargo.toml` and look 367 /// // for a single `world` in it. There must be exactly one for this to 368 /// // succeed. 369 /// bindgen!(); 370 /// 371 /// // Parse the `wit/` folder adjacent to this crate's `Cargo.toml` and look 372 /// // for the world `foo` contained in it. 373 /// bindgen!("foo"); 374 /// 375 /// // Parse the folder `other/wit/folder` adjacent to `Cargo.toml`. 376 /// bindgen!(in "other/wit/folder"); 377 /// bindgen!("foo" in "other/wit/folder"); 378 /// 379 /// // Parse the file `foo.wit` as a single-file WIT package with no 380 /// // dependencies. 381 /// bindgen!("foo" in "foo.wit"); 382 /// ``` 383 /// 384 /// A more configured version of invoking this macro looks like: 385 /// 386 /// ```rust,ignore 387 /// bindgen!({ 388 /// world: "foo", // not needed if `path` has one `world` 389 /// 390 /// // same as in `bindgen!(in "other/wit/folder") 391 /// path: "other/wit/folder", 392 /// 393 /// // Instead of `path` the WIT document can be provided inline if 394 /// // desired. 395 /// inline: " 396 /// package my:inline; 397 /// 398 /// world foo { 399 /// // ... 400 /// } 401 /// ", 402 /// 403 /// // Add calls to `tracing::span!` before each import or export is called 404 /// // to log arguments and return values. 405 /// // 406 /// // This option defaults to `false`. 407 /// tracing: true, 408 /// 409 /// // Imports will be async functions through #[async_trait] and exports 410 /// // are also invoked as async functions. Requires `Config::async_support` 411 /// // to be `true`. 412 /// // 413 /// // Note that this is only async for the host as the guest will still 414 /// // appear as if it's invoking blocking functions. 415 /// // 416 /// // This option defaults to `false`. 417 /// async: true, 418 /// 419 /// // Alternative mode of async configuration where this still implies 420 /// // async instantiation happens, for example, but more control is 421 /// // provided over which imports are async and which aren't. 422 /// // 423 /// // Note that in this mode all exports are still async. 424 /// async: { 425 /// // All imports are async except for functions with these names 426 /// except_imports: ["foo", "bar"], 427 /// 428 /// // All imports are synchronous except for functions with these names 429 /// // 430 /// // Note that this key cannot be specified with `except_imports`, 431 /// // only one or the other is accepted. 432 /// only_imports: ["foo", "bar"], 433 /// }, 434 /// 435 /// // This option is used to indicate whether imports can trap. 436 /// // 437 /// // Imports that may trap have their return types wrapped in 438 /// // `wasmtime::Result<T>` where the `Err` variant indicates that a 439 /// // trap will be raised in the guest. 440 /// // 441 /// // By default imports cannot trap and the return value is the return 442 /// // value from the WIT bindings itself. This value can be set to `true` 443 /// // to indicate that any import can trap. This value can also be set to 444 /// // an array-of-strings to indicate that only a set list of imports 445 /// // can trap. 446 /// trappable_imports: false, // no imports can trap (default) 447 /// // trappable_imports: true, // all imports can trap 448 /// // trappable_imports: ["foo", "bar"], // only these can trap 449 /// 450 /// // This can be used to translate WIT return values of the form 451 /// // `result<T, error-type>` into `Result<T, RustErrorType>` in Rust. 452 /// // Users must define `RustErrorType` and the `Host` trait for the 453 /// // interface which defines `error-type` will have a method 454 /// // called `convert_error_type` which converts `RustErrorType` 455 /// // into `wasmtime::Result<ErrorType>`. This conversion can either 456 /// // return the raw WIT error (`ErrorType` here) or a trap. 457 /// // 458 /// // By default this option is not specified. This option only takes 459 /// // effect when `trappable_imports` is set for some imports. 460 /// trappable_error_type: { 461 /// "wasi:io/streams/stream-error" => RustErrorType, 462 /// }, 463 /// 464 /// // All generated bindgen types are "owned" meaning types like `String` 465 /// // are used instead of `&str`, for example. This is the default and 466 /// // ensures that the same type used in both imports and exports uses the 467 /// // same generated type. 468 /// ownership: Owning, 469 /// 470 /// // Alternative to `Owning` above where borrowed types attempt to be used 471 /// // instead. The `duplicate_if_necessary` configures whether duplicate 472 /// // Rust types will be generated for the same WIT type if necessary, for 473 /// // example when a type is used both as an import and an export. 474 /// ownership: Borrowing { 475 /// duplicate_if_necessary: true 476 /// }, 477 /// 478 /// // Restrict the code generated to what's needed for the interface 479 /// // imports in the inlined WIT document fragment. 480 /// interfaces: " 481 /// import wasi:cli/command; 482 /// ", 483 /// 484 /// // Remap imported interfaces or resources to types defined in Rust 485 /// // elsewhere. Using this option will prevent any code from being 486 /// // generated for interfaces mentioned here. Resources named here will 487 /// // not have a type generated to represent the resource. 488 /// // 489 /// // Interfaces mapped with this option should be previously generated 490 /// // with an invocation of this macro. Resources need to be mapped to a 491 /// // Rust type name. 492 /// with: { 493 /// // This can be used to indicate that entire interfaces have 494 /// // bindings generated elsewhere with a path pointing to the 495 /// // bindinges-generated module. 496 /// "wasi:random/random": wasmtime_wasi::bindings::random::random, 497 /// 498 /// // Similarly entire packages can also be specified. 499 /// "wasi:cli": wasmtime_wasi::bindings::cli, 500 /// 501 /// // Or, if applicable, entire namespaces can additionally be mapped. 502 /// "wasi": wasmtime_wasi::bindings, 503 /// 504 /// // Versions are supported if multiple versions are in play: 505 /// "wasi:http/types@0.2.0": wasmtime_wasi_http::bindings::http::types, 506 /// "wasi:[email protected]": wasmtime_wasi_http::bindings::http, 507 /// 508 /// // The `with` key can also be used to specify the `T` used in 509 /// // import bindings of `Resource<T>`. This can be done to configure 510 /// // which typed resource shows up in generated bindings and can be 511 /// // useful when working with the typed methods of `ResourceTable`. 512 /// "wasi:filesystem/types/descriptor": MyDescriptorType, 513 /// }, 514 /// 515 /// // Addtional derive attributes to include on generated types (structs or enums). 516 /// // 517 /// // These are deduplicated and attached in a deterministic order. 518 /// additional_derives: [ 519 /// Hash, 520 /// serde::Deserialize, 521 /// serde::Serialize, 522 /// ] 523 /// }); 524 /// ``` 525 pub use wasmtime_component_macro::bindgen; 526 527 /// Derive macro to generate implementations of the [`ComponentType`] trait. 528 /// 529 /// This derive macro can be applied to `struct` and `enum` definitions and is 530 /// used to bind either a `record`, `enum`, or `variant` in the component model. 531 /// 532 /// Note you might be looking for [`bindgen!`] rather than this macro as that 533 /// will generate the entire type for you rather than just a trait 534 /// implementation. 535 /// 536 /// This macro supports a `#[component]` attribute which is used to customize 537 /// how the type is bound to the component model. A top-level `#[component]` 538 /// attribute is required to specify either `record`, `enum`, or `variant`. 539 /// 540 /// ## Records 541 /// 542 /// `record`s in the component model correspond to `struct`s in Rust. An example 543 /// is: 544 /// 545 /// ```rust 546 /// use wasmtime::component::ComponentType; 547 /// 548 /// #[derive(ComponentType)] 549 /// #[component(record)] 550 /// struct Color { 551 /// r: u8, 552 /// g: u8, 553 /// b: u8, 554 /// } 555 /// ``` 556 /// 557 /// which corresponds to the WIT type: 558 /// 559 /// ```wit 560 /// record color { 561 /// r: u8, 562 /// g: u8, 563 /// b: u8, 564 /// } 565 /// ``` 566 /// 567 /// Note that the name `Color` here does not need to match the name in WIT. 568 /// That's purely used as a name in Rust of what to refer to. The field names 569 /// must match that in WIT, however. Field names can be customized with the 570 /// `#[component]` attribute though. 571 /// 572 /// ```rust 573 /// use wasmtime::component::ComponentType; 574 /// 575 /// #[derive(ComponentType)] 576 /// #[component(record)] 577 /// struct VerboseColor { 578 /// #[component(name = "r")] 579 /// red: u8, 580 /// #[component(name = "g")] 581 /// green: u8, 582 /// #[component(name = "b")] 583 /// blue: u8, 584 /// } 585 /// ``` 586 /// 587 /// Also note that field ordering is significant at this time and must match 588 /// WIT. 589 /// 590 /// ## Variants 591 /// 592 /// `variant`s in the component model correspond to a subset of shapes of a Rust 593 /// `enum`. Variants in the component model have a single optional payload type 594 /// which means that not all Rust `enum`s correspond to component model 595 /// `variant`s. An example variant is: 596 /// 597 /// ```rust 598 /// use wasmtime::component::ComponentType; 599 /// 600 /// #[derive(ComponentType)] 601 /// #[component(variant)] 602 /// enum Filter { 603 /// #[component(name = "none")] 604 /// None, 605 /// #[component(name = "all")] 606 /// All, 607 /// #[component(name = "some")] 608 /// Some(Vec<String>), 609 /// } 610 /// ``` 611 /// 612 /// which corresponds to the WIT type: 613 /// 614 /// ```wit 615 /// variant filter { 616 /// none, 617 /// all, 618 /// some(list<string>), 619 /// } 620 /// ``` 621 /// 622 /// The `variant` style of derive allows an optional payload on Rust `enum` 623 /// variants but it must be a single unnamed field. Variants of the form `Foo(T, 624 /// U)` or `Foo { name: T }` are not supported at this time. 625 /// 626 /// Note that the order of variants in Rust must match the order of variants in 627 /// WIT. Additionally it's likely that `#[component(name = "...")]` is required 628 /// on all Rust `enum` variants because the name currently defaults to the Rust 629 /// name which is typically UpperCamelCase whereas WIT uses kebab-case. 630 /// 631 /// ## Enums 632 /// 633 /// `enum`s in the component model correspond to C-like `enum`s in Rust. Note 634 /// that a component model `enum` does not allow any payloads so the Rust `enum` 635 /// must additionally have no payloads. 636 /// 637 /// ```rust 638 /// use wasmtime::component::ComponentType; 639 /// 640 /// #[derive(ComponentType)] 641 /// #[component(enum)] 642 /// enum Setting { 643 /// #[component(name = "yes")] 644 /// Yes, 645 /// #[component(name = "no")] 646 /// No, 647 /// #[component(name = "auto")] 648 /// Auto, 649 /// } 650 /// ``` 651 /// 652 /// which corresponds to the WIT type: 653 /// 654 /// ```wit 655 /// enum setting { 656 /// yes, 657 /// no, 658 /// auto, 659 /// } 660 /// ``` 661 /// 662 /// Note that the order of variants in Rust must match the order of variants in 663 /// WIT. Additionally it's likely that `#[component(name = "...")]` is required 664 /// on all Rust `enum` variants because the name currently defaults to the Rust 665 /// name which is typically UpperCamelCase whereas WIT uses kebab-case. 666 pub use wasmtime_component_macro::ComponentType; 667 668 /// A derive macro for generating implementations of the [`Lift`] trait. 669 /// 670 /// This macro will likely be applied in conjunction with the 671 /// [`#[derive(ComponentType)]`](macro@ComponentType) macro along the lines 672 /// of `#[derive(ComponentType, Lift)]`. This trait enables reading values from 673 /// WebAssembly. 674 /// 675 /// Note you might be looking for [`bindgen!`] rather than this macro as that 676 /// will generate the entire type for you rather than just a trait 677 /// implementation. 678 /// 679 /// At this time this derive macro has no configuration. 680 /// 681 /// ## Examples 682 /// 683 /// ```rust 684 /// use wasmtime::component::{ComponentType, Lift}; 685 /// 686 /// #[derive(ComponentType, Lift)] 687 /// #[component(record)] 688 /// struct Color { 689 /// r: u8, 690 /// g: u8, 691 /// b: u8, 692 /// } 693 /// ``` 694 pub use wasmtime_component_macro::Lift; 695 696 /// A derive macro for generating implementations of the [`Lower`] trait. 697 /// 698 /// This macro will likely be applied in conjunction with the 699 /// [`#[derive(ComponentType)]`](macro@ComponentType) macro along the lines 700 /// of `#[derive(ComponentType, Lower)]`. This trait enables passing values to 701 /// WebAssembly. 702 /// 703 /// Note you might be looking for [`bindgen!`] rather than this macro as that 704 /// will generate the entire type for you rather than just a trait 705 /// implementation. 706 /// 707 /// At this time this derive macro has no configuration. 708 /// 709 /// ## Examples 710 /// 711 /// ```rust 712 /// use wasmtime::component::{ComponentType, Lower}; 713 /// 714 /// #[derive(ComponentType, Lower)] 715 /// #[component(record)] 716 /// struct Color { 717 /// r: u8, 718 /// g: u8, 719 /// b: u8, 720 /// } 721 /// ``` 722 pub use wasmtime_component_macro::Lower; 723 724 /// A macro to generate a Rust type corresponding to WIT `flags` 725 /// 726 /// This macro generates a type that implements the [`ComponentType`], [`Lift`], 727 /// and [`Lower`] traits. The generated Rust type corresponds to the `flags` 728 /// type in WIT. 729 /// 730 /// Example usage of this looks like: 731 /// 732 /// ```rust 733 /// use wasmtime::component::flags; 734 /// 735 /// flags! { 736 /// Permissions { 737 /// #[component(name = "read")] 738 /// const READ; 739 /// #[component(name = "write")] 740 /// const WRITE; 741 /// #[component(name = "execute")] 742 /// const EXECUTE; 743 /// } 744 /// } 745 /// 746 /// fn validate_permissions(permissions: &mut Permissions) { 747 /// if permissions.contains(Permissions::EXECUTE | Permissions::WRITE) { 748 /// panic!("cannot enable both writable and executable at the same time"); 749 /// } 750 /// 751 /// if permissions.contains(Permissions::READ) { 752 /// panic!("permissions must at least contain read"); 753 /// } 754 /// } 755 /// ``` 756 /// 757 /// which corresponds to the WIT type: 758 /// 759 /// ```wit 760 /// flags permissions { 761 /// read, 762 /// write, 763 /// execute, 764 /// } 765 /// ``` 766 /// 767 /// This generates a structure which is similar to/inspired by the [`bitflags` 768 /// crate](https://crates.io/crates/bitflags). The `Permissions` structure 769 /// generated implements the [`PartialEq`], [`Eq`], [`Debug`], [`BitOr`], 770 /// [`BitOrAssign`], [`BitAnd`], [`BitAndAssign`], [`BitXor`], [`BitXorAssign`], 771 /// and [`Not`] traits - in addition to the Wasmtime-specific component ones 772 /// [`ComponentType`], [`Lift`], and [`Lower`]. 773 /// 774 /// [`BitOr`]: std::ops::BitOr 775 /// [`BitOrAssign`]: std::ops::BitOrAssign 776 /// [`BitAnd`]: std::ops::BitAnd 777 /// [`BitAndAssign`]: std::ops::BitAndAssign 778 /// [`BitXor`]: std::ops::BitXor 779 /// [`BitXorAssign`]: std::ops::BitXorAssign 780 /// [`Not`]: std::ops::Not 781 pub use wasmtime_component_macro::flags; 782