Skip to main content

wasm_export

Attribute Macro wasm_export 

Source
#[wasm_export]
Expand description

Exports an impl block or a free function to JavaScript, in place of #[cfg_attr(feature = "wasm", wasm_bindgen(..))].

Arguments are forwarded to #[wasm_bindgen], so #[wasm_export(js_class = Foo)] behaves as it would there. The item must not be generic; wasm_bindgen cannot export generic items.

§#[wasm_only]

Marks a method whose only intended caller is JavaScript, because Rust has a better API for the same thing. The method is renamed with a __wasm_only_ prefix, hidden from documentation, and marked #[deprecated] so it shows struck through in IDE autocomplete. Takes an optional note = "..." for that deprecation. The JS name is unaffected: the original name is declared as the method’s js_name, unless it already declares one.

§Example

ⓘ
#[wasm_export(js_class = IpcClient)]
impl JsIpcClient {
    pub async fn send(&self, message: OutgoingMessage) -> Result<(), SendError> { ... }

    // Exported to JavaScript, but struck through for Rust — use `IpcClient::start` instead.
    #[wasm_only(note = "Use `IpcClient::start`.")]
    pub async fn start(&self) -> Result<(), AlreadyRunningError> { ... }
}