Trait JsCast
pub trait JsCast
where
Self: AsRef<JsValue> + Into<JsValue>,
A trait for dynamic checked and unchecked casting between JS types.
Unlike crate::Upcast, which provides type-safe zero-cost type
conversions for generic type wrappers, this trait can be used to
perform arbitrary casts with JS instance checking.
Specified in an RFC this trait is intended to provide support for casting JS values between different types of one another. In JS there aren't many static types but we've ascribed JS values with static types in Rust, yet they often need to be switched to other types temporarily! This trait provides both checked and unchecked casting into various kinds of values.
This trait is automatically implemented for any type imported in a
#[wasm_bindgen] extern block.
Required Methods
fn instanceof(val: &JsValue) -> boolPerforms a dynamic
instanceofcheck to see whether theJsValueprovided is an instance of this type.This is intended to be an internal implementation detail, you likely won't need to call this. It's generally called through the
is_instance_ofmethod instead.fn unchecked_from_js(val: JsValue) -> SelfPerforms a zero-cost unchecked conversion from a
JsValueinto an instance ofSelfThis is intended to be an internal implementation detail, you likely won't need to call this.
fn unchecked_from_js_ref(val: &JsValue) -> &SelfPerforms a zero-cost unchecked conversion from a
&JsValueinto an instance of&Self.Note the safety of this method, which basically means that
Selfmust be a newtype wrapper aroundJsValue.This is intended to be an internal implementation detail, you likely won't need to call this.
Provided Methods
fn has_type<T>(&self) -> bool where 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 theinstanceofoperator. This also works with primitive types like booleans/strings/numbers as well as cross-realm object likeArraywhich 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 theinstanceofoperator it's recommended to use that instead.fn dyn_into<T>(self) -> Result<T, Self> where T: JsCast,Performs a dynamic cast (checked at runtime) of this value into the target type
T.This method will return
Err(self)ifself.has_type::<T>()returnsfalse, and otherwise it will returnOk(T)manufactured with an unchecked cast (verified correct via thehas_typeoperation).fn dyn_ref<T>(&self) -> Option<&T> where T: JsCast,Performs a dynamic cast (checked at runtime) of this value into the target type
T.This method will return
Noneifself.has_type::<T>()returnsfalse, and otherwise it will returnSome(&T)manufactured with an unchecked cast (verified correct via thehas_typeoperation).fn unchecked_into<T>(self) -> T where T: JsCast,Performs a zero-cost unchecked cast into the specified type.
This method will convert the
selfvalue to the typeT, where bothselfandTare simple wrappers aroundJsValue. This method does not check whetherselfis an instance ofT. If used incorrectly then this method may cause runtime exceptions in both Rust and JS, this should be used with caution.fn unchecked_ref<T>(&self) -> &T where T: JsCast,Performs a zero-cost unchecked cast into a reference to the specified type.
This method will convert the
selfvalue to the typeT, where bothselfandTare simple wrappers aroundJsValue. This method does not check whetherselfis an instance ofT. If used incorrectly then this method may cause runtime exceptions in both Rust and JS, this should be used with caution.This method, unlike
unchecked_into, does not consume ownership ofselfand instead works over a shared reference.fn is_instance_of<T>(&self) -> bool where 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
instanceofoperator. This method returnsself instanceof T.Note that
instanceofdoes not always work with primitive values or across different realms (e.g. iframes). If you're not sure whether you specifically need onlyinstanceofit's recommended to usehas_typeinstead.fn is_type_of(val: &JsValue) -> boolPerforms a dynamic check to see whether the
JsValueprovided is a value of this type.Unlike
instanceof, this can be specialised to use a custom check by adding a#[wasm_bindgen(is_type_of = callback)]attribute to the type import declaration.Other than that, this is intended to be an internal implementation detail of
has_typeand you likely won't need to call this.