I'm having difficulty exporting a resource within a wit file.
I have the following wit file:
package example:greeter;
world greeter {
resource person {
constructor(name: string);
greet();
}
export person;
}
However this fails. The way I previously solved this when resources weren't working, is by passing in the state as list<u8>
and deserializing it in the guest. But this also meant any changes I made to a struct needed to be serailized again and returned.
I was hoping resources could solve this, so I could have access to a &self
or &mut self
on a struct within the guest.
Though it seems like resources are only meant to be implemented on the host?
I seemed to have some luck by doing this:
package example:greeter;
world greeter {
export person: interface {
resource person {
constructor(name: string);
greet();
}
};
}
Though I'm not sure if there's a better way
Also, I can only use &self
with resources? I can get around this limitation by wrapping my fields in RefCell, but it would be ideal if I could implement the trait with &mut self
. Is there a reason it's currently just &self
?
(Where I put greet()
should be green: func()
)
Currently resources must be in world interface exports, they can't be at the top level. For the &self bit it's something I'd like to fix but for now that does require refcell or cell yes
From the host side, I'm using:
wasmtime::component::bindgen!({
world: "aggregate",
ownership: Borrowing { duplicate_if_necessary: true },
async: true,
});
And it generates something like:
pub struct Aggregate {
interface0: exports::aggregate::Aggregate,
}
pub mod exports {
pub mod aggregate {
pub struct Aggregate {
init: wasmtime::component::Func,
apply: wasmtime::component::Func,
handle: wasmtime::component::Func,
}
}
}
The issue I'm having is that it doesn't add #[derive(Clone, Copy)]
to the Aggregate structs.
I dug into the options for wasmtime::component::bindgen!
and didn't see anything for adding derives to these, and sadly I can't implement them myself since the fields of aggregate are private.
Last updated: Dec 23 2024 at 12:05 UTC