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?
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
Karel Hrkal (kajacx) has marked this topic as resolved.
Last updated: Nov 22 2024 at 16:03 UTC