173 lines
10 KiB
HTML
173 lines
10 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 `Error` trait in crate `iron`.">
|
|||
|
<meta name="keywords" content="rust, rustlang, rust-lang, Error">
|
|||
|
|
|||
|
<title>iron::Error - Rust</title>
|
|||
|
|
|||
|
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
|
|||
|
<link rel="stylesheet" type="text/css" href="../main.css">
|
|||
|
|
|||
|
|
|||
|
<link rel="shortcut icon" href="https://avatars0.githubusercontent.com/u/7853871?s=256">
|
|||
|
|
|||
|
</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">
|
|||
|
<a href='../iron/index.html'><img src='https://avatars0.githubusercontent.com/u/7853871?s=128' alt='logo' width='100'></a>
|
|||
|
<p class='location'><a href='index.html'>iron</a></p><script>window.sidebarCurrent = {name: 'Error', ty: 'trait', 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 trait">
|
|||
|
<h1 class='fqn'><span class='in-band'>Trait <a href='index.html'>iron</a>::<wbr><a class='trait' href=''>Error</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-11' class='srclink' href='../error/trait.Error.html?gotosrc=11' title='goto source code'>[src]</a></span></h1>
|
|||
|
<pre class='rust trait'>pub trait Error: <a class='trait' href='https://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='https://doc.rust-lang.org/nightly/std/error/trait.Error.html' title='std::error::Error'>Error</a> + <a class='trait' href='https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html' title='core::fmt::Debug'>Debug</a> + <a class='trait' href='../typeable/trait.Typeable.html' title='typeable::Typeable'>Typeable</a> { }</pre><div class='docblock'><p>An extension to std::error::Error which provides dynamic downcasting of
|
|||
|
errors for use in highly generic contexts.</p>
|
|||
|
|
|||
|
<h2 id='when-to-use-this-trait' class='section-header'><a href='#when-to-use-this-trait'>When to use this trait</a></h2>
|
|||
|
<p>In the vast majority of cases, a library-specific <code>enum</code> should be used
|
|||
|
for cases where there can be many different types of errors. This has
|
|||
|
the benefit of being very performant and benefiting from all sorts
|
|||
|
of static checking at both the instantiation site and the handling
|
|||
|
site of the error.</p>
|
|||
|
|
|||
|
<p>In other cases, being generic over <code>std::error::Error</code> may be correct
|
|||
|
- usually for logging errors or in other places where an error is
|
|||
|
used as <em>input</em>.</p>
|
|||
|
|
|||
|
<p>Now, a motivating example for this trait, which doesn't fall under
|
|||
|
either of these cases:</p>
|
|||
|
|
|||
|
<p>Imagine we are creating a simple web middleware for verifying incoming
|
|||
|
HTTP requests. It will take in many different user-defined <code>Verifier</code>s
|
|||
|
and will call them one after the other, rejecting the request on any
|
|||
|
error.</p>
|
|||
|
|
|||
|
<p>The first step would be to write a <code>Verifier</code> trait:</p>
|
|||
|
|
|||
|
<pre class='rust rust-example-rendered'>
|
|||
|
<span class='kw'>pub</span> <span class='kw'>trait</span> <span class='ident'>Verifier</span> {
|
|||
|
<span class='doccomment'>/// Verify the request, yielding an error if the request is invalid.</span>
|
|||
|
<span class='kw'>fn</span> <span class='ident'>verify</span>(<span class='kw-2'>&</span><span class='ident'>Request</span>) <span class='op'>-></span> <span class='prelude-ty'>Result</span><span class='op'><</span>(), ???<span class='op'>></span>;
|
|||
|
}</pre>
|
|||
|
|
|||
|
<p>A problem quickly arises - what type do we use for the <code>Err</code> case? We
|
|||
|
cannot use a concrete type since each <code>Verifier</code> may wish to throw
|
|||
|
any number of different errors, and we cannot use a generic since
|
|||
|
the type is chosen by the implementor, not the caller, and it cannot
|
|||
|
be a generic on the trait since we will want to store many <code>Verifier</code>s
|
|||
|
together.</p>
|
|||
|
|
|||
|
<p>Enter: <code>Box<error::Error></code>, a type which can be used to represent
|
|||
|
any <code>std::error::Error</code> with the sufficient bounds, and can <em>also</em>
|
|||
|
be handled later by downcasting it to the right error using either
|
|||
|
<code>.downcast</code> or the <code>match_error!</code> macro. This type can be used to meet
|
|||
|
the needs of consumers like <code>Verifier</code>, but should not be used in cases
|
|||
|
where enums or generics are better suited.</p>
|
|||
|
</div><h2 id='methods'>Methods</h2><h3 class='impl'><span class='in-band'><code>impl <a class='trait' href='../iron/error/trait.Error.html' title='iron::error::Error'>Error</a> + 'static</code></span><span class='out-of-band'></span></h3>
|
|||
|
<div class='impl-items'><h4 id='method.is' class='method'><code>fn <a href='#method.is' class='fnname'>is</a><E>(&self) -> <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.bool.html'>bool</a> <span class='where'>where E: <a class='trait' href='../iron/error/trait.Error.html' title='iron::error::Error'>Error</a></span></code></h4>
|
|||
|
<div class='docblock'><p>Is this <code>Error</code> object of type <code>E</code>?</p>
|
|||
|
</div><h4 id='method.downcast' class='method'><code>fn <a href='#method.downcast' class='fnname'>downcast</a><E>(&self) -> <a class='enum' href='https://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a><&E> <span class='where'>where E: <a class='trait' href='../iron/error/trait.Error.html' title='iron::error::Error'>Error</a></span></code></h4>
|
|||
|
<div class='docblock'><p>If this error is <code>E</code>, downcast this error to <code>E</code>, by reference.</p>
|
|||
|
</div></div><h3 class='impl'><span class='in-band'><code>impl <a class='trait' href='../iron/error/trait.Error.html' title='iron::error::Error'>Error</a> + 'static + <a class='trait' href='https://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a></code></span><span class='out-of-band'></span></h3>
|
|||
|
<div class='impl-items'><h4 id='method.is-1' class='method'><code>fn <a href='#method.is-1' class='fnname'>is</a><E>(&self) -> <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.bool.html'>bool</a> <span class='where'>where E: <a class='trait' href='https://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='../iron/error/trait.Error.html' title='iron::error::Error'>Error</a></span></code></h4>
|
|||
|
<div class='docblock'><p>Is this <code>Error + Send</code> object of type <code>E</code>?</p>
|
|||
|
</div><h4 id='method.downcast-1' class='method'><code>fn <a href='#method.downcast-1' class='fnname'>downcast</a><E>(&self) -> <a class='enum' href='https://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a><&E> <span class='where'>where E: <a class='trait' href='https://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='../iron/error/trait.Error.html' title='iron::error::Error'>Error</a></span></code></h4>
|
|||
|
<div class='docblock'><p>If this error is <code>E</code>, downcast this error to <code>E</code>, by reference.</p>
|
|||
|
</div></div>
|
|||
|
<h2 id='implementors'>Implementors</h2>
|
|||
|
<ul class='item-list' id='implementors-list'>
|
|||
|
<li><code>impl<S> Error for S <span class='where'>where S: <a class='trait' href='https://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + <a class='trait' href='https://doc.rust-lang.org/nightly/std/error/trait.Error.html' title='std::error::Error'>Error</a> + <a class='trait' href='https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html' title='core::fmt::Debug'>Debug</a> + <a class='trait' href='../typeable/trait.Typeable.html' title='typeable::Typeable'>Typeable</a></span></code></li>
|
|||
|
</ul><script type="text/javascript" async
|
|||
|
src="../implementors/error/trait.Error.js">
|
|||
|
</script></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>
|
|||
|
<dt>+</dt>
|
|||
|
<dd>Collapse/expand all sections</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 = "iron";
|
|||
|
window.playgroundUrl = "";
|
|||
|
</script>
|
|||
|
<script src="../jquery.js"></script>
|
|||
|
<script src="../main.js"></script>
|
|||
|
|
|||
|
<script defer src="../search-index.js"></script>
|
|||
|
</body>
|
|||
|
</html>
|