Skip to content

Exporting functions returning resources prevents implementing the guest #330

Closed
@AaronFriel

Description

@AaronFriel

A world exporting a static function that returns a resource does not behave like a constructor, despite documentation. It's not clear how to construct a resource in the guest except via constructor calls.

WIT with exported resource and constructor, vs exported function that returns resource
package aici:abi;

world aici {
  export controller;
}

interface controller {
    resource runner {
        constructor(args: list<u8>);
        init-prompt: func(arg: init-prompt-arg) -> init-prompt-result;
    }
}

Versus

package aici:abi;

world aici {
  export run: (args: list<u8>) -> runner;
}

interface controller {
    resource runner {
        init-prompt: func(arg: init-prompt-arg) -> init-prompt-result;
    }
}

In the former, a GuestRunner trait is declared which enables constructing a GuestRunner on the client, and it's clear how to implement this on the guest.

Comparison of generated Rust from `wit_bindgen`
pub trait GuestRunner:'static{
  ... 
  fn new(args:_rt::Vec::<u8>,) -> Self;
                    
  fn init_prompt(&self,arg:InitPromptArg,) -> InitPromptResult;
}
pub trait Guest {
  type Runner:GuestRunner;
}

In the latter, we instead see:

pub type Runner = aici::abi::controller::Runner;

pub trait Guest {
    fn run(args:_rt::Vec::<u8>,) -> Runner;
} 

Where Runner is an opaque resource handle.

In the latter, there is no way to implement Guest safely because there is no way to obtain a valid Runner handle.

I would expect that when a resource occurs in a return position, the bindings to implement the resource is also generated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions