Stream: wit-bindgen

Topic: wit-bindgen-guest-rust missing import and export macros?


view this post on Zulip Ramon Klass (Nov 02 2022 at 20:03):

what happened with the import and export macros? The readme still refers to them but they're gone from current git main

view this post on Zulip Dan Gohman (Nov 02 2022 at 20:10):

They're changing appearance as part of the move to support Worlds. Part of what this is needed for is that if you generate bindings for multiple interfaces that use the same time, you want the bindings to use the same type too, so they need to be linked with each other.

view this post on Zulip Dan Gohman (Nov 02 2022 at 20:10):

Things are still evolving, but the new syntax looks like

wit_bindgen_guest_rust::generate!({
    import: "interface.wit",
    name: "foo",
});

view this post on Zulip Dan Gohman (Nov 02 2022 at 20:16):

Ah, also export is currently called default in this syntax, which is non-obvious.

view this post on Zulip Ramon Klass (Nov 02 2022 at 20:17):

seems you can't call the macro several times in the same file anymore (I want to import host functions and export guest functions)

view this post on Zulip Ramon Klass (Nov 02 2022 at 20:17):

but that's what worlds would be for I guess?

view this post on Zulip Dan Gohman (Nov 02 2022 at 20:21):

Yeah, at least in typical usage, you list all your imports and exports in a single macro invocation, and it generates unified bindings for all of them at once.

view this post on Zulip Ramon Klass (Nov 02 2022 at 20:22):

what's the syntax for multiple parameters to generate? just generate!({...}, {...})?

view this post on Zulip Dan Gohman (Nov 02 2022 at 20:23):

wit_bindgen_guest_rust::generate!({
    import: "red.wit",
    import: "green.wit",
    import: "blue.wit",
    name: "foo",
})

view this post on Zulip Ramon Klass (Nov 02 2022 at 20:27):

example with import and export:

wit_bindgen_guest_rust::generate!({
    import: "../exports/host.wit",
    export: "../exports/guest.wit",
    name: "foo"
});

seems to work, what did you mean with default?

view this post on Zulip Dan Gohman (Nov 02 2022 at 20:29):

Ah, possibly I'm behind the times here :-)

view this post on Zulip Ramon Klass (Nov 02 2022 at 20:33):

thanks for your help, this looks promising :)

view this post on Zulip Ramon Klass (Nov 02 2022 at 20:41):

Update: export: takes the export name from the wit filename while default: uses the provided name as name for the export

view this post on Zulip Ramon Klass (Nov 02 2022 at 20:41):

so you apparently can have several imports, several exports and one default export

view this post on Zulip Peter Huene (Nov 02 2022 at 20:49):

That's correct; the macro is migrating to a "world" view of generation, where what the component expects for its imports and exports is described as a single unit; so there's only one macro call for all the code generation. eventually this mechanism will allow the code generated to share type definitions (e.g. a.wit and b.wit make use of types coming from c.wit; the same generated types would appear in both a and b signatures)

view this post on Zulip Peter Huene (Nov 02 2022 at 20:50):

eventually the generate! macro will consume the world definition instead of having to specify imports and exports yourself, just the world the component targets

view this post on Zulip Peter Huene (Nov 02 2022 at 20:53):

oh dan said all this earlier (whoops :flushed:) didn't scroll back

view this post on Zulip Peter Huene (Nov 02 2022 at 20:56):

regarding the "default" export, it's the interface which will be directly exported by the component in the end, so if you export foo, you get an instance export named foo with the component's foo implementation exported from that instance; if you "default" export foo, the functions of foo are exported directly instead of via an instance

view this post on Zulip Ramon Klass (Nov 02 2022 at 20:58):

what does the export_foo! macro that is being generated do? (assuming name: "foo")

view this post on Zulip Peter Huene (Nov 02 2022 at 20:59):

it exports functions from the resulting core wasm module (before it gets compotentized) that adhere to the component model's canonical ABI; those exports call your user code via the traits the given type implements

view this post on Zulip Peter Huene (Nov 02 2022 at 20:59):

it's a way of informing the bindings what code to call for exported functions

view this post on Zulip Ramon Klass (Nov 02 2022 at 21:00):

so I need to impl foo::Foo for SomeStruct and export_foo!(SomeStruct) if I want those functions to be called? (guessing purely from "the compiler said it would run and macro expand looks sensible)

view this post on Zulip Peter Huene (Nov 02 2022 at 21:00):

that's right

view this post on Zulip Peter Huene (Nov 02 2022 at 21:01):

foo is the name of the world, i believe, in this case

view this post on Zulip Peter Huene (Nov 02 2022 at 21:01):

as there's one export macro per generate! iirc

view this post on Zulip Ramon Klass (Nov 02 2022 at 21:01):

yes it's the name provided to generate! which probably would be the world name

view this post on Zulip Peter Huene (Nov 02 2022 at 21:01):

so it exports all of the exports of the world

view this post on Zulip Peter Huene (Nov 02 2022 at 21:06):

for what it's worth, the cargo component tooling renames it to simply export! as the "world" it uses is based off of things in your Cargo.toml


Last updated: Dec 23 2024 at 12:05 UTC