Skip to content

rust: sharing type definitions across multiple interfaces #1415

@chenyan2002

Description

@chenyan2002

Consider the following WIT file:

package test:component;

interface i {
  resource r;
  enum kind { a, b, c }
  f: func(k: kind) -> r;
}
// wrapped-i is exactly the same as i
interface wrapped-i {
  resource r;
  enum kind { a, b, c }
  f: func(k: kind) -> r;
}

world root {
  import i;
  import wrapped-i;
  export i;
  export wrapped-i;
}

In wit-bindgen, kind will be defined four times: test::component::i::Kind, test::component::wrapped_i::Kind, exports::test::component::i::Kind, and exports::test::component::wrapped_i::Kind. It would be good to have an option to say that these four kind are the same, and we only define it once, the remaining module will use type alias instead. This avoids value cloning when we want to pass kind from one interface to another. This only works for types which contain no resources. With the lazy value proposal, this may change though.

I can see three options:

  1. add a interface_group flag, so that we can say import.i, import.wrapped-i, export.i, export.wrapped-i are in the same group, and the bindgen will find all non-resource types in the interface and make sure they are defined only once.

  2. We could also fix this by extending the WIT syntax, something like

world root {
  import i;
  import i as wrapped-i;
  export i;
  export i as wrapped-i
}

Then wit-bindgen will know that the non-resource types in these four interfaces are the same just by looking at the WIT file.

  1. add a type_eq flag, and say import.i.kind, import.wrapped-i.kind, export.i.kind and export.wrapped-i.kind are equal. This allows more fine grained control, and can handle cases where the two interfaces are not exactly the same, but we still want to share some data types.

It feels like that the interface level equality is better fixed from the WIT syntax, but the type level equality is a suitable extension in wit-bindgen alone and can also cover the interface equality case without waiting for a syntax change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    gen-rustRelated to bindings for Rust-compiled-to-WebAssembly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions