Trait silkenweb_html::macros::JsCast[][src]

pub trait JsCast: AsRef<JsValue> + Into<JsValue> {
    pub fn instanceof(val: &JsValue) -> bool;
pub fn unchecked_from_js(val: JsValue) -> Self;
pub fn unchecked_from_js_ref(val: &JsValue) -> &Self; pub fn has_type<T>(&self) -> bool
    where
        T: JsCast
, { ... }
pub fn dyn_into<T>(self) -> Result<T, Self>
    where
        T: JsCast
, { ... }
pub fn dyn_ref<T>(&self) -> Option<&T>
    where
        T: JsCast
, { ... }
pub fn unchecked_into<T>(self) -> T
    where
        T: JsCast
, { ... }
pub fn unchecked_ref<T>(&self) -> &T
    where
        T: JsCast
, { ... }
pub fn is_instance_of<T>(&self) -> bool
    where
        T: JsCast
, { ... }
pub fn is_type_of(val: &JsValue) -> bool { ... } }

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

pub fn instanceof(val: &JsValue) -> bool[src]

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.

pub fn unchecked_from_js(val: JsValue) -> Self[src]

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.

pub fn unchecked_from_js_ref(val: &JsValue) -> &Self[src]

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.

Loading content...

Provided methods

pub fn has_type<T>(&self) -> bool where
    T: JsCast
[src]

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.

pub fn dyn_into<T>(self) -> Result<T, Self> where
    T: JsCast
[src]

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

pub fn dyn_ref<T>(&self) -> Option<&T> where
    T: JsCast
[src]

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

pub fn unchecked_into<T>(self) -> T where
    T: JsCast
[src]

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.

pub fn unchecked_ref<T>(&self) -> &T where
    T: JsCast
[src]

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.

pub fn is_instance_of<T>(&self) -> bool where
    T: JsCast
[src]

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.

pub fn is_type_of(val: &JsValue) -> bool[src]

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.

Loading content...

Implementations on Foreign Types

impl JsCast for HtmlHrElement[src]

impl JsCast for UiEvent[src]

impl JsCast for HtmlProgressElement[src]

impl JsCast for Node[src]

impl JsCast for HtmlLiElement[src]

impl JsCast for FocusEvent[src]

impl JsCast for HtmlParamElement[src]

impl JsCast for HtmlDivElement[src]

impl JsCast for Text[src]

impl JsCast for HtmlTableElement[src]

impl JsCast for HtmlLabelElement[src]

impl JsCast for HtmlOutputElement[src]

impl JsCast for HtmlPictureElement[src]

impl JsCast for HtmlOListElement[src]

impl JsCast for HtmlPreElement[src]

impl JsCast for HtmlDListElement[src]

impl JsCast for HtmlImageElement[src]

impl JsCast for HtmlMenuElement[src]

impl JsCast for HtmlDataListElement[src]

impl JsCast for HtmlTableSectionElement[src]

impl JsCast for HtmlAudioElement[src]

impl JsCast for HtmlMeterElement[src]

impl JsCast for HtmlDialogElement[src]

impl JsCast for HtmlTableRowElement[src]

impl JsCast for Event[src]

impl JsCast for HtmlIFrameElement[src]

impl JsCast for InputEvent[src]

impl JsCast for KeyboardEvent[src]

impl JsCast for HtmlHeadingElement[src]

impl JsCast for HtmlParagraphElement[src]

impl JsCast for HtmlFormElement[src]

impl JsCast for HtmlDataElement[src]

impl JsCast for HtmlMediaElement[src]

impl JsCast for HtmlOptionElement[src]

impl JsCast for HtmlCanvasElement[src]

impl JsCast for HtmlHeadElement[src]

impl JsCast for HtmlTimeElement[src]

impl JsCast for HtmlSourceElement[src]

impl JsCast for HtmlElement[src]

impl JsCast for HtmlInputElement[src]

impl JsCast for HtmlTableCellElement[src]

impl JsCast for HtmlLegendElement[src]

impl JsCast for HtmlLinkElement[src]

impl JsCast for HtmlScriptElement[src]

impl JsCast for HtmlModElement[src]

impl JsCast for Document[src]

impl JsCast for HtmlQuoteElement[src]

impl JsCast for EventTarget[src]

impl JsCast for HtmlOptGroupElement[src]

impl JsCast for HtmlTableCaptionElement[src]

impl JsCast for HtmlSelectElement[src]

impl JsCast for CharacterData[src]

impl JsCast for HtmlTrackElement[src]

impl JsCast for HtmlTextAreaElement[src]

impl JsCast for HtmlButtonElement[src]

impl JsCast for HtmlBaseElement[src]

impl JsCast for Element[src]

impl JsCast for HtmlTitleElement[src]

impl JsCast for HtmlAreaElement[src]

impl JsCast for HtmlDetailsElement[src]

impl JsCast for HtmlMetaElement[src]

impl JsCast for HtmlEmbedElement[src]

impl JsCast for HtmlVideoElement[src]

impl JsCast for HtmlSpanElement[src]

impl JsCast for HtmlFieldSetElement[src]

impl JsCast for HtmlAnchorElement[src]

impl JsCast for HtmlTableColElement[src]

impl JsCast for HtmlObjectElement[src]

impl JsCast for HtmlBrElement[src]

impl JsCast for HtmlUListElement[src]

impl JsCast for MouseEvent[src]

impl JsCast for Window[src]

impl JsCast for HtmlMapElement[src]

impl JsCast for HtmlStyleElement[src]

impl JsCast for JsValue[src]

impl JsCast for EvalError[src]

impl JsCast for CompileError[src]

impl JsCast for PluralRules[src]

impl JsCast for RuntimeError[src]

impl JsCast for Uint8Array[src]

impl JsCast for Iterator[src]

impl JsCast for Table[src]

impl JsCast for Int32Array[src]

impl JsCast for UriError[src]

impl JsCast for Instance[src]

impl JsCast for DateTimeFormat[src]

impl JsCast for SyntaxError[src]

impl JsCast for ReferenceError[src]

impl JsCast for Collator[src]

impl JsCast for Module[src]

impl JsCast for Set[src]

impl JsCast for Symbol[src]

impl JsCast for Promise[src]

impl JsCast for Float32Array[src]

impl JsCast for WeakSet[src]

impl JsCast for TypeError[src]

impl JsCast for Date[src]

impl JsCast for Generator[src]

impl JsCast for JsString[src]

impl JsCast for ArrayBuffer[src]

impl JsCast for DataView[src]

impl JsCast for NumberFormat[src]

impl JsCast for RegExp[src]

impl JsCast for IteratorNext[src]

impl JsCast for Function[src]

impl JsCast for Boolean[src]

impl JsCast for Object[src]

impl JsCast for AsyncIterator[src]

impl JsCast for Number[src]

impl JsCast for Int8Array[src]

impl JsCast for RangeError[src]

impl JsCast for Uint32Array[src]

impl JsCast for SharedArrayBuffer[src]

impl JsCast for Int16Array[src]

impl JsCast for LinkError[src]

impl JsCast for WeakMap[src]

impl JsCast for Float64Array[src]

impl JsCast for Uint16Array[src]

impl JsCast for Map[src]

impl JsCast for Uint8ClampedArray[src]

impl JsCast for Error[src]

impl JsCast for Memory[src]

impl JsCast for Array[src]

impl JsCast for Proxy[src]

Loading content...

Implementors

Loading content...