use my::project::types::Host; use wasmtime::{ component::{bindgen, Component, Linker, ResourceTable}, Config, Engine, Store, }; use crate::my::project::types::HostTestresource; // Defaults to 'wit' folder adjacent to cargo.toml. // Change folder by using: bindgen!(in "other/with/folder") bindgen!({ with: { "my:project/types/testresource": TestResourceData, } }); pub struct TestResourceData { some_data: String, } struct TestState { name: String, resources: ResourceTable, } impl TestImports for TestState { fn hello(&mut self, hello_string: RecordString) -> std::result::Result<(), wasmtime::Error> { println!("{} - {}", self.name, hello_string.hello_string); Ok(()) } } impl HostTestresource for TestState { fn hello(&mut self,self_:wasmtime::component::Resource,) -> wasmtime::Result<()> { println!("HostTestResource - {} - {}", self.name, self.resources.get(&self_).unwrap().some_data); Ok(()) } fn drop(&mut self,rep:wasmtime::component::Resource) -> wasmtime::Result<()> { self.resources.delete(rep).unwrap(); Ok(()) } } impl Host for TestState { } fn main() -> wasmtime::Result<()> { let engine = Engine::new(Config::new().wasm_component_model(true))?; let component = Component::from_file(&engine, "target/wasm32-wasip1/release/component.wasm")?; let mut linker = Linker::new(&engine); Test::add_to_linker(&mut linker, |state: &mut TestState| state)?; let test_state = TestState { name: "Hello World".to_string(), resources: ResourceTable::new(), }; let mut store = Store::new( &engine, test_state ); let (bindings, _) = Test::instantiate(&mut store, &component, &linker)?; let resource = store.data_mut().resources.push(TestResourceData{some_data: "I'm a component with hidden data".to_owned()}).unwrap(); bindings.call_greet(&mut store, &RecordString { hello_string: "Hello".to_owned() }, resource)?; Ok(()) }