Skip to main content

IntoJsGeneric

Trait IntoJsGeneric 

Source
pub trait IntoJsGeneric {
    type JsCanon: JsGeneric;

    // Required method
    fn to_js(self) -> Self::JsCanon;
}
Expand description

Value conversion from a type into its canonical JsGeneric form.

This trait allows types to be converted into JsGeneric supported types, which are required to be erasably generic with JsValue.

The single associated type — rather than a free type parameter bounded by AsRef<T> — is what makes collection-style APIs infer annotation-free. Given an input A, there is exactly one A::JsCanon, so rustc never has to search across multiple AsRef impls to pick a target element type.

§Implementations

Provided impls:

  • JsValue in this crate.
  • Every #[wasm_bindgen]-imported type (identity — emitted by the macro).
  • Every generic js_sys container (Array<T>, Promise<T>, Set<T>, …) provides its own identity impl owned by js_sys.
  • References to cloneable implementors, so borrowed iteration can still produce owned JS-generic values.

This trait is deliberately not blanket-implemented over all JsGeneric types: each implementor explicitly opts in, which leaves room for future wrapper types to pick a non-identity Self::JsCanon.

§Example

use js_sys::{Array, Number};

let arr: Array<Number> = (0..10).map(Number::from).collect();

Required Associated Types§

Source

type JsCanon: JsGeneric

The canonical JsGeneric form of this type.

Required Methods§

Source

fn to_js(self) -> Self::JsCanon

Produce the canonical JsGeneric value for self.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: IntoJsGeneric + Clone> IntoJsGeneric for &T

Implementors§