Stream: wit-bindgen

Topic: Using borrow<resource> inside variants


view this post on Zulip Landon James (Mar 28 2024 at 17:27):

I am struggling to model something in WIT and not sure if I am missing something or if what I want to do is currently not supported by the model.

What I want is a container resource that takes ownership of a child resource (which could be one of several types of resources) and then can pass a reference to the child resource. I am modeling this with variant types like so:

package component:%variant;

interface test {
    resource foo-child {
        constructor();
    }

    resource bar-child {
        constructor();
    }

    variant children {
        foo(foo-child),
        bar(bar-child),
        none
    }

    variant borrowed-children {
        foo(borrow<foo-child>),
        bar(borrow<bar-child>),
        none
    }

    resource container {
        constructor(child: children);
        get-child: func() -> borrowed-children;
    }
}

world example {
    export test;
}

But wit-bindgen fails to build with this error:

$ cargo component build --release
  Generating bindings for variant (src/bindings.rs)
thread 'main' panicked at /Users/lnj/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wit-bindgen-rust-0.21.0/src/interface.rs:889:17:
assertion failed: mode.lifetime.is_none()
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The above WIT does work when removing references to the variant borrowed-children, so it seems to be an issue with using borrow<resource> in an unsupported way.

Just curious if there is a way to model this in WIT currently? I think the answer is that I would have to return the borrow<resource> directly from the get-child function which is difficult since I would like my container to be able to hold multiple types of resources.

view this post on Zulip Lann Martin (Mar 28 2024 at 17:30):

I don't know the answer to your question, but it looks like you are a couple of releases behind the latest for wit-bindgen. Could you try upgrading to make sure this isn't a fixed bug?

view this post on Zulip Landon James (Mar 28 2024 at 17:33):

I do not believe I can, at least directly, since I am using the latest version of cargo-component and wit-bindgen is a transitive dependency of that. I can try to quickly rewrite the rust implementing the WIT using wit-bindgen directly though.

view this post on Zulip Lann Martin (Mar 28 2024 at 17:35):

cargo-component also has upgraded dependencies on main if you wanted to try that instead

view this post on Zulip Joel Dice (Mar 28 2024 at 17:36):

Per https://github.com/bytecodealliance/wasm-tools/pull/1469, I don't believe it is possible to return a borrow<_> from a function in the Component Model, so I don't think container.get-child can work.

Currently in the component model it's not valid to have a function that returns a borrow resource. In WIT, however, there's no protection currently against this until a component is actually made. ...

view this post on Zulip Joel Dice (Mar 28 2024 at 17:37):

(We'll have better error diagnostics for such cases once that PR is merged, at least.)

view this post on Zulip Landon James (Mar 28 2024 at 17:44):

Ahh ok, well back to the drawing board for how to model this then, thanks for the pointer!


Last updated: Oct 23 2024 at 20:03 UTC