Skip to main content

wasm_state

Macro wasm_state 

Source
macro_rules! wasm_state {
    ($T:ty) => { ... };
}
Expand description

Generates thread-local state management boilerplate for WASM targets.

When building a Tauri game for the web, you need to replace State<Mutex<T>> (which requires threads) with thread-local storage (WASM is single-threaded). This macro generates the required thread_local! + RefCell pattern and accessor functions.

§Usage

use webtau::wasm_state;

struct GameWorld {
    score: i32,
}

wasm_state!(GameWorld);

§Generated API

  • set_state(val: T) — Initialize or replace the state.
  • with_state(|state| ...) — Read-only access to the state.
  • with_state_mut(|state| ...) — Mutable access to the state.

All three functions panic if called before set_state().