-
Notifications
You must be signed in to change notification settings - Fork 90
CABI: Add a resource.consume
method for owned handles
#238
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
Comments
Good idea, and thanks for the super-clear write-up! I suppose this is technically possible to achieve today by adding a branch to the destructor to make it a no-op in the cases where you'd use |
Thanks! You’re absolutely right that the intended outcome can be achieved already, though with some tricks. In Rust, for instance,
Even though the last two options presents reasonable workarounds that still present a safe interface to users, I still think that an explicit CABI function for invalidating an owned handle without dropping the inner value is a better approach. It avoids both the potential memory overhead and additional unsafety. Overall, I fully agree with your assessment. |
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]>
* add `Resource::take` method to `wit_bindgen::rt` 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]> * address review feedback - 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]> --------- Signed-off-by: Joel Dice <[email protected]>
* add `Resource::take` method to `wit_bindgen::rt` 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]> * address review feedback - 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]> --------- Signed-off-by: Joel Dice <[email protected]>
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 specifics of performing this conversion can be left to wit-bindgen (see, e.g., bytecodealliance/wit-bindgen#641 (comment)), it requires a canonical ABI primitive to consume an owned resource handle.
Detail
The canonical ABI defines a new method,
resource.consume
, which might be implemented as follows:In particular,
resource.consume
, which has a (handwavy) signature offn(owned_handle: i32) -> isize
:resource.drop
resource.rep
Closing Notes
Thank you for considering this extension to the canonical ABI :)
The text was updated successfully, but these errors were encountered: