Byte-Naut opened PR #14434 from Byte-Naut:issue-9946 to bytecodealliance:main:
Closes #9946.
Motivation
@tliron pointed out in #9946 that the exported resources example only gets a
ResourceAnyfrom a constructor, so it doesn't show what to do with a guest resource returned by an ordinary function, how to hand it back to the guest, or whether the host has to callresource_dropon it. TheResourceAnydocs also didn't say thattry_into_resourceonly works for host-defined resources. The ownership rules were explained in the thread, but they aren't written down in the docs or shown in the example yet. This follows @tliron's suggestion to show a resource both passed to and returned from exported functions.Changes
- The example's WIT gains three free functions:
get-default-loggerreturns an ownedlogger,inspect-loggertakesborrow<logger>, andtake-loggertakes an ownedlogger.- The example walks through them: it gets a returned logger, borrows it, calls a method on it, and drops it; then it constructs another logger and transfers it to the guest instead of dropping it. The comments point out that both parameter kinds are
ResourceAnyin Rust, so the WIT signature decides ownership, and thatResourceAnybeingCopydoesn't make a transferred handle reusable.- The
ResourceAnydocs are split into guest-defined, host-defined, and "Ownership and destruction" sections. The typed conversions now say they only accept host-defined resources, and the drop rule lists passing to anownparameter alongsideresource_dropand conversion.Documentation only: no runtime or public API changes. The
Guestbindings in the example module gain three methods from the new WIT.Testing
cargo test -p wasmtime --doc _6_exported_resources # with RUSTDOCFLAGS="--cfg docsrs"Locally: fmt, and
cargo doc -p wasmtimeboth with and without--cfg docsrs, with no new rustdoc warnings. I also ran the existingpass_guest_back_as_borrow,manually_destroy, andresource_drop_concurrent_in_run_concurrenttests, which cover the behavior the new docs describe.
resource_drop_concurrent, which came out of @SilverMira's comment here via #14291, was added in #14362, so the drop docs there already list all three variants.Developed with assistance from Claude (Anthropic). Per the Bytecode Alliance AI Tool Use Policy, I'm the author and accountable for this change.
Byte-Naut requested fitzgen for a review on PR #14434.
Byte-Naut requested wasmtime-core-reviewers for a review on PR #14434.
github-actions[bot] added the label wasmtime:api on PR #14434.
alexcrichton unassigned fitzgen from PR #14434 component: document ownership of exported guest resources.
alexcrichton requested alexcrichton for a review on PR #14434.
:thumbs_up: alexcrichton submitted PR review:
Thanks for the doc updates here it's much appreciated! In the abstract the
bindgen_examplesmodule is a bit lacking, so if you've got ideas of more examples to add there I think that'd be great!
alexcrichton added PR #14434 component: document ownership of exported guest resources to the merge queue.
:check: alexcrichton merged PR #14434.
alexcrichton removed PR #14434 component: document ownership of exported guest resources from the merge queue.
Last updated: Oct 11 2026 at 04:10 UTC