wasm_bindgen

Trait JsCast

Source
pub trait JsCast
where Self: 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 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 unchecked_into<T>(self) -> T where T: JsCast { ... } fn unchecked_ref<T>(&self) -> &T where T: JsCast { ... } fn is_instance_of<T>(&self) -> bool where T: JsCast { ... } fn is_type_of(val: &JsValue) -> bool { ... } }
Expand description

A trait for checked and unchecked casting between JS types.

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§

Source

fn instanceof(val: &JsValue) -> bool

Performs a dynamic instanceof check to see whether the JsValue provided 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_of method instead.

Source

fn unchecked_from_js(val: JsValue) -> Self

Performs a zero-cost unchecked conversion from a JsValue into an instance of Self

This is intended to be an internal implementation detail, you likely won’t need to call this.

Source

fn unchecked_from_js_ref(val: &JsValue) -> &Self

Performs a zero-cost unchecked conversion from a &JsValue into an instance of &Self.

Note the safety of this method, which basically means that Self must be a newtype wrapper around JsValue.

This is intended to be an internal implementation detail, you likely won’t need to call this.

Provided Methods§

Source

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 the instanceof operator. This also works with primitive types like booleans/strings/numbers as well as cross-realm object 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.

Source

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) if self.has_type::<T>() returns false, and otherwise it will return Ok(T) manufactured with an unchecked cast (verified correct via the has_type operation).

Source

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 None if self.has_type::<T>() returns false, and otherwise it will return Some(&T) manufactured with an unchecked cast (verified correct via the has_type operation).

Source

fn unchecked_into<T>(self) -> T
where T: JsCast,

Performs a zero-cost unchecked cast into the specified type.

This method will convert the self value to the type T, where both self and T are simple wrappers around JsValue. This method does not check whether self is an instance of T. If used incorrectly then this method may cause runtime exceptions in both Rust and JS, this should be used with caution.

Source

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 self value to the type T, where both self and T are simple wrappers around JsValue. This method does not check whether self is an instance of T. 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 of self and instead works over a shared reference.

Source

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 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.

Source

fn is_type_of(val: &JsValue) -> bool

Performs a dynamic check to see whether the JsValue provided 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_type and you likely won’t need to call this.

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.

Implementors§

Source§

impl JsCast for JsValue

impl JsCast for Collator

impl JsCast for Exception

impl JsCast for Global

impl JsCast for Instance

impl JsCast for LinkError

impl JsCast for Memory

impl JsCast for Module

impl JsCast for Table

impl JsCast for Tag

impl JsCast for Array

impl JsCast for BigInt

impl JsCast for Boolean

impl JsCast for DataView

impl JsCast for Date

impl JsCast for Error

impl JsCast for EvalError

impl JsCast for Function

impl JsCast for Generator

impl JsCast for Int8Array

impl JsCast for Iterator

impl JsCast for JsString

impl JsCast for Map

impl JsCast for Number

impl JsCast for Object

impl JsCast for Promise

impl JsCast for Proxy

impl JsCast for RegExp

impl JsCast for Set

impl JsCast for Symbol

impl JsCast for TypeError

impl JsCast for UriError

impl JsCast for WeakMap

impl JsCast for WeakSet

impl JsCast for Algorithm

impl JsCast for Animation

impl JsCast for Attr

impl JsCast for AudioData

impl JsCast for AudioNode

impl JsCast for BarProp

impl JsCast for Blob

impl JsCast for BlobEvent

impl JsCast for Bluetooth

impl JsCast for Cache

impl JsCast for Client

impl JsCast for Clients

impl JsCast for Clipboard

impl JsCast for Comment

impl JsCast for Crypto

impl JsCast for CryptoKey

impl JsCast for CssRule

impl JsCast for DelayNode

impl JsCast for Directory

impl JsCast for Document

impl JsCast for DomError

impl JsCast for DomMatrix

impl JsCast for DomParser

impl JsCast for DomPoint

impl JsCast for DomQuad

impl JsCast for DomRect

impl JsCast for DragEvent

impl JsCast for Element

impl JsCast for Event

impl JsCast for EventInit

impl JsCast for Exception

impl JsCast for ExtSRgb

impl JsCast for External

impl JsCast for File

impl JsCast for FileList

impl JsCast for FontData

impl JsCast for FontFace

impl JsCast for FormData

impl JsCast for GainNode

impl JsCast for Gamepad

impl JsCast for Gpu

impl JsCast for GpuBuffer

impl JsCast for GpuDevice

impl JsCast for GpuError

impl JsCast for GpuQueue

impl JsCast for Headers

impl JsCast for Hid

impl JsCast for HidDevice

impl JsCast for History

impl JsCast for IdbCursor

impl JsCast for IdbIndex

impl JsCast for ImageData

impl JsCast for IntlUtils

impl JsCast for KeyEvent

impl JsCast for L10nValue

impl JsCast for Location

impl JsCast for Lock

impl JsCast for LockInfo

impl JsCast for MediaKeys

impl JsCast for MediaList

impl JsCast for MidiInput

impl JsCast for MidiPort

impl JsCast for MimeType

impl JsCast for MozDebug

impl JsCast for Navigator

impl JsCast for Node

impl JsCast for NodeList

impl JsCast for Path2d

impl JsCast for Plugin

impl JsCast for Position

impl JsCast for PushEvent

impl JsCast for Range

impl JsCast for Request

impl JsCast for Response

impl JsCast for RtcStats

impl JsCast for Scheduler

impl JsCast for Screen

impl JsCast for Selection

impl JsCast for Serial

impl JsCast for ShareData

impl JsCast for Storage

impl JsCast for SvgAngle

impl JsCast for SvgLength

impl JsCast for SvgMatrix

impl JsCast for SvgNumber

impl JsCast for SvgPoint

impl JsCast for SvgRect

impl JsCast for TcpSocket

impl JsCast for Text

impl JsCast for TextTrack

impl JsCast for TimeEvent

impl JsCast for Touch

impl JsCast for TouchInit

impl JsCast for TouchList

impl JsCast for TreeView

impl JsCast for U2f

impl JsCast for UiEvent

impl JsCast for Url

impl JsCast for Usb

impl JsCast for UsbDevice

impl JsCast for VrDisplay

impl JsCast for VrLayer

impl JsCast for VrPose

impl JsCast for VttCue

impl JsCast for VttRegion

impl JsCast for WakeLock

impl JsCast for WebGlSync

impl JsCast for WebSocket

impl JsCast for Window

impl JsCast for Worker

impl JsCast for Worklet

impl JsCast for XrFrame

impl JsCast for XrHand

impl JsCast for XrLayer

impl JsCast for XrPose

impl JsCast for XrSession

impl JsCast for XrSpace

impl JsCast for XrSystem

impl JsCast for XrView