166 lines
No EOL
7.8 KiB
HTML
166 lines
No EOL
7.8 KiB
HTML
<!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 `AssertUnwindSafe` struct in crate `bitflags`.">
|
||
<meta name="keywords" content="rust, rustlang, rust-lang, AssertUnwindSafe">
|
||
|
||
<title>bitflags::__core::panic::AssertUnwindSafe - Rust</title>
|
||
|
||
<link rel="stylesheet" type="text/css" href="../../../rustdoc.css">
|
||
<link rel="stylesheet" type="text/css" href="../../../main.css">
|
||
|
||
|
||
|
||
|
||
</head>
|
||
<body class="rustdoc">
|
||
<!--[if lte IE 8]>
|
||
<div class="warning">
|
||
This old browser is unsupported and will most likely display funky
|
||
things.
|
||
</div>
|
||
<![endif]-->
|
||
|
||
|
||
|
||
<nav class="sidebar">
|
||
|
||
<p class='location'><a href='../../index.html'>bitflags</a>::<wbr><a href='../index.html'>__core</a>::<wbr><a href='index.html'>panic</a></p><script>window.sidebarCurrent = {name: 'AssertUnwindSafe', ty: 'struct', relpath: ''};</script><script defer src="sidebar-items.js"></script>
|
||
</nav>
|
||
|
||
<nav class="sub">
|
||
<form class="search-form js-only">
|
||
<div class="search-container">
|
||
<input class="search-input" name="search"
|
||
autocomplete="off"
|
||
placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
|
||
type="search">
|
||
</div>
|
||
</form>
|
||
</nav>
|
||
|
||
<section id='main' class="content struct">
|
||
<h1 class='fqn'><span class='in-band'>Struct <a href='../../index.html'>bitflags</a>::<wbr><a href='../index.html'>__core</a>::<wbr><a href='index.html'>panic</a>::<wbr><a class='struct' href=''>AssertUnwindSafe</a></span><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'>−</span>]
|
||
</a>
|
||
</span><a id='src-6732' class='srclink' href='https://doc.rust-lang.org/nightly/std/panic/struct.AssertUnwindSafe.html?gotosrc=6732' title='goto source code'>[src]</a></span></h1>
|
||
<pre class='rust struct'>pub struct AssertUnwindSafe<T>(pub T);</pre><span class="since">1.9.0</span><div class='docblock'><p>A simple wrapper around a type to assert that it is panic safe.</p>
|
||
|
||
<p>When using <code>recover</code> it may be the case that some of the closed over
|
||
variables are not panic safe. For example if <code>&mut T</code> is captured the
|
||
compiler will generate a warning indicating that it is not panic safe. It
|
||
may not be the case, however, that this is actually a problem due to the
|
||
specific usage of <code>recover</code> if panic safety is specifically taken into
|
||
account. This wrapper struct is useful for a quick and lightweight
|
||
annotation that a variable is indeed panic safe.</p>
|
||
|
||
<h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1>
|
||
<p>One way to use <code>AssertUnwindSafe</code> is to assert that the entire closure
|
||
itself is recover safe, bypassing all checks for all variables:</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>panic</span>::{<span class='self'>self</span>, <span class='ident'>AssertUnwindSafe</span>};
|
||
|
||
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>variable</span> <span class='op'>=</span> <span class='number'>4</span>;
|
||
|
||
<span class='comment'>// This code will not compile because the closure captures `&mut variable`</span>
|
||
<span class='comment'>// which is not considered panic safe by default.</span>
|
||
|
||
<span class='comment'>// panic::catch_unwind(|| {</span>
|
||
<span class='comment'>// variable += 3;</span>
|
||
<span class='comment'>// });</span>
|
||
|
||
<span class='comment'>// This, however, will compile due to the `AssertUnwindSafe` wrapper</span>
|
||
<span class='kw'>let</span> <span class='ident'>result</span> <span class='op'>=</span> <span class='ident'>panic</span>::<span class='ident'>catch_unwind</span>(<span class='ident'>AssertUnwindSafe</span>(<span class='op'>||</span> {
|
||
<span class='ident'>variable</span> <span class='op'>+=</span> <span class='number'>3</span>;
|
||
}));
|
||
<span class='comment'>// ...</span></pre>
|
||
|
||
<p>Wrapping the entire closure amounts to a blanket assertion that all captured
|
||
variables are unwind safe. This has the downside that if new captures are
|
||
added in the future, they will also be considered unwind safe. Therefore,
|
||
you may prefer to just wrap individual captures, as shown below. This is
|
||
more annotation, but it ensures that if a new capture is added which is not
|
||
unwind safe, you will get a compilation error at that time, which will
|
||
allow you to consider whether that new capture in fact represent a bug or
|
||
not.</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>panic</span>::{<span class='self'>self</span>, <span class='ident'>AssertUnwindSafe</span>};
|
||
|
||
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>variable</span> <span class='op'>=</span> <span class='number'>4</span>;
|
||
<span class='kw'>let</span> <span class='ident'>other_capture</span> <span class='op'>=</span> <span class='number'>3</span>;
|
||
|
||
<span class='kw'>let</span> <span class='ident'>result</span> <span class='op'>=</span> {
|
||
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>wrapper</span> <span class='op'>=</span> <span class='ident'>AssertUnwindSafe</span>(<span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='ident'>variable</span>);
|
||
<span class='ident'>panic</span>::<span class='ident'>catch_unwind</span>(<span class='kw'>move</span> <span class='op'>||</span> {
|
||
<span class='op'>*</span><span class='op'>*</span><span class='ident'>wrapper</span> <span class='op'>+=</span> <span class='ident'>other_capture</span>;
|
||
})
|
||
};
|
||
<span class='comment'>// ...</span></pre>
|
||
</div></section>
|
||
<section id='search' class="content hidden"></section>
|
||
|
||
<section class="footer"></section>
|
||
|
||
<aside id="help" class="hidden">
|
||
<div>
|
||
<h1 class="hidden">Help</h1>
|
||
|
||
<div class="shortcuts">
|
||
<h2>Keyboard Shortcuts</h2>
|
||
|
||
<dl>
|
||
<dt>?</dt>
|
||
<dd>Show this help dialog</dd>
|
||
<dt>S</dt>
|
||
<dd>Focus the search field</dd>
|
||
<dt>⇤</dt>
|
||
<dd>Move up in search results</dd>
|
||
<dt>⇥</dt>
|
||
<dd>Move down in search results</dd>
|
||
<dt>⏎</dt>
|
||
<dd>Go to active search result</dd>
|
||
</dl>
|
||
</div>
|
||
|
||
<div class="infos">
|
||
<h2>Search Tricks</h2>
|
||
|
||
<p>
|
||
Prefix searches with a type followed by a colon (e.g.
|
||
<code>fn:</code>) to restrict the search to a given type.
|
||
</p>
|
||
|
||
<p>
|
||
Accepted types are: <code>fn</code>, <code>mod</code>,
|
||
<code>struct</code>, <code>enum</code>,
|
||
<code>trait</code>, <code>type</code>, <code>macro</code>,
|
||
and <code>const</code>.
|
||
</p>
|
||
|
||
<p>
|
||
Search functions by type signature (e.g.
|
||
<code>vec -> usize</code> or <code>* -> vec</code>)
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</aside>
|
||
|
||
|
||
|
||
<script>
|
||
window.rootPath = "../../../";
|
||
window.currentCrate = "bitflags";
|
||
window.playgroundUrl = "";
|
||
</script>
|
||
<script src="../../../jquery.js"></script>
|
||
<script src="../../../main.js"></script>
|
||
|
||
<script defer src="../../../search-index.js"></script>
|
||
</body>
|
||
</html> |