pub trait TryFromJsValue: Sized {
const NULLABLE: bool = false;
// Required method
fn try_from_js_value_ref(value: &JsValue) -> Option<Self>;
// Provided method
fn try_from_js_value(value: JsValue) -> Result<Self, JsValue> { ... }
}Expand description
TryFromJsValue is a trait for converting a JavaScript value (JsValue)
into a Rust type. It is used by the wasm_bindgen
proc-macro to allow conversion to user types.
The semantics of this trait for various types are designed to provide a runtime analog of the static semantics implemented by the IntoWasmAbi function bindgen, with the exception that conversions are constrained to not cast invalid types.
For example, where the Wasm static semantics will permit foo(x: i32) when passed
from JS foo("5") to treat that as foo(5), this trait will instead throw. Apart
from these reduced type conversion cases, behaviours should otherwise match the
static semantics.
Types implementing this trait must specify their conversion logic from
JsValue to the Rust type, handling any potential errors that may occur
during the conversion process.
§⚠️ Unstable
This is part of the internal convert module, no
stability guarantees are provided. Use at your own risk. See its
documentation for more details.
Provided Associated Constants§
Sourceconst NULLABLE: bool = false
const NULLABLE: bool = false
Whether Self accepts null as a distinct value space, as
Nullable<T> does. An enclosing Option then passes null through
instead of reading it as None, so Option<T> is itself nullable
exactly when T is not. JsValue does not set this: it accepts
null as one value among all of them, not as a distinct space, and
Option<JsValue> reads it as None like the ABI does. This is the
JS-value side of NullableFromWasmAbi, which claims null at the
ABI.
Required Methods§
Sourcefn try_from_js_value_ref(value: &JsValue) -> Option<Self>
fn try_from_js_value_ref(value: &JsValue) -> Option<Self>
Performs the conversion.
Provided Methods§
Sourcefn try_from_js_value(value: JsValue) -> Result<Self, JsValue>
fn try_from_js_value(value: JsValue) -> Result<Self, JsValue>
Performs the conversion.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".