I'm trying to do an host implementation of part of the wasi-messaging API and I'm struggling with how I support the resources that are defined.
In particular I'm trying to implement the following WIT setup
world pubsub {
import wasi:messaging/producer@0.2.0-draft;
}
The producer interface has a function that returns an error resource
resource error {
trace: static func() -> string;
}
interface producer {
use messaging-types.{client, channel, message, error};
send: func(c: client, ch: channel, m: list<message>) -> result<_, error>;
}
For the host implementation I have this
#[async_trait::async_trait]
impl wasi::messaging::producer::Host for PublishData {
async fn send(
&mut self,
component:wasmtime::component::Resource<Client>,
channel:Channel,
messages:Vec<Message>) -> wasmtime::Result<Result<(),wasmtime::component::Resource<Error>>> {
for msg in messages {
self.publisher.publishmsg(channel.to_string(), msg.data);
}
let err = MyError;
let resource: Resource<Error> = self.table.push(err).unwrap();
Ok(Ok(resource));
}
}
I understand that I have to use the ResourceTable to create the resource but cannot see how I create the Error type. The bindgen generates error as an empty enum
pub enum Error{}
and I see there is also a HostError trait generated too
pub trait HostError {
async fn trace(&mut self,) -> wasmtime::Result<String>;
fn drop(&mut self,rep:wasmtime::component::Resource<Error>) -> wasmtime::Result<()>;
}#[doc = " There are two types of channels:"]
but its not clear to me if this trait is what I implement or do it some other way. I haven't really found any examples that provide a guide on this.
In your call to bindgen!
you'll want to specify with
for the error type pointing to your own error type to avoid using the default enum Error {}
.
WIth a change like this we'd probably require you to specify with
to avoid having a hard-to-use default.
Thanks Alex, I had seen the "with" statements before but had not connected the dots
Richard Backhouse has marked this topic as resolved.
Last updated: Nov 22 2024 at 17:03 UTC