Is there a fundamental reason why wit_bindgen::rt::Resource<T: RustResource>
doesn't have an into_inner
method allowing you to drop the resource handle and take back ownership of the T
instance? If not, I'd be happy to make a PR to add it.
Context: I'm updating wasi-virt
to use a more recent WASI snapshot based on resources, and such a method would make my life easier.
Nah that seems reasonable to me to add!
Hmm, now I'm not sure it's that easy. If the guest calls into the host to drop the handle, it will call back into the guest to run the dtor
, right? But if it has already moved out of the Box
that the handle referred to, the dtor
will try to drop the Box
it just moved out of.
It's like I need some way to say to the host "drop this handle, but don't call the dtor".
Or else store the T
in a Box<Option<T>>
and .take()
it.
Hm true yeah, invoking drop without the destructor is the topic of this issue.
To allow &mut self
in methods I've already wanted to replace the representation of Box<T>
with Box<RefCell<T>>
. Throwing an Option
in there I don't think would be too bad
Yeah, resource.consume
would be great. Meanwhile, I'm pursuing the Option
approach and have managed to break a bunch of runtime tests. Probably something dumb I did, but not obvious yet.
If you want to get really nasty we can probably skip the Option
with a ptr::read
plus a global saying "hey dtor don't actually do anything"
I think the Option
approach is fine until we have resource.consume
. I think I see why the tests are failing now; I need to update the binding generator to generate *const Option<X>
instead of *const X
.
Got the tests passing; will post a PR once I've verified it does what I need in wasi-virt
.
Last updated: Dec 23 2024 at 13:07 UTC