wasm-bindgen/api/wasm_bindgen/trait.JsCast.html

93 lines
20 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `JsCast` trait in crate `wasm_bindgen`."><meta name="keywords" content="rust, rustlang, rust-lang, JsCast"><title>wasm_bindgen::JsCast - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="../favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc trait"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a href='../wasm_bindgen/index.html'><div class='logo-container'><img src='../rust-logo.png' alt='logo'></div></a><p class='location'>Trait JsCast</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.instanceof">instanceof</a><a href="#tymethod.unchecked_from_js">unchecked_from_js</a><a href="#tymethod.unchecked_from_js_ref">unchecked_from_js_ref</a></div><a class="sidebar-title" href="#provided-methods">Provided Methods</a><div class="sidebar-links"><a href="#method.dyn_into">dyn_into</a><a href="#method.dyn_ref">dyn_ref</a><a href="#method.has_type">has_type</a><a href="#method.is_instance_of">is_instance_of</a><a href="#method.is_type_of">is_type_of</a><a href="#method.unchecked_into">unchecked_into</a><a href="#method.unchecked_ref">unchecked_ref</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='index.html'>wasm_bindgen</a></p><script>window.sidebarCurrent = {name: 'JsCast', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"></div><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../src/wasm_bindgen/cast.rs.html#15-156' title='goto source code'>[src]</a></span><span class='in-band'>Trait <a href='index.html'>wasm_bindgen</a>::<wbr><a class="trait" href=''>JsCast</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class='rust trait'>pub trait JsCast <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.AsRef.html" title="trait core::convert::AsRef">AsRef</a>&lt;<a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;<a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>&gt;,&nbsp;</span>{
fn <a href='#tymethod.instanceof' class='fnname'>instanceof</a>(val: &amp;<a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>;
<div class='item-spacer'></div> fn <a href='#tymethod.unchecked_from_js' class='fnname'>unchecked_from_js</a>(val: <a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; Self;
<div class='item-spacer'></div> fn <a href='#tymethod.unchecked_from_js_ref' class='fnname'>unchecked_from_js_ref</a>(val: &amp;<a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>Self;
fn <a href='#method.has_type' class='fnname'>has_type</a>&lt;T&gt;(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a><br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a></span>,
{ ... }
<div class='item-spacer'></div> fn <a href='#method.dyn_into' class='fnname'>dyn_into</a>&lt;T&gt;(self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, Self&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a></span>,
{ ... }
<div class='item-spacer'></div> fn <a href='#method.dyn_ref' class='fnname'>dyn_ref</a>&lt;T&gt;(&amp;self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a></span>,
{ ... }
<div class='item-spacer'></div> fn <a href='#method.unchecked_into' class='fnname'>unchecked_into</a>&lt;T&gt;(self) -&gt; T<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a></span>,
{ ... }
<div class='item-spacer'></div> fn <a href='#method.unchecked_ref' class='fnname'>unchecked_ref</a>&lt;T&gt;(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a></span>,
{ ... }
<div class='item-spacer'></div> fn <a href='#method.is_instance_of' class='fnname'>is_instance_of</a>&lt;T&gt;(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a><br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a></span>,
{ ... }
<div class='item-spacer'></div> fn <a href='#method.is_type_of' class='fnname'>is_type_of</a>(val: &amp;<a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> { ... }
}</pre></div><div class='docblock'><p>A trait for checked and unchecked casting between JS types.</p>
<p>Specified <a href="https://github.com/rustwasm/rfcs/pull/2">in an RFC</a> 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.</p>
<p>This trait is automatically implemented for any type imported in a
<code>#[wasm_bindgen]</code> <code>extern</code> block.</p>
</div>
<h2 id='required-methods' class='small-section-header'>Required methods<a href='#required-methods' class='anchor'></a></h2><div class='methods'><h3 id='tymethod.instanceof' class='method'><code id='instanceof.v'>fn <a href='#tymethod.instanceof' class='fnname'>instanceof</a>(val: &amp;<a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code></h3><div class='docblock'><p>Performs a dynamic <code>instanceof</code> check to see whether the <code>JsValue</code>
provided is an instance of this type.</p>
<p>This is intended to be an internal implementation detail, you likely
won't need to call this. It's generally called through the
<code>is_instance_of</code> method instead.</p>
</div><h3 id='tymethod.unchecked_from_js' class='method'><code id='unchecked_from_js.v'>fn <a href='#tymethod.unchecked_from_js' class='fnname'>unchecked_from_js</a>(val: <a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; Self</code></h3><div class='docblock'><p>Performs a zero-cost unchecked conversion from a <code>JsValue</code> into an
instance of <code>Self</code></p>
<p>This is intended to be an internal implementation detail, you likely
won't need to call this.</p>
</div><h3 id='tymethod.unchecked_from_js_ref' class='method'><code id='unchecked_from_js_ref.v'>fn <a href='#tymethod.unchecked_from_js_ref' class='fnname'>unchecked_from_js_ref</a>(val: &amp;<a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>Self</code></h3><div class='docblock'><p>Performs a zero-cost unchecked conversion from a <code>&amp;JsValue</code> into an
instance of <code>&amp;Self</code>.</p>
<p>Note the safety of this method, which basically means that <code>Self</code> must
be a newtype wrapper around <code>JsValue</code>.</p>
<p>This is intended to be an internal implementation detail, you likely
won't need to call this.</p>
</div></div><span class='loading-content'>Loading content...</span>
<h2 id='provided-methods' class='small-section-header'>Provided methods<a href='#provided-methods' class='anchor'></a></h2><div class='methods'><h3 id='method.has_type' class='method'><code id='has_type.v'>fn <a href='#method.has_type' class='fnname'>has_type</a>&lt;T&gt;(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a>,&nbsp;</span></code></h3><div class='docblock'><p>Test whether this JS value has a type <code>T</code>.</p>
<p>This method will dynamically check to see if this JS object can be
casted to the JS object of type <code>T</code>. Usually this uses the <code>instanceof</code>
operator. This also works with primitive types like
booleans/strings/numbers as well as cross-realm object like <code>Array</code>
which can originate from other iframes.</p>
<p>In general this is intended to be a more robust version of
<code>is_instance_of</code>, but if you want strictly the <code>instanceof</code> operator
it's recommended to use that instead.</p>
</div><h3 id='method.dyn_into' class='method'><code id='dyn_into.v'>fn <a href='#method.dyn_into' class='fnname'>dyn_into</a>&lt;T&gt;(self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, Self&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a>,&nbsp;</span></code></h3><div class='docblock'><p>Performs a dynamic cast (checked at runtime) of this value into the
target type <code>T</code>.</p>
<p>This method will return <code>Err(self)</code> if <code>self.has_type::&lt;T&gt;()</code>
returns <code>false</code>, and otherwise it will return <code>Ok(T)</code> manufactured with
an unchecked cast (verified correct via the <code>has_type</code> operation).</p>
</div><h3 id='method.dyn_ref' class='method'><code id='dyn_ref.v'>fn <a href='#method.dyn_ref' class='fnname'>dyn_ref</a>&lt;T&gt;(&amp;self) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a>,&nbsp;</span></code></h3><div class='docblock'><p>Performs a dynamic cast (checked at runtime) of this value into the
target type <code>T</code>.</p>
<p>This method will return <code>None</code> if <code>self.has_type::&lt;T&gt;()</code>
returns <code>false</code>, and otherwise it will return <code>Some(&amp;T)</code> manufactured
with an unchecked cast (verified correct via the <code>has_type</code> operation).</p>
</div><h3 id='method.unchecked_into' class='method'><code id='unchecked_into.v'>fn <a href='#method.unchecked_into' class='fnname'>unchecked_into</a>&lt;T&gt;(self) -&gt; T <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a>,&nbsp;</span></code></h3><div class='docblock'><p>Performs a zero-cost unchecked cast into the specified type.</p>
<p>This method will convert the <code>self</code> value to the type <code>T</code>, where both
<code>self</code> and <code>T</code> are simple wrappers around <code>JsValue</code>. This method <strong>does
not check whether <code>self</code> is an instance of <code>T</code></strong>. If used incorrectly
then this method may cause runtime exceptions in both Rust and JS, this
should be used with caution.</p>
</div><h3 id='method.unchecked_ref' class='method'><code id='unchecked_ref.v'>fn <a href='#method.unchecked_ref' class='fnname'>unchecked_ref</a>&lt;T&gt;(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a>,&nbsp;</span></code></h3><div class='docblock'><p>Performs a zero-cost unchecked cast into a reference to the specified
type.</p>
<p>This method will convert the <code>self</code> value to the type <code>T</code>, where both
<code>self</code> and <code>T</code> are simple wrappers around <code>JsValue</code>. This method <strong>does
not check whether <code>self</code> is an instance of <code>T</code></strong>. If used incorrectly
then this method may cause runtime exceptions in both Rust and JS, this
should be used with caution.</p>
<p>This method, unlike <code>unchecked_into</code>, does not consume ownership of
<code>self</code> and instead works over a shared reference.</p>
</div><h3 id='method.is_instance_of' class='method'><code id='is_instance_of.v'>fn <a href='#method.is_instance_of' class='fnname'>is_instance_of</a>&lt;T&gt;(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a> <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../wasm_bindgen/trait.JsCast.html" title="trait wasm_bindgen::JsCast">JsCast</a>,&nbsp;</span></code></h3><div class='docblock'><p>Test whether this JS value is an instance of the type <code>T</code>.</p>
<p>This method performs a dynamic check (at runtime) using the JS
<code>instanceof</code> operator. This method returns <code>self instanceof T</code>.</p>
<p>Note that <code>instanceof</code> does not always work with primitive values or
across different realms (e.g. iframes). If you're not sure whether you
specifically need only <code>instanceof</code> it's recommended to use <code>has_type</code>
instead.</p>
</div><h3 id='method.is_type_of' class='method'><code id='is_type_of.v'>fn <a href='#method.is_type_of' class='fnname'>is_type_of</a>(val: &amp;<a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code></h3><div class='docblock'><p>Performs a dynamic check to see whether the <code>JsValue</code> provided
is a value of this type.</p>
<p>Unlike <code>instanceof</code>, this can be specialised to use a custom check by
adding a <code>#[wasm_bindgen(is_type_of = callback)]</code> attribute to the
type import declaration.</p>
<p>Other than that, this is intended to be an internal implementation
detail of <code>has_type</code> and you likely won't need to call this.</p>
</div></div><span class='loading-content'>Loading content...</span>
<h2 id='implementors' class='small-section-header'>Implementors<a href='#implementors' class='anchor'></a></h2><div class='item-list' id='implementors-list'><h3 id='impl-JsCast' class='impl'><code class='in-band'>impl JsCast for <a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a></code><a href='#impl-JsCast' class='anchor'></a><a class='srclink' href='../src/wasm_bindgen/lib.rs.html#444-458' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.instanceof' class="method hidden"><code id='instanceof.v-1'>fn <a href='#method.instanceof' class='fnname'>instanceof</a>(_val: &amp;<a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code><a class='srclink' href='../src/wasm_bindgen/lib.rs.html#447-449' title='goto source code'>[src]</a></h4><h4 id='method.unchecked_from_js' class="method hidden"><code id='unchecked_from_js.v-1'>fn <a href='#method.unchecked_from_js' class='fnname'>unchecked_from_js</a>(val: <a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; Self</code><a class='srclink' href='../src/wasm_bindgen/lib.rs.html#451-453' title='goto source code'>[src]</a></h4><h4 id='method.unchecked_from_js_ref' class="method hidden"><code id='unchecked_from_js_ref.v-1'>fn <a href='#method.unchecked_from_js_ref' class='fnname'>unchecked_from_js_ref</a>(val: &amp;<a class="struct" href="../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>Self</code><a class='srclink' href='../src/wasm_bindgen/lib.rs.html#455-457' title='goto source code'>[src]</a></h4></div></div><span class='loading-content'>Loading content...</span><script type="text/javascript" src="../implementors/wasm_bindgen/trait.JsCast.js" async></script></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../";window.currentCrate = "wasm_bindgen";</script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>