wasm-bindgen/api/wasm_bindgen/closure/struct.Closure.html

299 lines
51 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 `Closure` struct in crate `wasm_bindgen`."><meta name="keywords" content="rust, rustlang, rust-lang, Closure"><title>wasm_bindgen::closure::Closure - 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 struct"><!--[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'>Struct Closure</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#implementations">Methods</a><div class="sidebar-links"><a href="#method.forget">forget</a><a href="#method.new">new</a><a href="#method.once">once</a><a href="#method.once_into_js">once_into_js</a><a href="#method.wrap">wrap</a></div><a class="sidebar-title" href="#trait-implementations">Trait Implementations</a><div class="sidebar-links"><a href="#impl-AsRef%3CJsValue%3E">AsRef&lt;JsValue&gt;</a><a href="#impl-Debug">Debug</a><a href="#impl-Drop">Drop</a><a href="#impl-IntoWasmAbi">IntoWasmAbi</a></div><a class="sidebar-title" href="#synthetic-implementations">Auto Trait Implementations</a><div class="sidebar-links"><a href="#impl-RefUnwindSafe">RefUnwindSafe</a><a href="#impl-Send">!Send</a><a href="#impl-Sync">!Sync</a><a href="#impl-Unpin">Unpin</a><a href="#impl-UnwindSafe">UnwindSafe</a></div><a class="sidebar-title" href="#blanket-implementations">Blanket Implementations</a><div class="sidebar-links"><a href="#impl-Any">Any</a><a href="#impl-Borrow%3CT%3E">Borrow&lt;T&gt;</a><a href="#impl-BorrowMut%3CT%3E">BorrowMut&lt;T&gt;</a><a href="#impl-From%3CT%3E">From&lt;T&gt;</a><a href="#impl-Into%3CU%3E">Into&lt;U&gt;</a><a href="#impl-TryFrom%3CU%3E">TryFrom&lt;U&gt;</a><a href="#impl-TryInto%3CU%3E">TryInto&lt;U&gt;</a></div></div><p class='location'><a href='../index.html'>wasm_bindgen</a>::<wbr><a href='index.html'>closure</a></p><script>window.sidebarCurrent = {name: 'Closure', ty: 'struct', 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/closure.rs.html#243-246' title='goto source code'>[src]</a></span><span class='in-band'>Struct <a href='../index.html'>wasm_bindgen</a>::<wbr><a href='index.html'>closure</a>::<wbr><a class="struct" href=''>Closure</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class='rust struct'>pub struct Closure&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; { /* fields omitted */ }</pre></div><div class='docblock'><p>A handle to both a closure in Rust as well as JS closure which will invoke
the Rust closure.</p>
<p>A <code>Closure</code> is the primary way that a <code>'static</code> lifetime closure is
transferred from Rust to JS. <code>Closure</code> currently requires that the closures
it's created with have the <code>'static</code> lifetime in Rust for soundness reasons.</p>
<p>This type is a &quot;handle&quot; in the sense that whenever it is dropped it will
invalidate the JS closure that it refers to. Any usage of the closure in JS
after the <code>Closure</code> has been dropped will raise an exception. It's then up
to you to arrange for <code>Closure</code> to be properly deallocate at an appropriate
location in your program.</p>
<p>The type parameter on <code>Closure</code> is the type of closure that this represents.
Currently this can only be the <code>Fn</code> and <code>FnMut</code> traits with up to 7
arguments (and an optional return value). The arguments/return value of the
trait must be numbers like <code>u32</code> for now, although this restriction may be
lifted in the future!</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Here are a number of examples of using <code>Closure</code>.</p>
<h2 id="using-the-setinterval-api" class="section-header"><a href="#using-the-setinterval-api">Using the <code>setInterval</code> API</a></h2>
<p>Sample usage of <code>Closure</code> to invoke the <code>setInterval</code> API.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">wasm_bindgen</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">extern</span> <span class="string">&quot;C&quot;</span> {
<span class="kw">fn</span> <span class="ident">setInterval</span>(<span class="ident">closure</span>: <span class="kw-2">&amp;</span><span class="ident">Closure</span><span class="op">&lt;</span><span class="ident">FnMut</span>()<span class="op">&gt;</span>, <span class="ident">time</span>: <span class="ident">u32</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">i32</span>;
<span class="kw">fn</span> <span class="ident">clearInterval</span>(<span class="ident">id</span>: <span class="ident">i32</span>);
<span class="attribute">#[<span class="ident">wasm_bindgen</span>(<span class="ident">js_namespace</span> <span class="op">=</span> <span class="ident">console</span>)]</span>
<span class="kw">fn</span> <span class="ident">log</span>(<span class="ident">s</span>: <span class="kw-2">&amp;</span><span class="ident">str</span>);
}
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">IntervalHandle</span> {
<span class="ident">interval_id</span>: <span class="ident">i32</span>,
<span class="ident">_closure</span>: <span class="ident">Closure</span><span class="op">&lt;</span><span class="ident">FnMut</span>()<span class="op">&gt;</span>,
}
<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">IntervalHandle</span> {
<span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) {
<span class="ident">clearInterval</span>(<span class="self">self</span>.<span class="ident">interval_id</span>);
}
}
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">run</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="ident">IntervalHandle</span> {
<span class="comment">// First up we use `Closure::wrap` to wrap up a Rust closure and create</span>
<span class="comment">// a JS closure.</span>
<span class="kw">let</span> <span class="ident">cb</span> <span class="op">=</span> <span class="ident">Closure</span>::<span class="ident">wrap</span>(<span class="ident">Box</span>::<span class="ident">new</span>(<span class="op">|</span><span class="op">|</span> {
<span class="ident">log</span>(<span class="string">&quot;interval elapsed!&quot;</span>);
}) <span class="kw">as</span> <span class="ident">Box</span><span class="op">&lt;</span><span class="ident">FnMut</span>()<span class="op">&gt;</span>);
<span class="comment">// Next we pass this via reference to the `setInterval` function, and</span>
<span class="comment">// `setInterval` gets a handle to the corresponding JS closure.</span>
<span class="kw">let</span> <span class="ident">interval_id</span> <span class="op">=</span> <span class="ident">setInterval</span>(<span class="kw-2">&amp;</span><span class="ident">cb</span>, <span class="number">1_000</span>);
<span class="comment">// If we were to drop `cb` here it would cause an exception to be raised</span>
<span class="comment">// whenever the interval elapses. Instead we *return* our handle back to JS</span>
<span class="comment">// so JS can decide when to cancel the interval and deallocate the closure.</span>
<span class="ident">IntervalHandle</span> {
<span class="ident">interval_id</span>,
<span class="ident">_closure</span>: <span class="ident">cb</span>,
}
}</pre></div>
<h2 id="casting-a-closure-to-a-js_sysfunction" class="section-header"><a href="#casting-a-closure-to-a-js_sysfunction">Casting a <code>Closure</code> to a <code>js_sys::Function</code></a></h2>
<p>This is the same <code>setInterval</code> example as above, except it is using
<code>web_sys</code> (which uses <code>js_sys::Function</code> for callbacks) instead of manually
writing bindings to <code>setInterval</code> and other Web APIs.</p>
<div class='information'><div class='tooltip ignore'><span class='tooltiptext'>This example is not tested</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered ignore">
<span class="kw">use</span> <span class="ident">wasm_bindgen</span>::<span class="ident">JsCast</span>;
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">IntervalHandle</span> {
<span class="ident">interval_id</span>: <span class="ident">i32</span>,
<span class="ident">_closure</span>: <span class="ident">Closure</span><span class="op">&lt;</span><span class="ident">FnMut</span>()<span class="op">&gt;</span>,
}
<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">IntervalHandle</span> {
<span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) {
<span class="kw">let</span> <span class="ident">window</span> <span class="op">=</span> <span class="ident">web_sys</span>::<span class="ident">window</span>().<span class="ident">unwrap</span>();
<span class="ident">window</span>.<span class="ident">clear_interval_with_handle</span>(<span class="self">self</span>.<span class="ident">interval_id</span>);
}
}
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">run</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">IntervalHandle</span>, <span class="ident">JsValue</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="ident">cb</span> <span class="op">=</span> <span class="ident">Closure</span>::<span class="ident">wrap</span>(<span class="ident">Box</span>::<span class="ident">new</span>(<span class="op">|</span><span class="op">|</span> {
<span class="ident">web_sys</span>::<span class="ident">console</span>::<span class="ident">log_1</span>(<span class="kw-2">&amp;</span><span class="string">&quot;inverval elapsed!&quot;</span>.<span class="ident">into</span>());
}) <span class="kw">as</span> <span class="ident">Box</span><span class="op">&lt;</span><span class="ident">FnMut</span>()<span class="op">&gt;</span>);
<span class="kw">let</span> <span class="ident">window</span> <span class="op">=</span> <span class="ident">web_sys</span>::<span class="ident">window</span>().<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="ident">interval_id</span> <span class="op">=</span> <span class="ident">window</span>.<span class="ident">set_interval_with_callback_and_timeout_and_arguments_0</span>(
<span class="comment">// Note this method call, which uses `as_ref()` to get a `JsValue`</span>
<span class="comment">// from our `Closure` which is then converted to a `&amp;Function`</span>
<span class="comment">// using the `JsCast::unchecked_ref` function.</span>
<span class="ident">cb</span>.<span class="ident">as_ref</span>().<span class="ident">unchecked_ref</span>(),
<span class="number">1_000</span>,
)<span class="question-mark">?</span>;
<span class="comment">// Same as above.</span>
<span class="prelude-val">Ok</span>(<span class="ident">IntervalHandle</span> {
<span class="ident">interval_id</span>,
<span class="ident">_closure</span>: <span class="ident">cb</span>,
})
}</pre></div>
<h2 id="using-fnonce-and-closureonce-with-requestanimationframe" class="section-header"><a href="#using-fnonce-and-closureonce-with-requestanimationframe">Using <code>FnOnce</code> and <code>Closure::once</code> with <code>requestAnimationFrame</code></a></h2>
<p>Because <code>requestAnimationFrame</code> only calls its callback once, we can use
<code>FnOnce</code> and <code>Closure::once</code> with it.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">wasm_bindgen</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">extern</span> <span class="string">&quot;C&quot;</span> {
<span class="kw">fn</span> <span class="ident">requestAnimationFrame</span>(<span class="ident">closure</span>: <span class="kw-2">&amp;</span><span class="ident">Closure</span><span class="op">&lt;</span><span class="ident">FnMut</span>()<span class="op">&gt;</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">u32</span>;
<span class="kw">fn</span> <span class="ident">cancelAnimationFrame</span>(<span class="ident">id</span>: <span class="ident">u32</span>);
<span class="attribute">#[<span class="ident">wasm_bindgen</span>(<span class="ident">js_namespace</span> <span class="op">=</span> <span class="ident">console</span>)]</span>
<span class="kw">fn</span> <span class="ident">log</span>(<span class="ident">s</span>: <span class="kw-2">&amp;</span><span class="ident">str</span>);
}
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">AnimationFrameHandle</span> {
<span class="ident">animation_id</span>: <span class="ident">u32</span>,
<span class="ident">_closure</span>: <span class="ident">Closure</span><span class="op">&lt;</span><span class="ident">FnMut</span>()<span class="op">&gt;</span>,
}
<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">AnimationFrameHandle</span> {
<span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) {
<span class="ident">cancelAnimationFrame</span>(<span class="self">self</span>.<span class="ident">animation_id</span>);
}
}
<span class="comment">// A type that will log a message when it is dropped.</span>
<span class="kw">struct</span> <span class="ident">LogOnDrop</span>(<span class="kw-2">&amp;</span><span class="lifetime">&#39;static</span> <span class="ident">str</span>);
<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">LogOnDrop</span> {
<span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) {
<span class="ident">log</span>(<span class="self">self</span>.<span class="number">0</span>);
}
}
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">run</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="ident">AnimationFrameHandle</span> {
<span class="comment">// We are using `Closure::once` which takes a `FnOnce`, so the function</span>
<span class="comment">// can drop and/or move things that it closes over.</span>
<span class="kw">let</span> <span class="ident">fired</span> <span class="op">=</span> <span class="ident">LogOnDrop</span>(<span class="string">&quot;animation frame fired or canceled&quot;</span>);
<span class="kw">let</span> <span class="ident">cb</span> <span class="op">=</span> <span class="ident">Closure</span>::<span class="ident">once</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> <span class="ident">drop</span>(<span class="ident">fired</span>));
<span class="comment">// Schedule the animation frame!</span>
<span class="kw">let</span> <span class="ident">animation_id</span> <span class="op">=</span> <span class="ident">requestAnimationFrame</span>(<span class="kw-2">&amp;</span><span class="ident">cb</span>);
<span class="comment">// Again, return a handle to JS, so that the closure is not dropped</span>
<span class="comment">// immediately and JS can decide whether to cancel the animation frame.</span>
<span class="ident">AnimationFrameHandle</span> {
<span class="ident">animation_id</span>,
<span class="ident">_closure</span>: <span class="ident">cb</span>,
}
}</pre></div>
<h2 id="converting-fnonces-directly-into-javascript-functions-with-closureonce_into_js" class="section-header"><a href="#converting-fnonces-directly-into-javascript-functions-with-closureonce_into_js">Converting <code>FnOnce</code>s directly into JavaScript Functions with <code>Closure::once_into_js</code></a></h2>
<p>If we don't want to allow a <code>FnOnce</code> to be eagerly dropped (maybe because we
just want it to drop after it is called and don't care about cancellation)
then we can use the <code>Closure::once_into_js</code> function.</p>
<p>This is the same <code>requestAnimationFrame</code> example as above, but without
supporting early cancellation.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">wasm_bindgen</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">extern</span> <span class="string">&quot;C&quot;</span> {
<span class="comment">// We modify the binding to take an untyped `JsValue` since that is what</span>
<span class="comment">// is returned by `Closure::once_into_js`.</span>
<span class="comment">//</span>
<span class="comment">// If we were using the `web_sys` binding for `requestAnimationFrame`,</span>
<span class="comment">// then the call sites would cast the `JsValue` into a `&amp;js_sys::Function`</span>
<span class="comment">// using `f.unchecked_ref::&lt;js_sys::Function&gt;()`. See the `web_sys`</span>
<span class="comment">// example above for details.</span>
<span class="kw">fn</span> <span class="ident">requestAnimationFrame</span>(<span class="ident">callback</span>: <span class="ident">JsValue</span>);
<span class="attribute">#[<span class="ident">wasm_bindgen</span>(<span class="ident">js_namespace</span> <span class="op">=</span> <span class="ident">console</span>)]</span>
<span class="kw">fn</span> <span class="ident">log</span>(<span class="ident">s</span>: <span class="kw-2">&amp;</span><span class="ident">str</span>);
}
<span class="comment">// A type that will log a message when it is dropped.</span>
<span class="kw">struct</span> <span class="ident">LogOnDrop</span>(<span class="kw-2">&amp;</span><span class="lifetime">&#39;static</span> <span class="ident">str</span>);
<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">LogOnDrop</span> {
<span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) {
<span class="ident">log</span>(<span class="self">self</span>.<span class="number">0</span>);
}
}
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">run</span>() {
<span class="comment">// We are using `Closure::once_into_js` which takes a `FnOnce` and</span>
<span class="comment">// converts it into a JavaScript function, which is returned as a</span>
<span class="comment">// `JsValue`.</span>
<span class="kw">let</span> <span class="ident">fired</span> <span class="op">=</span> <span class="ident">LogOnDrop</span>(<span class="string">&quot;animation frame fired&quot;</span>);
<span class="kw">let</span> <span class="ident">cb</span> <span class="op">=</span> <span class="ident">Closure</span>::<span class="ident">once_into_js</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> <span class="ident">drop</span>(<span class="ident">fired</span>));
<span class="comment">// Schedule the animation frame!</span>
<span class="ident">requestAnimationFrame</span>(<span class="ident">cb</span>);
<span class="comment">// No need to worry about whether or not we drop a `Closure`</span>
<span class="comment">// here or return some sort of handle to JS!</span>
}</pre></div>
</div><h2 id='implementations' class='small-section-header'>Implementations<a href='#implementations' class='anchor'></a></h2><h3 id='impl' class='impl'><code class='in-band'>impl&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: WasmClosure,&nbsp;</span></code><a href='#impl' class='anchor'></a><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#253-376' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.new' class="method"><code id='new.v'>pub fn <a href='#method.new' class='fnname'>new</a>&lt;F&gt;(t: F) -&gt; <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unsize.html" title="trait core::marker::Unsize">Unsize</a>&lt;T&gt; + 'static,&nbsp;</span></code><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#264-269' title='goto source code'>[src]</a></h4><div class='docblock'><p>A more ergonomic version of <code>Closure::wrap</code> that does the boxing and
cast to trait object for you.</p>
<p><em>This method requires the <code>nightly</code> feature of the <code>wasm-bindgen</code> crate
to be enabled, meaning this is a nightly-only API. Users on stable
should use <code>Closure::wrap</code>.</em></p>
</div><h4 id='method.wrap' class="method"><code id='wrap.v'>pub fn <a href='#method.wrap' class='fnname'>wrap</a>(data: <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;T&gt;) -&gt; <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt;</code><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#288-357' title='goto source code'>[src]</a></h4><div class='docblock'><p>Creates a new instance of <code>Closure</code> from the provided boxed Rust
function.</p>
<p>Note that the closure provided here, <code>Box&lt;T&gt;</code>, has a few requirements
associated with it:</p>
<ul>
<li>
<p>It must implement <code>Fn</code> or <code>FnMut</code> (for <code>FnOnce</code> functions see
<code>Closure::once</code> and <code>Closure::once_into_js</code>).</p>
</li>
<li>
<p>It must be <code>'static</code>, aka no stack references (use the <code>move</code>
keyword).</p>
</li>
<li>
<p>It can have at most 7 arguments.</p>
</li>
<li>
<p>Its arguments and return values are all types that can be shared with
JS (i.e. have <code>#[wasm_bindgen]</code> annotations or are simple numbers,
etc.)</p>
</li>
</ul>
</div><h4 id='method.forget' class="method"><code id='forget.v'>pub fn <a href='#method.forget' class='fnname'>forget</a>(self)</code><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#370-375' title='goto source code'>[src]</a></h4><div class='docblock'><p>Leaks this <code>Closure</code> to ensure it remains valid for the duration of the
entire program.</p>
<blockquote>
<p><strong>Note</strong>: this function will leak memory. It should be used sparingly
to ensure the memory leak doesn't affect the program too much.</p>
</blockquote>
<p>When a <code>Closure</code> is dropped it will invalidate the associated JS
closure, but this isn't always desired. Some callbacks are alive for
the entire duration of the program, so this can be used to conveniently
leak this instance of <code>Closure</code> while performing as much internal
cleanup as it can.</p>
</div></div><h3 id='impl-1' class='impl'><code class='in-band'>impl <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;dyn <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>()&gt;</code><a href='#impl-1' class='anchor'></a><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#381-443' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.once' class="method"><code id='once.v'>pub fn <a href='#method.once' class='fnname'>once</a>&lt;F, A, R&gt;(fn_once: F) -&gt; <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;F::FnMut&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: 'static + WasmClosureFnOnce&lt;A, R&gt;,&nbsp;</span></code><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#409-414' title='goto source code'>[src]</a></h4><div class='docblock'><p>Create a <code>Closure</code> from a function that can only be called once.</p>
<p>Since we have no way of enforcing that JS cannot attempt to call this
<code>FnOne(A...) -&gt; R</code> more than once, this produces a <code>Closure&lt;FnMut(A...) -&gt; R&gt;</code> that will dynamically throw a JavaScript error if called more
than once.</p>
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">wasm_bindgen</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="comment">// Create an non-`Copy`, owned `String`.</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">&quot;Hello&quot;</span>);
<span class="comment">// Close over `s`. Since `f` returns `s`, it is `FnOnce` and can only be</span>
<span class="comment">// called once. If it was called a second time, it wouldn&#39;t have any `s`</span>
<span class="comment">// to work with anymore!</span>
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="kw">move</span> <span class="op">|</span><span class="op">|</span> {
<span class="ident">s</span> <span class="op">+</span><span class="op">=</span> <span class="string">&quot;, World!&quot;</span>;
<span class="ident">s</span>
};
<span class="comment">// Create a `Closure` from `f`. Note that the `Closure`&#39;s type parameter</span>
<span class="comment">// is `FnMut`, even though `f` is `FnOnce`.</span>
<span class="kw">let</span> <span class="ident">closure</span>: <span class="ident">Closure</span><span class="op">&lt;</span><span class="ident">FnMut</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="ident">String</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Closure</span>::<span class="ident">once</span>(<span class="ident">f</span>);</pre></div>
</div><h4 id='method.once_into_js' class="method"><code id='once_into_js.v'>pub fn <a href='#method.once_into_js' class='fnname'>once_into_js</a>&lt;F, A, R&gt;(fn_once: F) -&gt; <a class="struct" href="../../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a> <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: 'static + WasmClosureFnOnce&lt;A, R&gt;,&nbsp;</span></code><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#437-442' title='goto source code'>[src]</a></h4><div class='docblock'><p>Convert a <code>FnOnce(A...) -&gt; R</code> into a JavaScript <code>Function</code> object.</p>
<p>If the JavaScript function is invoked more than once, it will throw an
exception.</p>
<p>Unlike <code>Closure::once</code>, this does <em>not</em> return a <code>Closure</code> that can be
dropped before the function is invoked to deallocate the closure. The
only way the <code>FnOnce</code> is deallocated is by calling the JavaScript
function. If the JavaScript function is never called then the <code>FnOnce</code>
and everything it closes over will leak.</p>
<div class='information'><div class='tooltip ignore'><span class='tooltiptext'>This example is not tested</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered ignore">
<span class="kw">use</span> <span class="ident">js_sys</span>;
<span class="kw">use</span> <span class="ident">wasm_bindgen</span>::{<span class="ident">prelude</span>::<span class="kw-2">*</span>, <span class="ident">JsCast</span>};
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">Closure</span>::<span class="ident">once_into_js</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> {
<span class="comment">// ...</span>
});
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">f</span>.<span class="ident">is_instance_of</span>::<span class="op">&lt;</span><span class="ident">js_sys</span>::<span class="ident">Function</span><span class="op">&gt;</span>());</pre></div>
</div></div><h2 id='trait-implementations' class='small-section-header'>Trait Implementations<a href='#trait-implementations' class='anchor'></a></h2><div id='trait-implementations-list'><h3 id='impl-AsRef%3CJsValue%3E' class='impl'><code class='in-band'>impl&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; <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; for <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt;</code><a href='#impl-AsRef%3CJsValue%3E' class='anchor'></a><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#456-460' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.as_ref' class="method hidden"><code id='as_ref.v'>fn <a href='https://doc.rust-lang.org/nightly/core/convert/trait.AsRef.html#tymethod.as_ref' class='fnname'>as_ref</a>(&amp;self) -&gt; &amp;<a class="struct" href="../../wasm_bindgen/struct.JsValue.html" title="struct wasm_bindgen::JsValue">JsValue</a></code><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#457-459' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Performs the conversion.</p>
</div></div><h3 id='impl-Debug' class='impl'><code class='in-band'>impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a> for <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,&nbsp;</span></code><a href='#impl-Debug' class='anchor'></a><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#493-500' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.fmt' class="method hidden"><code id='fmt.v'>fn <a href='https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html#tymethod.fmt' class='fnname'>fmt</a>(&amp;self, f: &amp;mut <a class="struct" href="https://doc.rust-lang.org/nightly/core/fmt/struct.Formatter.html" title="struct core::fmt::Formatter">Formatter</a>) -&gt; <a class="type" href="https://doc.rust-lang.org/nightly/core/fmt/type.Result.html" title="type core::fmt::Result">Result</a></code><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#497-499' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Formats the value using the given formatter. <a href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html#tymethod.fmt">Read more</a></p>
</div></div><h3 id='impl-Drop' class='impl'><code class='in-band'>impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html" title="trait core::ops::drop::Drop">Drop</a> for <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,&nbsp;</span></code><a href='#impl-Drop' class='anchor'></a><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#502-515' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.drop' class="method hidden"><code id='drop.v'>fn <a href='https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html#tymethod.drop' class='fnname'>drop</a>(&amp;mut self)</code><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#506-514' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Executes the destructor for this type. <a href="https://doc.rust-lang.org/nightly/core/ops/drop/trait.Drop.html#tymethod.drop">Read more</a></p>
</div></div><h3 id='impl-IntoWasmAbi' class='impl'><code class='in-band'>impl&lt;'a, T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; <a class="trait" href="../../wasm_bindgen/convert/trait.IntoWasmAbi.html" title="trait wasm_bindgen::convert::IntoWasmAbi">IntoWasmAbi</a> for &amp;'a <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: WasmClosure,&nbsp;</span></code><a href='#impl-IntoWasmAbi' class='anchor'></a><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#472-481' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Abi' class="type"><code id='Abi.t'>type <a href='../../wasm_bindgen/convert/trait.IntoWasmAbi.html#associatedtype.Abi' class="type">Abi</a> = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code></h4><div class='docblock'><p>The wasm ABI type that this converts into when crossing the ABI boundary. <a href="../../wasm_bindgen/convert/trait.IntoWasmAbi.html#associatedtype.Abi">Read more</a></p>
</div><h4 id='method.into_abi' class="method hidden"><code id='into_abi.v'>fn <a href='../../wasm_bindgen/convert/trait.IntoWasmAbi.html#tymethod.into_abi' class='fnname'>into_abi</a>(self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u32.html">u32</a></code><a class='srclink' href='../../src/wasm_bindgen/closure.rs.html#478-480' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Convert <code>self</code> into <code>Self::Abi</code> so that it can be sent across the wasm ABI boundary. <a href="../../wasm_bindgen/convert/trait.IntoWasmAbi.html#tymethod.into_abi">Read more</a></p>
</div></div></div><h2 id='synthetic-implementations' class='small-section-header'>Auto Trait Implementations<a href='#synthetic-implementations' class='anchor'></a></h2><div id='synthetic-implementations-list'><h3 id='impl-RefUnwindSafe' class='impl'><code class='in-band'>impl&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/std/panic/trait.RefUnwindSafe.html" title="trait std::panic::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="https://doc.rust-lang.org/nightly/std/panic/trait.RefUnwindSafe.html" title="trait std::panic::RefUnwindSafe">RefUnwindSafe</a>,&nbsp;</span></code><a href='#impl-RefUnwindSafe' class='anchor'></a></h3><div class='impl-items'></div><h3 id='impl-Send' class='impl'><code class='in-band'>impl&lt;T&gt; !<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt;</code><a href='#impl-Send' class='anchor'></a></h3><div class='impl-items'></div><h3 id='impl-Sync' class='impl'><code class='in-band'>impl&lt;T&gt; !<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt;</code><a href='#impl-Sync' class='anchor'></a></h3><div class='impl-items'></div><h3 id='impl-Unpin' class='impl'><code class='in-band'>impl&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> for <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt;</code><a href='#impl-Unpin' class='anchor'></a></h3><div class='impl-items'></div><h3 id='impl-UnwindSafe' class='impl'><code class='in-band'>impl&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/std/panic/trait.UnwindSafe.html" title="trait std::panic::UnwindSafe">UnwindSafe</a> for <a class="struct" href="../../wasm_bindgen/closure/struct.Closure.html" title="struct wasm_bindgen::closure::Closure">Closure</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="https://doc.rust-lang.org/nightly/std/panic/trait.UnwindSafe.html" title="trait std::panic::UnwindSafe">UnwindSafe</a>,&nbsp;</span></code><a href='#impl-UnwindSafe' class='anchor'></a></h3><div class='impl-items'></div></div><h2 id='blanket-implementations' class='small-section-header'>Blanket Implementations<a href='#blanket-implementations' class='anchor'></a></h2><div id='blanket-implementations-list'><h3 id='impl-Any' class='impl'><code class='in-band'>impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/any/trait.Any.html" title="trait core::any::Any">Any</a> for T <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: 'static + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,&nbsp;</span></code><a href='#impl-Any' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/any.rs.html#108-112' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.type_id' class="method hidden"><code id='type_id.v'>fn <a href='https://doc.rust-lang.org/nightly/core/any/trait.Any.html#tymethod.type_id' class='fnname'>type_id</a>(&amp;self) -&gt; <a class="struct" href="https://doc.rust-lang.org/nightly/core/any/struct.TypeId.html" title="struct core::any::TypeId">TypeId</a></code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/any.rs.html#109-111' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Gets the <code>TypeId</code> of <code>self</code>. <a href="https://doc.rust-lang.org/nightly/core/any/trait.Any.html#tymethod.type_id">Read more</a></p>
</div></div><h3 id='impl-Borrow%3CT%3E' class='impl'><code class='in-band'>impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html" title="trait core::borrow::Borrow">Borrow</a>&lt;T&gt; for T <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,&nbsp;</span></code><a href='#impl-Borrow%3CT%3E' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#213-217' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.borrow' class="method hidden"><code id='borrow.v'>fn <a href='https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html#tymethod.borrow' class='fnname'>borrow</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;</a>T</code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#214-216' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Immutably borrows from an owned value. <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.Borrow.html#tymethod.borrow">Read more</a></p>
</div></div><h3 id='impl-BorrowMut%3CT%3E' class='impl'><code class='in-band'>impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html" title="trait core::borrow::BorrowMut">BorrowMut</a>&lt;T&gt; for T <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,&nbsp;</span></code><a href='#impl-BorrowMut%3CT%3E' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#220-224' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.borrow_mut' class="method hidden"><code id='borrow_mut.v'>fn <a href='https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut' class='fnname'>borrow_mut</a>(&amp;mut self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>T</code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/borrow.rs.html#221-223' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Mutably borrows from an owned value. <a href="https://doc.rust-lang.org/nightly/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut">Read more</a></p>
</div></div><h3 id='impl-From%3CT%3E' class='impl'><code class='in-band'>impl&lt;T&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt; for T</code><a href='#impl-From%3CT%3E' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#565-569' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.from' class="method hidden"><code id='from.v'>fn <a href='https://doc.rust-lang.org/nightly/core/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(t: T) -&gt; T</code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#566-568' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Performs the conversion.</p>
</div></div><h3 id='impl-Into%3CU%3E' class='impl'><code class='in-band'>impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;U&gt; for T <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;U: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;T&gt;,&nbsp;</span></code><a href='#impl-Into%3CU%3E' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#554-561' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.into' class="method hidden"><code id='into.v'>fn <a href='https://doc.rust-lang.org/nightly/core/convert/trait.Into.html#tymethod.into' class='fnname'>into</a>(self) -&gt; U</code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#558-560' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Performs the conversion.</p>
</div></div><h3 id='impl-TryFrom%3CU%3E' class='impl'><code class='in-band'>impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;U&gt; for T <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;U: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;T&gt;,&nbsp;</span></code><a href='#impl-TryFrom%3CU%3E' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#602-611' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Error' class="type"><code id='Error.t'>type <a href='https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error' class="type">Error</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a></code></h4><div class='docblock'><p>The type returned in the event of a conversion error.</p>
</div><h4 id='method.try_from' class="method hidden"><code id='try_from.v'>fn <a href='https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#tymethod.try_from' class='fnname'>try_from</a>(value: U) -&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, &lt;T as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;U&gt;&gt;::<a class="type" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>&gt;</code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#608-610' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Performs the conversion.</p>
</div></div><h3 id='impl-TryInto%3CU%3E' class='impl'><code class='in-band'>impl&lt;T, U&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html" title="trait core::convert::TryInto">TryInto</a>&lt;U&gt; for T <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;U: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;,&nbsp;</span></code><a href='#impl-TryInto%3CU%3E' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#588-597' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Error-1' class="type"><code id='Error.t-1'>type <a href='https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html#associatedtype.Error' class="type">Error</a> = &lt;U as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;&gt;::<a class="type" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a></code></h4><div class='docblock'><p>The type returned in the event of a conversion error.</p>
</div><h4 id='method.try_into' class="method hidden"><code id='try_into.v'>fn <a href='https://doc.rust-lang.org/nightly/core/convert/trait.TryInto.html#tymethod.try_into' class='fnname'>try_into</a>(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;U, &lt;U as <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;T&gt;&gt;::<a class="type" href="https://doc.rust-lang.org/nightly/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>&gt;</code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/convert/mod.rs.html#594-596' title='goto source code'>[src]</a></h4><div class='docblock hidden'><p>Performs the conversion.</p>
</div></div></div></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>