wit-bindgen
wit-bindgen copied to clipboard
Ownership of resource handles
I'm would like to use wit to generate host/wasmtime and guest/rust bindings for lunatic. However, I'm still not sure how to correctly model a specific use case.
In lunatic some of the host resources can be sent between different instances. This means that they are consumed by some host functions and removed from the resource table. This doesn't work well with the current Canonical ABI proposal that requires all resources to expose a clone
and drop
API and implies reference counting.
One workaround is to keep the host resource inside of an Option<T>
and remove it once it needs to be sent to another instance. Then on every invocation of a host function dealing with the resource check if the value is Some(T)
. This works okish.
The bigger issues here is that the guest side generated code also generates a Clone
and Drop
implementation. We would like to avoid exposing the Clone
functionality to developers though. It would make the API better reflect the underlaying properties of the resource. Is there a way of disabling this Clone
option, and ideally not even use reference counting inside of the host in this case?
I guess my question i more general. How to handle this specific scenario with interface types, when the function "consumes" the resource?
I also feel like I didn't completely understand the difference between a resource and a resource handle. Maybe the automatic cloning can be avoided if using resources directly, instead of handles?
I think this is probably best addressed at the interface-types layer rather than wit-bindgen here. There's not really a way to simply tweak codegen to remove Clone
impls since they're reflecting how interface types handles/resources are expected to work.
I believe what you want for your use case is a Rust-like owned resource and perhaps a borrowed resource as well. We've discussed this internally in the past but they haven't ever gotten over the finish line to the point of being spec'd and implemented. For now I'd recommend opening an issue on the interface-types repository to track the feature request.
own
and borrow
types will soon be added to the component-model. I believe this will handle your use-case https://github.com/WebAssembly/component-model/pull/129
I am currently working on a small side project that will need the new own
and borrow
types in wit-bindgen
(I wrote an original prototype when the old resource implementation was still around). Is there a rough timeline on when they will be implemented, or some way I could help contribute with their implementation?
Resources are pretty high up on the list of things to do, but I don't have a timeline for when they'll be done. Work on resources will span many repositories and many aspects of the design an implementation, so unfortunately I don't know of a great way to have the work spread out amongst many at this time.