[][src]Trait augdom::JsCast

pub trait JsCast: AsRef<JsValue> + Into<JsValue> {
    fn instanceof(val: &JsValue) -> bool;
fn unchecked_from_js(val: JsValue) -> Self;
fn unchecked_from_js_ref(val: &JsValue) -> &Self; 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 { ... } }

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

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.

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.

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.

Loading content...

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

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

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

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.

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.

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.

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.

Loading content...

Implementations on Foreign Types

impl JsCast for JsValue[src]

impl JsCast for Node[src]

impl JsCast for BlobEvent[src]

impl JsCast for AudioProcessingEvent[src]

impl JsCast for PointerEvent[src]

impl JsCast for IdbVersionChangeEvent[src]

impl JsCast for UserProximityEvent[src]

impl JsCast for FetchEvent[src]

impl JsCast for EventTarget[src]

impl JsCast for HtmlCanvasElement[src]

impl JsCast for Text[src]

impl JsCast for TransitionEvent[src]

impl JsCast for NamedNodeMap[src]

impl JsCast for AnimationEvent[src]

impl JsCast for TouchEvent[src]

impl JsCast for UiEvent[src]

impl JsCast for CompositionEvent[src]

impl JsCast for Document[src]

impl JsCast for CharacterData[src]

impl JsCast for HtmlElement[src]

impl JsCast for Element[src]

impl JsCast for HtmlInputElement[src]

impl JsCast for CloseEvent[src]

impl JsCast for Window[src]

impl JsCast for GamepadEvent[src]

impl JsCast for Event[src]

impl JsCast for Attr[src]

impl JsCast for DeviceMotionEvent[src]

impl JsCast for HashChangeEvent[src]

impl JsCast for KeyboardEvent[src]

impl JsCast for StorageEvent[src]

impl JsCast for TimeEvent[src]

impl JsCast for Worker[src]

impl JsCast for OfflineAudioCompletionEvent[src]

impl JsCast for BeforeUnloadEvent[src]

impl JsCast for ErrorEvent[src]

impl JsCast for SpeechSynthesisEvent[src]

impl JsCast for PushEvent[src]

impl JsCast for MessageEvent[src]

impl JsCast for DeviceOrientationEvent[src]

impl JsCast for ProgressEvent[src]

impl JsCast for PopStateEvent[src]

impl JsCast for SpeechSynthesisErrorEvent[src]

impl JsCast for DragEvent[src]

impl JsCast for NotificationEvent[src]

impl JsCast for MouseEvent[src]

impl JsCast for SpeechRecognitionEvent[src]

impl JsCast for DomRect[src]

impl JsCast for FocusEvent[src]

impl JsCast for PageTransitionEvent[src]

impl JsCast for NodeList[src]

impl JsCast for WheelEvent[src]

impl JsCast for ClipboardEvent[src]

impl JsCast for CanvasRenderingContext2d[src]

impl JsCast for Table[src]

impl JsCast for DataView[src]

impl JsCast for Int8Array[src]

impl JsCast for SyntaxError[src]

impl JsCast for RangeError[src]

impl JsCast for Number[src]

impl JsCast for PluralRules[src]

impl JsCast for Uint8ClampedArray[src]

impl JsCast for Object[src]

impl JsCast for WeakMap[src]

impl JsCast for Map[src]

impl JsCast for ArrayBuffer[src]

impl JsCast for ReferenceError[src]

impl JsCast for Iterator[src]

impl JsCast for WeakSet[src]

impl JsCast for Uint32Array[src]

impl JsCast for RegExp[src]

impl JsCast for Set[src]

impl JsCast for Instance[src]

impl JsCast for Collator[src]

impl JsCast for Boolean[src]

impl JsCast for TypeError[src]

impl JsCast for Error[src]

impl JsCast for UriError[src]

impl JsCast for Generator[src]

impl JsCast for AsyncIterator[src]

impl JsCast for Uint8Array[src]

impl JsCast for JsString[src]

impl JsCast for EvalError[src]

impl JsCast for SharedArrayBuffer[src]

impl JsCast for Float64Array[src]

impl JsCast for Function[src]

impl JsCast for IteratorNext[src]

impl JsCast for LinkError[src]

impl JsCast for Memory[src]

impl JsCast for Float32Array[src]

impl JsCast for Date[src]

impl JsCast for Int32Array[src]

impl JsCast for DateTimeFormat[src]

impl JsCast for Proxy[src]

impl JsCast for Uint16Array[src]

impl JsCast for Promise[src]

impl JsCast for Array[src]

impl JsCast for CompileError[src]

impl JsCast for Symbol[src]

impl JsCast for RuntimeError[src]

impl JsCast for Module[src]

impl JsCast for Int16Array[src]

impl JsCast for NumberFormat[src]

Loading content...

Implementors

impl JsCast for Abort[src]

impl JsCast for AbortProgress[src]

impl JsCast for AbortTransaction[src]

impl JsCast for AfterPrint[src]

impl JsCast for AnimationCancel[src]

impl JsCast for AnimationEnd[src]

impl JsCast for AnimationIteration[src]

impl JsCast for AnimationRepeat[src]

impl JsCast for AnimationStart[src]

impl JsCast for AppInstalled[src]

impl JsCast for AudioComplete[src]

impl JsCast for AudioEnd[src]

impl JsCast for AudioEnded[src]

impl JsCast for AudioProcess[src]

impl JsCast for AudioStart[src]

impl JsCast for BeforePrint[src]

impl JsCast for BeforeUnload[src]

impl JsCast for Blur[src]

impl JsCast for CanPlay[src]

impl JsCast for CanPlayThrough[src]

impl JsCast for Change[src]

impl JsCast for ChargingChange[src]

impl JsCast for ChargingTime[src]

impl JsCast for ChildMessage[src]

impl JsCast for Click[src]

impl JsCast for CloseWebsocket[src]

impl JsCast for CompositionEnd[src]

impl JsCast for CompositionStart[src]

impl JsCast for CompositionUpdate[src]

impl JsCast for ConnectionBlocked[src]

impl JsCast for ContextMenu[src]

impl JsCast for ContextMenuShow[src]

impl JsCast for Cp[src]

impl JsCast for Cut[src]

impl JsCast for DataLoaded[src]

impl JsCast for DeviceChange[src]

impl JsCast for DeviceMotion[src]

impl JsCast for DeviceOrientation[src]

impl JsCast for DischargingTime[src]

impl JsCast for DomContentLoaded[src]

impl JsCast for DoubleClick[src]

impl JsCast for Drag[src]

impl JsCast for DragEnd[src]

impl JsCast for DragEnter[src]

impl JsCast for DragLeave[src]

impl JsCast for DragOver[src]

impl JsCast for DragStart[src]

impl JsCast for Dropped[src]

impl JsCast for DurationChange[src]

impl JsCast for Emptied[src]

impl JsCast for EventSourceError[src]

impl JsCast for EventSourceMessage[src]

impl JsCast for EventSourceOpen[src]

impl JsCast for Focus[src]

impl JsCast for FocusIn[src]

impl JsCast for FocusOut[src]

impl JsCast for FormReset[src]

impl JsCast for FullscreenChange[src]

impl JsCast for FullscreenError[src]

impl JsCast for GamepadConnected[src]

impl JsCast for GamepadDisconnected[src]

impl JsCast for GotPointerCapture[src]

impl JsCast for HashChange[src]

impl JsCast for Input[src]

impl JsCast for Invalid[src]

impl JsCast for KeyDown[src]

impl JsCast for KeyUp[src]

impl JsCast for LanguageChange[src]

impl JsCast for LevelChange[src]

impl JsCast for LoadEnd[src]

impl JsCast for LoadStart[src]

impl JsCast for LostPointerCapture[src]

impl JsCast for MessageError[src]

impl JsCast for MetadataLoaded[src]

impl JsCast for MouseDown[src]

impl JsCast for MouseEnter[src]

impl JsCast for MouseLeave[src]

impl JsCast for MouseMove[src]

impl JsCast for MouseOut[src]

impl JsCast for MouseOver[src]

impl JsCast for MouseUp[src]

impl JsCast for NotificationClick[src]

impl JsCast for Offline[src]

impl JsCast for Online[src]

impl JsCast for OrientationChange[src]

impl JsCast for PageHide[src]

impl JsCast for PageShow[src]

impl JsCast for Paste[src]

impl JsCast for Pause[src]

impl JsCast for Play[src]

impl JsCast for PlaybackEnded[src]

impl JsCast for PlaybackRateChange[src]

impl JsCast for Playing[src]

impl JsCast for PointerCancel[src]

impl JsCast for PointerDown[src]

impl JsCast for PointerEnter[src]

impl JsCast for PointerLeave[src]

impl JsCast for PointerLockChange[src]

impl JsCast for PointerLockError[src]

impl JsCast for PointerMove[src]

impl JsCast for PointerOut[src]

impl JsCast for PointerOver[src]

impl JsCast for PointerUp[src]

impl JsCast for PopState[src]

impl JsCast for Progress[src]

impl JsCast for ProgressError[src]

impl JsCast for ProgressLoad[src]

impl JsCast for Push[src]

impl JsCast for PushSubscriptionChange[src]

impl JsCast for ReadyStateChange[src]

impl JsCast for RequestError[src]

impl JsCast for ResourceError[src]

impl JsCast for ResourceLoad[src]

impl JsCast for Scroll[src]

impl JsCast for Seeked[src]

impl JsCast for Seeking[src]

impl JsCast for Select[src]

impl JsCast for SelectionChange[src]

impl JsCast for SelectionStart[src]

impl JsCast for ServiceWorkerMessage[src]

impl JsCast for SlotChange[src]

impl JsCast for SoundEnd[src]

impl JsCast for SoundStart[src]

impl JsCast for SpeechBoundary[src]

impl JsCast for SpeechEnd[src]

impl JsCast for SpeechError[src]

impl JsCast for SpeechMark[src]

impl JsCast for SpeechPause[src]

impl JsCast for SpeechRecognitionEnd[src]

impl JsCast for SpeechRecognitionError[src]

impl JsCast for SpeechRecognitionNoMatch[src]

impl JsCast for SpeechRecognitionResult[src]

impl JsCast for SpeechRecognitionStart[src]

impl JsCast for SpeechRecognized[src]

impl JsCast for SpeechResume[src]

impl JsCast for SpeechStart[src]

impl JsCast for SpeechSynthesisEnd[src]

impl JsCast for Stalled[src]

impl JsCast for Storage[src]

impl JsCast for Submit[src]

impl JsCast for Success[src]

impl JsCast for Suspend[src]

impl JsCast for SvgAbort[src]

impl JsCast for SvgAnimationBegin[src]

impl JsCast for SvgAnimationEnd[src]

impl JsCast for SvgError[src]

impl JsCast for SvgLoad[src]

impl JsCast for SvgResize[src]

impl JsCast for SvgScroll[src]

impl JsCast for SvgUnload[src]

impl JsCast for SvgZoom[src]

impl JsCast for TimeUpdate[src]

impl JsCast for Timeout[src]

impl JsCast for TimingBufferFull[src]

impl JsCast for TouchCancel[src]

impl JsCast for TouchEnd[src]

impl JsCast for TouchMove[src]

impl JsCast for TouchStart[src]

impl JsCast for TransactionComplete[src]

impl JsCast for TransactionVersionChange[src]

impl JsCast for TransitionEnd[src]

impl JsCast for Unload[src]

impl JsCast for UpgradeNeeded[src]

impl JsCast for UserProximity[src]

impl JsCast for ViewResize[src]

impl JsCast for VisibilityChange[src]

impl JsCast for VoicesChanged[src]

impl JsCast for VolumeChange[src]

impl JsCast for Waiting[src]

impl JsCast for WebsocketError[src]

impl JsCast for WebsocketMessage[src]

impl JsCast for WebsocketOpen[src]

impl JsCast for Wheel[src]

impl JsCast for WorkerMessage[src]

Loading content...