pub trait JsCast:
Sized
+ AsRef<JsValue>
+ Into<JsValue> {
// Required methods
fn instanceof(val: &JsValue) -> bool;
fn unchecked_from_js(val: JsValue) -> Self;
fn unchecked_from_js_ref(val: &JsValue) -> &Self;
// Provided methods
fn is_type_of(val: &JsValue) -> bool { ... }
fn has_type<T>(&self) -> bool
where T: JsCast { ... }
fn dyn_into<T>(self) -> Result<T, Self>
where T: JsCast { ... }
fn dyn_ref<T>(&self) -> Option<&T>
where T: JsCast { ... }
fn is_instance_of<T>(&self) -> bool
where T: JsCast { ... }
fn unchecked_into<T>(self) -> T
where T: JsCast { ... }
fn unchecked_ref<T>(&self) -> &T
where T: JsCast { ... }
}Expand description
Trait for types that can be cast to and from JsValue.
This is the wry-bindgen equivalent of wasm-bindgen’s JsCast trait.
It enables safe and unsafe casting between JavaScript types.
Required Methods§
Sourcefn instanceof(val: &JsValue) -> bool
fn instanceof(val: &JsValue) -> bool
Check if a JsValue is an instance of this type.
This performs a runtime instanceof check in JavaScript.
Sourcefn unchecked_from_js(val: JsValue) -> Self
fn unchecked_from_js(val: JsValue) -> Self
Unchecked cast from JsValue to this type.
§Safety
This does not perform any runtime checks. The caller must ensure the value is actually of the correct type.
Sourcefn unchecked_from_js_ref(val: &JsValue) -> &Self
fn unchecked_from_js_ref(val: &JsValue) -> &Self
Unchecked cast from a JsValue reference to a reference of this type.
§Safety
This does not perform any runtime checks. The caller must ensure the value is actually of the correct type.
Provided Methods§
Sourcefn is_type_of(val: &JsValue) -> bool
fn is_type_of(val: &JsValue) -> bool
Performs a dynamic type check to see whether the JsValue provided
is a value of this type.
Unlike instanceof, this can be specialized to check for primitive types
or perform other type checks that aren’t possible with instanceof.
The default implementation falls back to instanceof.
Sourcefn has_type<T>(&self) -> boolwhere
T: JsCast,
fn has_type<T>(&self) -> boolwhere
T: JsCast,
Test whether this JS value has a type T.
This method will dynamically check to see if this JS object can be
casted to the JS object of type T. Usually this uses the instanceof
operator, but can be customized with is_type_of. This also works
with primitive types like booleans/strings/numbers as well as cross-realm
objects like Array which can originate from other iframes.
In general this is intended to be a more robust version of
is_instance_of, but if you want strictly the instanceof operator
it’s recommended to use that instead.
Sourcefn dyn_into<T>(self) -> Result<T, Self>where
T: JsCast,
fn dyn_into<T>(self) -> Result<T, Self>where
T: JsCast,
Try to cast this value to type T.
Returns Ok(T) if the value is an instance of T,
otherwise returns Err(self) with the original value.
Sourcefn dyn_ref<T>(&self) -> Option<&T>where
T: JsCast,
fn dyn_ref<T>(&self) -> Option<&T>where
T: JsCast,
Try to get a reference to type T from this value.
Returns Some(&T) if the value is an instance of T,
otherwise returns None.
Sourcefn is_instance_of<T>(&self) -> boolwhere
T: JsCast,
fn is_instance_of<T>(&self) -> boolwhere
T: JsCast,
Test whether this JS value is an instance of the type T.
This method performs a dynamic check (at runtime) using the JS
instanceof operator. This method returns self instanceof T.
Note that instanceof does not always work with primitive values or
across different realms (e.g. iframes). If you’re not sure whether you
specifically need only instanceof it’s recommended to use has_type
instead.
Sourcefn unchecked_into<T>(self) -> Twhere
T: JsCast,
fn unchecked_into<T>(self) -> Twhere
T: JsCast,
Unchecked cast to another type.
Sourcefn unchecked_ref<T>(&self) -> &Twhere
T: JsCast,
fn unchecked_ref<T>(&self) -> &Twhere
T: JsCast,
Unchecked cast to a reference of another type.
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.