Trait wgpu::web_sys::js_sys::wasm_bindgen::prelude::JsCast

source ·
pub trait JsCast: 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 { ... }
}
Available on webgl or webgpu only.
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.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl JsCast for AngleInstancedArrays

source§

impl JsCast for Document

source§

impl JsCast for Element

source§

impl JsCast for Event

source§

impl JsCast for EventTarget

source§

impl JsCast for ExtBlendMinmax

source§

impl JsCast for ExtColorBufferFloat

source§

impl JsCast for ExtColorBufferHalfFloat

source§

impl JsCast for ExtDisjointTimerQuery

source§

impl JsCast for ExtFragDepth

source§

impl JsCast for ExtSRgb

source§

impl JsCast for ExtShaderTextureLod

source§

impl JsCast for ExtTextureFilterAnisotropic

source§

impl JsCast for HtmlCanvasElement

source§

impl JsCast for HtmlElement

source§

impl JsCast for HtmlImageElement

source§

impl JsCast for HtmlMediaElement

source§

impl JsCast for HtmlVideoElement

source§

impl JsCast for ImageBitmap

source§

impl JsCast for ImageBitmapRenderingContext

source§

impl JsCast for Navigator

source§

impl JsCast for Node

source§

impl JsCast for NodeList

source§

impl JsCast for OesElementIndexUint

source§

impl JsCast for OesStandardDerivatives

source§

impl JsCast for OesTextureFloat

source§

impl JsCast for OesTextureFloatLinear

source§

impl JsCast for OesTextureHalfFloat

source§

impl JsCast for OesTextureHalfFloatLinear

source§

impl JsCast for OesVertexArrayObject

source§

impl JsCast for OffscreenCanvas

source§

impl JsCast for OvrMultiview2

source§

impl JsCast for WebGl2RenderingContext

source§

impl JsCast for WebGlActiveInfo

source§

impl JsCast for WebGlBuffer

source§

impl JsCast for WebGlFramebuffer

source§

impl JsCast for WebGlProgram

source§

impl JsCast for WebGlQuery

source§

impl JsCast for WebGlRenderbuffer

source§

impl JsCast for WebGlRenderingContext

source§

impl JsCast for WebGlSampler

source§

impl JsCast for WebGlShader

source§

impl JsCast for WebGlSync

source§

impl JsCast for WebGlTexture

source§

impl JsCast for WebGlTransformFeedback

source§

impl JsCast for WebGlUniformLocation

source§

impl JsCast for WebGlVertexArrayObject

source§

impl JsCast for WebglColorBufferFloat

source§

impl JsCast for WebglCompressedTextureAstc

source§

impl JsCast for WebglCompressedTextureEtc1

source§

impl JsCast for WebglCompressedTextureEtc

source§

impl JsCast for WebglCompressedTexturePvrtc

source§

impl JsCast for WebglCompressedTextureS3tc

source§

impl JsCast for WebglCompressedTextureS3tcSrgb

source§

impl JsCast for WebglDebugRendererInfo

source§

impl JsCast for WebglDebugShaders

source§

impl JsCast for WebglDepthTexture

source§

impl JsCast for WebglDrawBuffers

source§

impl JsCast for WebglLoseContext

source§

impl JsCast for Window

source§

impl JsCast for WorkerGlobalScope

source§

impl JsCast for WorkerNavigator

source§

impl JsCast for Collator

source§

impl JsCast for DateTimeFormat

source§

impl JsCast for NumberFormat

source§

impl JsCast for PluralRules

source§

impl JsCast for RelativeTimeFormat

source§

impl JsCast for CompileError

source§

impl JsCast for Exception

source§

impl JsCast for Global

source§

impl JsCast for Instance

source§

impl JsCast for LinkError

source§

impl JsCast for Memory

source§

impl JsCast for Module

source§

impl JsCast for RuntimeError

source§

impl JsCast for Table

source§

impl JsCast for Tag

source§

impl JsCast for Array

source§

impl JsCast for ArrayBuffer

source§

impl JsCast for AsyncIterator

source§

impl JsCast for BigInt64Array

source§

impl JsCast for BigInt

source§

impl JsCast for BigUint64Array

source§

impl JsCast for Boolean

source§

impl JsCast for DataView

source§

impl JsCast for Date

source§

impl JsCast for Error

source§

impl JsCast for EvalError

source§

impl JsCast for Float32Array

source§

impl JsCast for Float64Array

source§

impl JsCast for Function

source§

impl JsCast for Generator

source§

impl JsCast for Int8Array

source§

impl JsCast for Int16Array

source§

impl JsCast for Int32Array

source§

impl JsCast for Iterator

source§

impl JsCast for IteratorNext

source§

impl JsCast for JsString

source§

impl JsCast for Map

source§

impl JsCast for Number

source§

impl JsCast for Object

source§

impl JsCast for Promise

source§

impl JsCast for Proxy

source§

impl JsCast for RangeError

source§

impl JsCast for ReferenceError

source§

impl JsCast for RegExp

source§

impl JsCast for Set

source§

impl JsCast for SharedArrayBuffer

source§

impl JsCast for Symbol

source§

impl JsCast for SyntaxError

source§

impl JsCast for TypeError

source§

impl JsCast for Uint8Array

source§

impl JsCast for Uint8ClampedArray

source§

impl JsCast for Uint16Array

source§

impl JsCast for Uint32Array

source§

impl JsCast for UriError

source§

impl JsCast for WeakMap

source§

impl JsCast for WeakSet

source§

impl JsCast for JsValue