rusty-man/tests/html/1.46.0/anyhow/trait.Context.html
2020-09-11 20:30:19 +02:00

114 lines
22 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 `Context` trait in crate `anyhow`."><meta name="keywords" content="rust, rustlang, rust-lang, Context"><title>anyhow::Context - 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='../anyhow/index.html'><div class='logo-container'><img src='../rust-logo.png' alt='logo'></div></a><p class='location'>Trait Context</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.context">context</a><a href="#tymethod.with_context">with_context</a></div><a class="sidebar-title" href="#foreign-impls">Implementations on Foreign Types</a><div class="sidebar-links"><a href="#impl-Context%3CT%2C%20Infallible%3E-for-Option%3CT%3E">Option&lt;T&gt;</a><a href="#impl-Context%3CT%2C%20E%3E-for-Result%3CT%2C%20E%3E">Result&lt;T, E&gt;</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='index.html'>anyhow</a></p><script>window.sidebarCurrent = {name: 'Context', 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/anyhow/lib.rs.html#573-585' title='goto source code'>[src]</a></span><span class='in-band'>Trait <a href='index.html'>anyhow</a>::<wbr><a class="trait" href=''>Context</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class='rust trait'>pub trait Context&lt;T, E&gt;: Sealed {
fn <a href='#tymethod.context' class='fnname'>context</a>&lt;C&gt;(self, context: C) -&gt; <a class="type" href="../anyhow/type.Result.html" title="type anyhow::Result">Result</a>&lt;T, <a class="struct" href="../anyhow/struct.Error.html" title="struct anyhow::Error">Error</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static</span>;
<div class='item-spacer'></div> fn <a href='#tymethod.with_context' class='fnname'>with_context</a>&lt;C, F&gt;(self, f: F) -&gt; <a class="type" href="../anyhow/type.Result.html" title="type anyhow::Result">Result</a>&lt;T, <a class="struct" href="../anyhow/struct.Error.html" title="struct anyhow::Error">Error</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F: <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; C</span>;
}</pre></div><div class='docblock'><p>Provides the <code>context</code> method for <code>Result</code>.</p>
<p>This trait is sealed and cannot be implemented for types outside of
<code>anyhow</code>.</p>
<br>
<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">anyhow</span>::{<span class="ident">Context</span>, <span class="prelude-ty">Result</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fs</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">path</span>::<span class="ident">PathBuf</span>;
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">ImportantThing</span> {
<span class="ident">path</span>: <span class="ident">PathBuf</span>,
}
<span class="kw">impl</span> <span class="ident">ImportantThing</span> {
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">detach</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {...}
}
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">do_it</span>(<span class="kw-2">mut</span> <span class="ident">it</span>: <span class="ident">ImportantThing</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;</span><span class="op">&gt;</span> {
<span class="ident">it</span>.<span class="ident">detach</span>().<span class="ident">context</span>(<span class="string">&quot;Failed to detach the important thing&quot;</span>)<span class="question-mark">?</span>;
<span class="kw">let</span> <span class="ident">path</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="ident">it</span>.<span class="ident">path</span>;
<span class="kw">let</span> <span class="ident">content</span> <span class="op">=</span> <span class="ident">fs</span>::<span class="ident">read</span>(<span class="ident">path</span>)
.<span class="ident">with_context</span>(<span class="op">|</span><span class="op">|</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">&quot;Failed to read instrs from {}&quot;</span>, <span class="ident">path</span>.<span class="ident">display</span>()))<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(<span class="ident">content</span>)
}</pre></div>
<p>When printed, the outermost context would be printed first and the lower
level underlying causes would be enumerated below.</p>
<pre><code class="language-console">Error: Failed to read instrs from ./path/to/instrs.json
Caused by:
No such file or directory (os error 2)
</code></pre>
<br>
<h1 id="effect-on-downcasting" class="section-header"><a href="#effect-on-downcasting">Effect on downcasting</a></h1>
<p>After attaching context of type <code>C</code> onto an error of type <code>E</code>, the resulting
<code>anyhow::Error</code> may be downcast to <code>C</code> <strong>or</strong> to <code>E</code>.</p>
<p>That is, in codebases that rely on downcasting, Anyhow's context supports
both of the following use cases:</p>
<ul>
<li>
<p><strong>Attaching context whose type is insignificant onto errors whose type
is used in downcasts.</strong></p>
<p>In other error libraries whose context is not designed this way, it can
be risky to introduce context to existing code because new context might
break existing working downcasts. In Anyhow, any downcast that worked
before adding context will continue to work after you add a context, so
you should freely add human-readable context to errors wherever it would
be helpful.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">anyhow</span>::{<span class="ident">Context</span>, <span class="prelude-ty">Result</span>};
<span class="kw">fn</span> <span class="ident">do_it</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {
<span class="ident">helper</span>().<span class="ident">context</span>(<span class="string">&quot;Failed to complete the work&quot;</span>)<span class="question-mark">?</span>;
...
}
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">err</span> <span class="op">=</span> <span class="ident">do_it</span>().<span class="ident">unwrap_err</span>();
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">e</span>) <span class="op">=</span> <span class="ident">err</span>.<span class="ident">downcast_ref</span>::<span class="op">&lt;</span><span class="ident">SuspiciousError</span><span class="op">&gt;</span>() {
<span class="comment">// If helper() returned SuspiciousError, this downcast will</span>
<span class="comment">// correctly succeed even with the context in between.</span>
}
}</pre></div>
</li>
<li>
<p><strong>Attaching context whose type is used in downcasts onto errors whose
type is insignificant.</strong></p>
<p>Some codebases prefer to use machine-readable context to categorize
lower level errors in a way that will be actionable to higher levels of
the application.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">anyhow</span>::{<span class="ident">Context</span>, <span class="prelude-ty">Result</span>};
<span class="kw">fn</span> <span class="ident">do_it</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {
<span class="ident">helper</span>().<span class="ident">context</span>(<span class="ident">HelperFailed</span>)<span class="question-mark">?</span>;
...
}
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">err</span> <span class="op">=</span> <span class="ident">do_it</span>().<span class="ident">unwrap_err</span>();
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">e</span>) <span class="op">=</span> <span class="ident">err</span>.<span class="ident">downcast_ref</span>::<span class="op">&lt;</span><span class="ident">HelperFailed</span><span class="op">&gt;</span>() {
<span class="comment">// If helper failed, this downcast will succeed because</span>
<span class="comment">// HelperFailed is the context that has been attached to</span>
<span class="comment">// that error.</span>
}
}</pre></div>
</li>
</ul>
</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.context' class='method'><code>fn <a href='#tymethod.context' class='fnname'>context</a>&lt;C&gt;(self, context: C) -&gt; <a class="type" href="../anyhow/type.Result.html" title="type anyhow::Result">Result</a>&lt;T, <a class="struct" href="../anyhow/struct.Error.html" title="struct anyhow::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;C: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,&nbsp;</span></code></h3><div class='docblock'><p>Wrap the error value with additional context.</p>
</div><h3 id='tymethod.with_context' class='method'><code>fn <a href='#tymethod.with_context' class='fnname'>with_context</a>&lt;C, F&gt;(self, f: F) -&gt; <a class="type" href="../anyhow/type.Result.html" title="type anyhow::Result">Result</a>&lt;T, <a class="struct" href="../anyhow/struct.Error.html" title="struct anyhow::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;C: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <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; C,&nbsp;</span></code></h3><div class='docblock'><p>Wrap the error value with additional context that is evaluated lazily
only once an error does occur.</p>
</div></div><span class='loading-content'>Loading content...</span>
<h2 id='foreign-impls' class='small-section-header'>Implementations on Foreign Types<a href='#foreign-impls' class='anchor'></a></h2><h3 id='impl-Context%3CT%2C%20E%3E-for-Result%3CT%2C%20E%3E' class='impl'><code class='in-band'>impl&lt;T, E&gt; <a class="trait" href="../anyhow/trait.Context.html" title="trait anyhow::Context">Context</a>&lt;T, E&gt; for <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, E&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;E: StdError + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,&nbsp;</span></code><a href='#impl-Context%3CT%2C%20E%3E-for-Result%3CT%2C%20E%3E' class='anchor'></a><a class='srclink' href='../src/anyhow/context.rs.html#42-60' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='method.context' class="method hidden"><code>fn <a href='#method.context' class='fnname'>context</a>&lt;C&gt;(self, context: C) -&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, <a class="struct" href="../anyhow/struct.Error.html" title="struct anyhow::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;C: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,&nbsp;</span></code><a class='srclink' href='../src/anyhow/context.rs.html#46-51' title='goto source code'>[src]</a></h4><h4 id='method.with_context' class="method hidden"><code>fn <a href='#method.with_context' class='fnname'>with_context</a>&lt;C, F&gt;(self, context: F) -&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, <a class="struct" href="../anyhow/struct.Error.html" title="struct anyhow::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;C: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <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; C,&nbsp;</span></code><a class='srclink' href='../src/anyhow/context.rs.html#53-59' title='goto source code'>[src]</a></h4></div><h3 id='impl-Context%3CT%2C%20Infallible%3E-for-Option%3CT%3E' class='impl'><code class='in-band'>impl&lt;T&gt; <a class="trait" href="../anyhow/trait.Context.html" title="trait anyhow::Context">Context</a>&lt;T, <a class="enum" href="https://doc.rust-lang.org/nightly/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a>&gt; for <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;T&gt;</code><a href='#impl-Context%3CT%2C%20Infallible%3E-for-Option%3CT%3E' class='anchor'></a><a class='srclink' href='../src/anyhow/context.rs.html#82-97' title='goto source code'>[src]</a></h3><div class='docblock'>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">anyhow</span>::{<span class="ident">Context</span>, <span class="prelude-ty">Result</span>};
<span class="kw">fn</span> <span class="ident">maybe_get</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
...
}
<span class="kw">fn</span> <span class="ident">demo</span>() <span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {
<span class="kw">let</span> <span class="ident">t</span> <span class="op">=</span> <span class="ident">maybe_get</span>().<span class="ident">context</span>(<span class="string">&quot;there is no T&quot;</span>)<span class="question-mark">?</span>;
...
}</pre></div>
</div><div class='impl-items'><h4 id='method.context-1' class="method hidden"><code>fn <a href='#method.context' class='fnname'>context</a>&lt;C&gt;(self, context: C) -&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, <a class="struct" href="../anyhow/struct.Error.html" title="struct anyhow::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;C: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,&nbsp;</span></code><a class='srclink' href='../src/anyhow/context.rs.html#83-88' title='goto source code'>[src]</a></h4><h4 id='method.with_context-1' class="method hidden"><code>fn <a href='#method.with_context' class='fnname'>with_context</a>&lt;C, F&gt;(self, context: F) -&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, <a class="struct" href="../anyhow/struct.Error.html" title="struct anyhow::Error">Error</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;C: <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <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; C,&nbsp;</span></code><a class='srclink' href='../src/anyhow/context.rs.html#90-96' title='goto source code'>[src]</a></h4></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'></div><span class='loading-content'>Loading content...</span><script type="text/javascript" src="../implementors/anyhow/trait.Context.js" async></script></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../";window.currentCrate = "anyhow";</script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>