wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

[Profiles] Support for linking

Open kulakowski-wasm opened this issue 2 years ago • 5 comments

kulakowski-wasm avatar Jun 24 '22 23:06 kulakowski-wasm

Would this include as a goal that WIT type definitions shared by multiple imports/exports generate shared code/types instead of separate redundant ones?

esoterra avatar Jun 30 '22 19:06 esoterra

Yes, that's the goal here. We need to be able to use types from one interface in another interface. For example, given:

produce.wit:

record info {
    field: u32
}

produce-info: func() -> info

consume.wit:

use { info } from produce

consume-info: func(i: info)

and main.rs

wit_bindgen_rust::import!("produce.wit");
wit_bindgen_rust::import!("consume.wit");

fn main() -> std::io::Result<()> {
    let info = produce::produce_info();
    consume::consume_info(info);
    Ok(())
}

This currently gets an error: "expected struct consume::Info, found struct produce::Info".

I think the solution here is profiles. Once implemented, we'll be able to define a profile that pulls in multiple wit files, and then wit-bindgen can generate a linked bindings module for them.

sunfishcode avatar Jul 06 '22 00:07 sunfishcode

I like that framing, I think profiles will be a good way to connect this all together and present it as singular input to the code generator.

esoterra avatar Jul 06 '22 01:07 esoterra

I've arbitrarily chosen this as the blessed issue for "use does not work as one might expect", especially with resources, so more information about this issue can be learned from the litany of referenced issues as well.

alexcrichton avatar Sep 06 '22 18:09 alexcrichton

I'm also closing the related #191.

theduke avatar Sep 11 '22 13:09 theduke

In the example above, why not define

messaging.wit :    
   record Info { field: u32 }

so that it generates Messaging.Info

and have both producer and consumer import the common struct ? It is a shared struct - why not define it in an interface library of shared types?

stevelr avatar Dec 15 '22 21:12 stevelr

I believe that this is now done as of https://github.com/bytecodealliance/wit-bindgen/pull/457, so I'm going to close this.

alexcrichton avatar Jan 25 '23 18:01 alexcrichton