TryFromJsValue

Trait TryFromJsValue 

Source
pub trait TryFromJsValue: Sized {
    type Error;

    // Required method
    fn try_from_js_value(value: JsValue) -> Result<Self, Self::Error>;
}
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.

Required Associated Types§

Source

type Error

The type returned in the event of a conversion error.

Required Methods§

Source

fn try_from_js_value(value: JsValue) -> Result<Self, Self::Error>

Performs the conversion.

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.

Implementations on Foreign Types§

Source§

impl TryFromJsValue for bool

Source§

impl TryFromJsValue for char

Source§

impl TryFromJsValue for f32

Source§

impl TryFromJsValue for f64

Source§

impl TryFromJsValue for i8

Source§

impl TryFromJsValue for i16

Source§

impl TryFromJsValue for i32

Source§

impl TryFromJsValue for i64

Source§

impl TryFromJsValue for i128

Source§

impl TryFromJsValue for isize

Source§

impl TryFromJsValue for u8

Source§

impl TryFromJsValue for u16

Source§

impl TryFromJsValue for u32

Source§

impl TryFromJsValue for u64

Source§

impl TryFromJsValue for u128

Source§

impl TryFromJsValue for ()

Source§

impl TryFromJsValue for usize

Source§

impl TryFromJsValue for String

Source§

impl<T: TryFromJsValue> TryFromJsValue for Option<T>

Implementors§

Source§

impl<T> TryFromJsValue for T
where T: JsCast,