Available on crate feature
component-model and crate feature runtime and (docsrs) only.Expand description
Example of a world which exports a resource.
- Guest resources are modeled as
ResourceAny. Note that this type is not specialized per-resource at this time so care must be taken to not mix them up. - Resource-related methods are a projection from a
Gueststructure, for example toGuestLoggerhere. - Resource-related methods all take a
ResourceAnyas an argument or a return value. - The WIT signature determines ownership. A
loggerparameter transfers ownership, whileborrow<logger>borrows the resource for one call. - The host must explicitly drop each owned
ResourceAnyit retains.
use wasmtime::{Result, Engine, Store};
use wasmtime::component::{bindgen, Component, Linker};
use self::exports::example::exported_resources::logging::Level;
bindgen!({
inline: r#"
package example:exported-resources;
world export-some-resources {
export logging;
}
interface logging {
enum level {
debug,
info,
warn,
error,
}
resource logger {
constructor(max-level: level);
get-max-level: func() -> level;
set-max-level: func(level: level);
log: func(level: level, msg: string);
}
inspect-logger: func(logger: borrow<logger>) -> level;
take-logger: func(logger: logger);
get-default-logger: func() -> logger;
}
"#,
});
struct MyState;
fn main() -> Result<()> {
let engine = Engine::default();
let component = Component::from_file(&engine, "./your-component.wasm")?;
let linker = Linker::new(&engine);
// ... this small example has no imports so nothing is added here, but
// if you had imports this is where they'd go.
let mut store = Store::new(&engine, MyState);
let bindings = ExportSomeResources::instantiate(&mut store, &component, &linker)?;
let guest = bindings.example_exported_resources_logging();
let logger = guest.logger();
// Free interface functions are methods on `guest`. A function that
// returns a WIT `logger` gives the host an owned `ResourceAny`.
let returned_logger = guest.call_get_default_logger(&mut store)?;
// Both `logger` and `borrow<logger>` parameters are `ResourceAny` in
// Rust, so the WIT signature determines ownership. `inspect-logger`
// takes `borrow<logger>`, so the host keeps ownership after this call.
let max_level = guest.call_inspect_logger(&mut store, returned_logger)?;
logger.call_log(&mut store, returned_logger, max_level, "hello!")?;
// The host must either drop an owned resource or transfer it back to
// the guest. This also applies to resources nested in records,
// variants, lists, and other values.
returned_logger.resource_drop(&mut store)?;
// Resource methods are all attached to `logger` and take the
// `ResourceAny` parameter explicitly.
let my_logger = logger.call_constructor(&mut store, Level::Warn)?;
assert_eq!(logger.call_get_max_level(&mut store, my_logger)?, Level::Warn);
logger.call_set_max_level(&mut store, my_logger, Level::Info)?;
logger.call_log(&mut store, my_logger, Level::Debug, "hello!")?;
// `take-logger` takes an owned WIT `logger`, so this call transfers
// ownership to the guest. `ResourceAny` is `Copy`, but the host must
// not drop or reuse `my_logger` afterwards.
guest.call_take_logger(&mut store, my_logger)?;
Ok(())
}Modules§
Structs§
- Export
Some Resources - Auto-generated bindings for an instance a component which
implements the world
export-some-resources. - Export
Some Resources Indices - Auto-generated bindings for index of the exports of
export-some-resources. - Export
Some Resources Pre - Auto-generated bindings for a pre-instantiated version of a
component which implements the world
export-some-resources.