what happened with the import and export macros? The readme still refers to them but they're gone from current git main
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.
Things are still evolving, but the new syntax looks like
wit_bindgen_guest_rust::generate!({
import: "interface.wit",
name: "foo",
});
Ah, also export
is currently called default
in this syntax, which is non-obvious.
seems you can't call the macro several times in the same file anymore (I want to import host functions and export guest functions)
but that's what worlds would be for I guess?
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.
what's the syntax for multiple parameters to generate? just generate!({...}, {...})?
wit_bindgen_guest_rust::generate!({
import: "red.wit",
import: "green.wit",
import: "blue.wit",
name: "foo",
})
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?
Ah, possibly I'm behind the times here :-)
thanks for your help, this looks promising :)
Update: export: takes the export name from the wit filename while default: uses the provided name as name for the export
so you apparently can have several imports, several exports and one default export
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)
eventually the generate!
macro will consume the world definition instead of having to specify imports and exports yourself, just the world the component targets
oh dan said all this earlier (whoops :flushed:) didn't scroll back
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
what does the export_foo! macro that is being generated do? (assuming name: "foo")
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
it's a way of informing the bindings what code to call for exported functions
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)
that's right
foo
is the name of the world, i believe, in this case
as there's one export macro per generate!
iirc
yes it's the name provided to generate! which probably would be the world name
so it exports all of the exports of the world
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