-
Notifications
You must be signed in to change notification settings - Fork 226
add Resource::take
method to wit_bindgen::rt
#753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This allows the guest to take back ownership of an exported resource from the host; you can think of it as the reverse of `Resource::new`. It's a bit awkward to do this for the time being; WebAssembly/component-model#238 will improve the situation if accepted. Signed-off-by: Joel Dice <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is required and has been tested to work for the WASI-Virt upgrade path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks reasonable to me and just a few cosmetic comments.
crates/guest-rust/src/lib.rs
Outdated
} | ||
|
||
/// Takes back ownership of the object, dropping the resource handle. | ||
pub fn take(resource: Self) -> T |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be rename to into_inner
perhaps? The take
terminology seems derivitive of Option<T>
which is ideally only an implementation detail here.
crates/guest-rust/src/lib.rs
Outdated
@@ -250,7 +250,18 @@ impl<T: WasmResource> Resource<T> { | |||
where | |||
T: RustResource, | |||
{ | |||
let _ = Box::from_raw(rep as *mut T); | |||
let _ = Box::from_raw(rep as *mut Option<T>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be up for a small refactoring of this module? If so I think it'd be good to have something like
type RawRep<T> = Option<T>;
or something like that where all of these casts and various "raw" methods would work with RawRep<T>
. That way future changes to RawRep<T>
would help trigger compilation errors where appropriate. Basically I think it'd be good to centralize the representation of Option<T>
if possible.
crates/rust/src/bindgen.rs
Outdated
@@ -443,7 +443,7 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
.as_deref() | |||
.unwrap() | |||
.to_upper_camel_case(); | |||
format!("&*({op} as u32 as usize as *const {name})") | |||
format!("Option::as_ref(&*({op} as u32 as usize as *const Option<{name}>)).unwrap()") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind adding a helper method to Resource<T>
which is #[doc(hidden)]
to call here? That way this could avoid duplicating knowledge of the internal representation across the code generator and the actual source
- add `type RawRep<T> = Option<T>` alias - rename `Resource::take` to `Resource::into_inner` - add `Resource::lift_borrow` and use it in code generator Signed-off-by: Joel Dice <[email protected]>
@alexcrichton thanks for the feedback; I just pushed and update. I wasn't sure about the scope of your |
👍 |
This allows the guest to take back ownership of an exported resource from the host; you can think of it as the reverse of
Resource::new
. It's a bit awkward to do this for the time being;WebAssembly/component-model#238 will improve the situation if accepted.