Stream: wit-bindgen

Topic: ✔ export_host! macro


view this post on Zulip alisomay (Jun 29 2023 at 15:09):

Hello all.
I'm having trouble to find export_host! macro which was stated in the Rust example.

error: cannot find macro `export_host` in this scope
  --> rust_module/src/lib.rs:18:1
   |
18 | export_host!(MyTransformer);
   | ^^^^^^^^^^^

I couldn't either spot it in the wit-bindgen code base.
Would you point me to the right direction?
Thanks for your help in advance.

view this post on Zulip Robin Brown (Jun 29 2023 at 15:10):

Where is the example from?

view this post on Zulip alisomay (Jun 29 2023 at 15:10):

It is from the README.md in wit-bindgen code base.

view this post on Zulip alisomay (Jun 29 2023 at 15:11):

I slightly modified the names but it is the same.
The original example is

// src/lib.rs

// Use a procedural macro to generate bindings for the world we specified in
// `host.wit`
wit_bindgen::generate!("host");

// Define a custom type and implement the generated `Host` trait for it which
// represents implementing all the necesssary exported interfaces for this
// component.
struct MyHost;

impl Host for MyHost {
    fn run() {
        print("Hello, world!");
    }
}

export_host!(MyHost);

view this post on Zulip alisomay (Jun 29 2023 at 15:12):

Which is trying to generate from this wit I suppose.

// wit/host.wit
package example:host

world host {
  import print: func(msg: string)

  export run: func()
}

view this post on Zulip Robin Brown (Jun 29 2023 at 15:17):

You won't find a macro named export_host in the wit-bindgen repo because wit-bindgen creates that macro when you use it to generate and the name is formatted as export_{} where the parameter is the name of the world which just happens to be host in this case.

view this post on Zulip Robin Brown (Jun 29 2023 at 15:18):

https://github.com/bytecodealliance/wit-bindgen/blob/0cc1e99c2682f421d8c81145fb72f13cf28be12a/crates/rust/src/lib.rs#L261

A language binding generator for WebAssembly interface types - wit-bindgen/crates/rust/src/lib.rs at 0cc1e99c2682f421d8c81145fb72f13cf28be12a · bytecodealliance/wit-bindgen

view this post on Zulip alisomay (Jun 29 2023 at 15:18):

Now I got it :)
Since the host is such a generic name it confused me.
Thanks for the help!

view this post on Zulip Robin Brown (Jun 29 2023 at 15:18):

you're welcome!

view this post on Zulip alisomay (Jun 29 2023 at 15:31):

Here is a PR which imo clarifies this potential confusion, if you'd like to consider.
https://github.com/bytecodealliance/wit-bindgen/pull/599

Improves the documentation on export_host! macro in the Rust guest example. Since host seems to be a generic name one can confuse this generated macro with a macro which should come from wit-bindge...

view this post on Zulip Notification Bot (Jul 04 2023 at 14:48):

alisomay has marked this topic as resolved.


Last updated: Dec 23 2024 at 12:05 UTC