Stream: wit-bindgen

Topic: an `into_inner` method for `wit_bindgen::rt::Resource`


view this post on Zulip Joel Dice (Nov 09 2023 at 23:21):

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.

view this post on Zulip Alex Crichton (Nov 09 2023 at 23:22):

Nah that seems reasonable to me to add!

view this post on Zulip Joel Dice (Nov 09 2023 at 23:35):

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.

view this post on Zulip Joel Dice (Nov 09 2023 at 23:37):

It's like I need some way to say to the host "drop this handle, but don't call the dtor".

view this post on Zulip Joel Dice (Nov 09 2023 at 23:38):

Or else store the T in a Box<Option<T>> and .take() it.

view this post on Zulip Alex Crichton (Nov 10 2023 at 00:09):

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

Motivation At the moment, there is no way to convert an owned resource handle inside the resource-exporting component back into an owned value of the type that implements the resource. While the sp...

view this post on Zulip Joel Dice (Nov 10 2023 at 00:11):

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.

view this post on Zulip Alex Crichton (Nov 10 2023 at 00:17):

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"

view this post on Zulip Joel Dice (Nov 10 2023 at 00:21):

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.

view this post on Zulip Joel Dice (Nov 10 2023 at 00:27):

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