Expand description

Downcast a JavaScript wrapper generated by wasm-bindgen back to its original struct.

Examples

use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use wasm_bindgen_downcast::DowncastJS;

#[wasm_bindgen]
#[derive(DowncastJS)]
pub struct Person {
    name: String,
}

/// Try to greet something. If it is the provided value wraps a [`Person`]
/// struct, we'll try to greet them by name.
#[wasm_bindgen]
pub fn greet(maybe_person: &JsValue) -> Option<String> {
    let p = Person::downcast_js_ref(maybe_person)?;
    Some(format!("Hello, {}!", p.name))
}

Structs

A token used when downcasting wrapper classes back to their Rust types.

Traits

Cast a Rust wrapper class that was generated by #[wasm_bindgen] back to the underlying Rust type.

Derive Macros