111 lines
24 KiB
HTML
111 lines
24 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="Provides the `context` method for `Result`."><meta name="keywords" content="rust, rustlang, rust-lang, Context"><title>Context in anyhow - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><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="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script src="../crates.js"></script><script defer src="../main.js"></script>
|
||
<noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">☰</div><a class="sidebar-logo" href="../anyhow/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.png" alt="logo"></div>
|
||
</a><h2 class="location">Trait Context</h2><div class="sidebar-elems"><div class="block items"><h3 class="sidebar-title"><a href="#required-methods">Required Methods</a></h3><div class="sidebar-links"><a href="#tymethod.context">context</a><a href="#tymethod.with_context">with_context</a></div><h3 class="sidebar-title"><a href="#foreign-impls">Implementations on Foreign Types</a></h3><div class="sidebar-links"><a href="#impl-Context%3CT%2C%20Infallible%3E-for-Option%3CT%3E">Option<T></a><a href="#impl-Context%3CT%2C%20E%3E-for-Result%3CT%2C%20E%3E">Result<T, E></a></div><h3 class="sidebar-title"><a href="#implementors">Implementors</a></h3></div><h2 class="location">Other items in<br><a href="index.html">anyhow</a></h2><div id="sidebar-vars" data-name="Context" data-ty="trait" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../anyhow/index.html"><img class="rust-logo" src="../rust-logo.png" alt="logo"></a><nav class="sub"><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../brush.svg"></button><div id="theme-choices" role="menu"></div></div><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" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../wheel.svg"></a></div></form></nav></div><section id="main-content" class="content"><h1 class="fqn"><span class="in-band">Trait <a href="index.html">anyhow</a>::<wbr><a class="trait" href="#">Context</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></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 class="srclink" href="../src/anyhow/lib.rs.html#597-609" title="goto source code">[src]</a></span></h1><div class="docblock item-decl"><pre class="rust trait"><code>pub trait Context<T, E>: Sealed {
|
||
fn <a href="#tymethod.context" class="fnname">context</a><C>(self, context: C) -> <a class="type" href="type.Result.html" title="type anyhow::Result">Result</a><T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>><br> <span class="where">where<br> C: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/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><C, F>(self, f: F) -> <a class="type" href="type.Result.html" title="type anyhow::Result">Result</a><T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>><br> <span class="where">where<br> C: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,<br> F: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> C</span>;
|
||
}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><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>
|
||
<h2 id="example" class="section-header"><a href="#example">Example</a></h2>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><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::fs</span>;
|
||
<span class="kw">use</span> <span class="ident">std::path::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">&mut</span> <span class="self">self</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span>()<span class="op">></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="prelude-ty">Result</span><span class="op"><</span><span class="ident">Vec</span><span class="op"><</span><span class="ident">u8</span><span class="op">></span><span class="op">></span> {
|
||
<span class="ident">it</span>.<span class="ident">detach</span>().<span class="ident">context</span>(<span class="string">"Failed to detach the important thing"</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">path</span> <span class="op">=</span> <span class="kw-2">&</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::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="string">"Failed to read instrs from {}"</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>)
|
||
}</code></pre></div>
|
||
<p>When printed, the outermost context would be printed first and the lower
|
||
level underlying causes would be enumerated below.</p>
|
||
<div class="example-wrap"><pre class="language-console"><code>Error: Failed to read instrs from ./path/to/instrs.json
|
||
|
||
Caused by:
|
||
No such file or directory (os error 2)</code></pre></div>
|
||
<p>Refer to the <a href="struct.Error.html#display-representations">Display representations</a> documentation for other forms in
|
||
which this context chain can be rendered.</p>
|
||
<br>
|
||
<h2 id="effect-on-downcasting" class="section-header"><a href="#effect-on-downcasting">Effect on downcasting</a></h2>
|
||
<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"><code><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="prelude-ty">Result</span><span class="op"><</span>()<span class="op">></span> {
|
||
<span class="ident">helper</span>().<span class="ident">context</span>(<span class="string">"Failed to complete the work"</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"><</span><span class="ident">SuspiciousError</span><span class="op">></span>() {
|
||
<span class="comment">// If helper() returned SuspiciousError, this downcast will</span>
|
||
<span class="comment">// correctly succeed even with the context in between.</span>
|
||
}
|
||
}</code></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"><code><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="prelude-ty">Result</span><span class="op"><</span>()<span class="op">></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"><</span><span class="ident">HelperFailed</span><span class="op">></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>
|
||
}
|
||
}</code></pre></div>
|
||
</li>
|
||
</ul>
|
||
</div></details><h2 id="required-methods" class="small-section-header">Required methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle" open><summary><div id="tymethod.context" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/anyhow/lib.rs.html#599-601" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#tymethod.context" class="fnname">context</a><C>(self, context: C) -> <a class="type" href="type.Result.html" title="type anyhow::Result">Result</a><T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>> <span class="where fmt-newline">where<br> C: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static, </span></h4></div></summary><div class="docblock"><p>Wrap the error value with additional context.</p>
|
||
</div></details><details class="rustdoc-toggle" open><summary><div id="tymethod.with_context" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/anyhow/lib.rs.html#605-608" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#tymethod.with_context" class="fnname">with_context</a><C, F>(self, f: F) -> <a class="type" href="type.Result.html" title="type anyhow::Result">Result</a><T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>> <span class="where fmt-newline">where<br> C: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,<br> F: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> C, </span></h4></div></summary><div class="docblock"><p>Wrap the error value with additional context that is evaluated lazily
|
||
only once an error does occur.</p>
|
||
</div></details></div><h2 id="foreign-impls" class="small-section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor"></a></h2><details class="rustdoc-toggle implementors-toggle"><summary><div id="impl-Context%3CT%2C%20E%3E-for-Result%3CT%2C%20E%3E" class="impl has-srclink"><div class="rightside"><a class="srclink" href="../src/anyhow/context.rs.html#42-60" title="goto source code">[src]</a></div><a href="#impl-Context%3CT%2C%20E%3E-for-Result%3CT%2C%20E%3E" class="anchor"></a><h3 class="code-header in-band">impl<T, E> <a class="trait" href="trait.Context.html" title="trait anyhow::Context">Context</a><T, E> for <a class="enum" href="https://doc.rust-lang.org/1.59.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, E> <span class="where fmt-newline">where<br> E: StdError + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static, </span></h3></div></summary><div class="impl-items"><div id="method.context" class="method trait-impl has-srclink"><div class="rightside"><a class="srclink" href="../src/anyhow/context.rs.html#46-51" title="goto source code">[src]</a></div><a href="#method.context" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.context" class="fnname">context</a><C>(self, context: C) -> <a class="enum" href="https://doc.rust-lang.org/1.59.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>> <span class="where fmt-newline">where<br> C: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static, </span></h4></div><div id="method.with_context" class="method trait-impl has-srclink"><div class="rightside"><a class="srclink" href="../src/anyhow/context.rs.html#53-59" title="goto source code">[src]</a></div><a href="#method.with_context" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.with_context" class="fnname">with_context</a><C, F>(self, context: F) -> <a class="enum" href="https://doc.rust-lang.org/1.59.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>> <span class="where fmt-newline">where<br> C: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,<br> F: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> C, </span></h4></div></div></details><details class="rustdoc-toggle implementors-toggle"><summary><div id="impl-Context%3CT%2C%20Infallible%3E-for-Option%3CT%3E" class="impl has-srclink"><div class="rightside"><a class="srclink" href="../src/anyhow/context.rs.html#82-97" title="goto source code">[src]</a></div><a href="#impl-Context%3CT%2C%20Infallible%3E-for-Option%3CT%3E" class="anchor"></a><h3 class="code-header in-band">impl<T> <a class="trait" href="trait.Context.html" title="trait anyhow::Context">Context</a><T, <a class="enum" href="https://doc.rust-lang.org/1.59.0/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a>> for <a class="enum" href="https://doc.rust-lang.org/1.59.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><T></h3></div></summary><div class="docblock">
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><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="prelude-ty">Option</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> {
|
||
...
|
||
}
|
||
|
||
<span class="kw">fn</span> <span class="ident">demo</span>() -> <span class="prelude-ty">Result</span><span class="op"><</span>()<span class="op">></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">"there is no T"</span>)<span class="question-mark">?</span>;
|
||
...
|
||
}</code></pre></div>
|
||
</div><div class="impl-items"><div id="method.context-1" class="method trait-impl has-srclink"><div class="rightside"><a class="srclink" href="../src/anyhow/context.rs.html#83-88" title="goto source code">[src]</a></div><a href="#method.context-1" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.context" class="fnname">context</a><C>(self, context: C) -> <a class="enum" href="https://doc.rust-lang.org/1.59.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>> <span class="where fmt-newline">where<br> C: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static, </span></h4></div><div id="method.with_context-1" class="method trait-impl has-srclink"><div class="rightside"><a class="srclink" href="../src/anyhow/context.rs.html#90-96" title="goto source code">[src]</a></div><a href="#method.with_context-1" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.with_context" class="fnname">with_context</a><C, F>(self, context: F) -> <a class="enum" href="https://doc.rust-lang.org/1.59.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>> <span class="where fmt-newline">where<br> C: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,<br> F: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> C, </span></h4></div></div></details><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div class="item-list" id="implementors-list"></div><script type="text/javascript" src="../implementors/anyhow/trait.Context.js" async></script></section><section id="search" class="content hidden"></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="anyhow" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.59.0 (9d1b2106e 2022-02-23)" ></div>
|
||
</body></html> |