wasmtime::component::bindgen_examples

Module _1_world_imports

source
Available on crate feature runtime and crate feature component-model and docsrs only.
Expand description

An example of generated bindings for top-level imported functions and interfaces into a world.

The code used to generate this module is:

use wasmtime::component::*;
use wasmtime::{Engine, Store};

bindgen!({
    inline: r#"
        package example:imports;

        world my-world {
            /// Fetch a greeting to present.
            import greet: func() -> string;

            /// Log a message to the host.
            import log: func(msg: string);

            import my-custom-host: interface {
                tick: func();
            }
        }
    "#,
});

struct MyState {
    // ...
}

impl my_custom_host::Host for MyState {
    fn tick(&mut self) {
        todo!()
    }
}

impl MyWorldImports for MyState {
    fn greet(&mut self) -> String {
        todo!()
    }

    fn log(&mut self, msg: String) {
        println!("{msg}");
    }
}

fn main() -> wasmtime::Result<()> {
    let engine = Engine::default();
    let component = Component::from_file(&engine, "./your-component.wasm")?;

    let mut linker = Linker::new(&engine);
    MyWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?;

    let mut store = Store::new(
        &engine,
        MyState { /* ... */ },
    );
    let bindings = MyWorld::instantiate(&mut store, &component, &linker)?;

    // ... NB: this world has no exports just yet so not much can be done
    // with `bindings`.

    Ok(())
}

Modules§

Structs§

  • Auto-generated bindings for an instance a component which implements the world my-world.
  • Auto-generated bindings for index of the exports of my-world.
  • Auto-generated bindings for a pre-instantiated version of a component which implements the world my-world.

Traits§