Skip to main content

JsGeneric

Trait JsGeneric 

Source
pub trait JsGeneric:
    ErasableGeneric<Repr = JsValue>
    + UpcastFrom<Self>
    + Upcast<Self>
    + Upcast<JsValue>
    + 'static { }
Expand description

A convenience trait for types that erase to JsValue.

This is a shorthand for ErasableGeneric<Repr = JsValue>, used as a bound on generic parameters that must be representable as JavaScript values.

§When to Use

Use JsGeneric as a trait bound when you need a generic type that:

  • Can be passed to/from JavaScript
  • Is type-erased to JsValue at the FFI boundary

§Examples

use wasm_bindgen::JsGeneric;

fn process_js_values<T: JsGeneric>(items: &[T]) {
    // T can be any JS-compatible type
}

§Implementors

This trait is automatically implemented for all types that implement ErasableGeneric<Repr = JsValue>, including:

  • All js_sys types (Object, Array, Function, etc.)
  • JsValue itself
  • Custom types imported via #[wasm_bindgen]

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: ErasableGeneric<Repr = JsValue> + UpcastFrom<T> + Upcast<JsValue> + 'static> JsGeneric for T