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:
JsValuein this crate.- Every
#[wasm_bindgen]-imported type (identity — emitted by the macro). - Every generic
js_syscontainer (Array<T>,Promise<T>,Set<T>, …) provides its own identity impl owned byjs_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();