Stream: wit-bindgen

Topic: Storing state in `Guest` struct


view this post on Zulip Jonathan H (Aug 01 2025 at 20:32):

Hi, all. I am wondering what the best way to store state in the Guest struct is when implementing the Guest trait after wit_bindgen::generate.

Ideally I would be able to define fields in the struct for which I am defining the impl. However, there is no self paramter. All the functions are associated functions. Is the only option to store state in global variables?

Thank you.

view this post on Zulip Alex Crichton (Aug 01 2025 at 20:44):

Yes you'll have to use global state because these are global functions

view this post on Zulip Jonathan H (Aug 01 2025 at 21:44):

Do you happen to have any examples for a good convention for that? Or is it just something like OnceCell with a mutex?

view this post on Zulip Nathan Bedell (Aug 02 2025 at 14:48):

Hey all. Random question: I'm curious what the general performance characteristics are of passing resource types between components. Can it be expected to be fairly fast?

I'm just wondering because I'm used to the JNI, where doing similar things is generally considered to be fairly slow. Is the situation better for WASM?

The larger motivation behind this is I'm trying to figure out the most efficient way to design a wit world for multi-platform UI development. Currently in my prototype I have a resource type for views basically, but I'm wondering if that's the right approach, or if I should try something else (e.x. passing a single representation of the entire UI layout in a single call by value -- so using something like a DOM represented with variants / records basically).

I know I won't get a more concrete answer until I bench this myself, but I'm curious if anyone had any general thoughts.

view this post on Zulip Nathan Bedell (Aug 02 2025 at 15:02):

Oof, sorry. New to zulip. I think I should have made a new topic for this. :(

view this post on Zulip Celarye (Aug 02 2025 at 15:49):

I had the same question and if I understand it correctly we should be able to use resources for this. The Rust example in this book has a section on implementing resources.

view this post on Zulip Celarye (Aug 02 2025 at 16:01):

I broke the second link, it should be https://component-model.bytecodealliance.org/language-support/rust.html#using-resources.

view this post on Zulip Celarye (Aug 02 2025 at 18:53):

Nevermind, global variables are probably better for this :thinking:


Last updated: Dec 06 2025 at 07:03 UTC