Stream: wit-bindgen

Topic: ✔ Is it possible to return borrowed resource from a funct...


view this post on Zulip Karel Hrkal (kajacx) (May 09 2024 at 18:06):

Hello, I was trying out resources but when I tried to add a simple "roundtrip" function that would just return a borrowed resource, I got an error. This is the wit file (simplified):

package component-test:wit-protocol;

interface employees {
  resource employee-res {
    constructor(name: string, min-salary: u32);
    get-name: func() -> string;
    set-name: func(name: string);
    get-min-salary: func() -> u32;
  }
}

interface guest-fns {
  use employees.{employee-res};

  // Works fine
  employee-roundtrip: func(employee: employee-res) -> employee-res;

  // Doesn't work
  employee-borrow-roundtrip: func(employee: string) -> borrow<employee-res>;
}

world resources {
  export employees;
  export guest-fns;
}

And this is the error that I get:

proc macro panicked
 --> src\lib.rs:3:1
3 | / wit_bindgen::generate!({
4 | |     path: "../protocol.wit",
5 | |     world: "resources",
6 | | });
  | |__^
  = help: message: assertion failed: mode.lifetime.is_none()

After looking into the print_results method it looks like the return type must be an owned type with no lifetime, so returning a resource borrow is impossible? Or am I missing something?

A language binding generator for WebAssembly interface types - bytecodealliance/wit-bindgen

view this post on Zulip Alex Crichton (May 09 2024 at 18:17):

Not currently, no. Borrowed resources returned from functions are always considered invalid in the compoent model.

Historically tooling relied on deferring this error to the validator itself which led to weird error messages like you're seeing above. Tools nowadays should check this themselves and present a more first-class error

view this post on Zulip Notification Bot (May 09 2024 at 18:23):

Karel Hrkal (kajacx) has marked this topic as resolved.


Last updated: Dec 23 2024 at 12:05 UTC