Files
GopherGate/target/doc/anyhow/trait.Context.html
2026-02-26 12:00:21 -05:00

119 lines
20 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="Provides the `context` method for `Result`."><title>Context in anyhow - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="anyhow" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.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]--><rustdoc-topbar><h2><a href="#">Context</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../anyhow/index.html">anyhow</a><span class="version">1.0.102</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Context</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#example" title="Example">Example</a></li><li><a href="#effect-on-downcasting" title="Effect on downcasting">Effect on downcasting</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.context" title="context">context</a></li><li><a href="#tymethod.with_context" title="with_context">with_context</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-Context%3CT,+Infallible%3E-for-Option%3CT%3E" title="Option&#60;T&#62;">Option&#60;T&#62;</a></li><li><a href="#impl-Context%3CT,+E%3E-for-Result%3CT,+E%3E" title="Result&#60;T, E&#62;">Result&#60;T, E&#62;</a></li></ul><h3><a href="#dyn-compatibility">Dyn Compatibility</a></h3><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate anyhow</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="index.html">anyhow</a></div><h1>Trait <span class="trait">Context</span>&nbsp;<button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/anyhow/lib.rs.html#616-628">Source</a> </span></div><pre class="rust item-decl"><code>pub trait Context&lt;T, E&gt;: Sealed {
// Required methods
fn <a href="#tymethod.context" class="fn">context</a>&lt;C&gt;(self, context: C) -&gt; <a class="type" href="type.Result.html" title="type anyhow::Result">Result</a>&lt;T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>&gt;
<span class="where">where C: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static</span>;
<span class="item-spacer"></span> fn <a href="#tymethod.with_context" class="fn">with_context</a>&lt;C, F&gt;(self, f: F) -&gt; <a class="type" href="type.Result.html" title="type anyhow::Result">Result</a>&lt;T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>&gt;
<span class="where">where C: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -&gt; C</span>;
}</code></pre><details class="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"><a class="doc-anchor" href="#example">§</a>Example</h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>anyhow::{Context, <span class="prelude-ty">Result</span>};
<span class="kw">use </span>std::fs;
<span class="kw">use </span>std::path::PathBuf;
<span class="kw">pub struct </span>ImportantThing {
path: PathBuf,
}
<span class="kw">impl </span>ImportantThing {
<span class="kw">pub fn </span>detach(<span class="kw-2">&amp;mut </span><span class="self">self</span>) -&gt; <span class="prelude-ty">Result</span>&lt;()&gt; {...}
}
<span class="kw">pub fn </span>do_it(<span class="kw-2">mut </span>it: ImportantThing) -&gt; <span class="prelude-ty">Result</span>&lt;Vec&lt;u8&gt;&gt; {
it.detach().context(<span class="string">"Failed to detach the important thing"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>path = <span class="kw-2">&amp;</span>it.path;
<span class="kw">let </span>content = fs::read(path)
.with_context(|| <span class="macro">format!</span>(<span class="string">"Failed to read instrs from {}"</span>, path.display()))<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(content)
}</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" title="struct anyhow::Error">Display representations</a> documentation for other forms in
which this context chain can be rendered.</p>
<br>
<h2 id="effect-on-downcasting"><a class="doc-anchor" href="#effect-on-downcasting">§</a>Effect on downcasting</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, Anyhows 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>anyhow::{Context, <span class="prelude-ty">Result</span>};
<span class="kw">fn </span>do_it() -&gt; <span class="prelude-ty">Result</span>&lt;()&gt; {
helper().context(<span class="string">"Failed to complete the work"</span>)<span class="question-mark">?</span>;
...
}
<span class="kw">fn </span>main() {
<span class="kw">let </span>err = do_it().unwrap_err();
<span class="kw">if let </span><span class="prelude-val">Some</span>(e) = err.downcast_ref::&lt;SuspiciousError&gt;() {
<span class="comment">// If helper() returned SuspiciousError, this downcast will
// 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>anyhow::{Context, <span class="prelude-ty">Result</span>};
<span class="kw">fn </span>do_it() -&gt; <span class="prelude-ty">Result</span>&lt;()&gt; {
helper().context(HelperFailed)<span class="question-mark">?</span>;
...
}
<span class="kw">fn </span>main() {
<span class="kw">let </span>err = do_it().unwrap_err();
<span class="kw">if let </span><span class="prelude-val">Some</span>(e) = err.downcast_ref::&lt;HelperFailed&gt;() {
<span class="comment">// If helper failed, this downcast will succeed because
// HelperFailed is the context that has been attached to
// that error.
</span>}
}</code></pre></div></li>
</ul>
</div></details><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.context" class="method"><a class="src rightside" href="../src/anyhow/lib.rs.html#618-620">Source</a><h4 class="code-header">fn <a href="#tymethod.context" class="fn">context</a>&lt;C&gt;(self, context: C) -&gt; <a class="type" href="type.Result.html" title="type anyhow::Result">Result</a>&lt;T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>&gt;<div class="where">where
C: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></h4></section></summary><div class="docblock"><p>Wrap the error value with additional context.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="tymethod.with_context" class="method"><a class="src rightside" href="../src/anyhow/lib.rs.html#624-627">Source</a><h4 class="code-header">fn <a href="#tymethod.with_context" class="fn">with_context</a>&lt;C, F&gt;(self, f: F) -&gt; <a class="type" href="type.Result.html" title="type anyhow::Result">Result</a>&lt;T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>&gt;<div class="where">where
C: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -&gt; C,</div></h4></section></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="dyn-compatibility" class="section-header">Dyn Compatibility<a href="#dyn-compatibility" class="anchor">§</a></h2><div class="dyn-compatibility-info"><p>This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.93.1/reference/items/traits.html#dyn-compatibility">dyn compatible</a>.</p><p><i>In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.</i></p></div><h2 id="foreign-impls" class="section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor">§</a></h2><details class="toggle implementors-toggle"><summary><section id="impl-Context%3CT,+Infallible%3E-for-Option%3CT%3E" class="impl"><a class="src rightside" href="../src/anyhow/context.rs.html#90-113">Source</a><a href="#impl-Context%3CT,+Infallible%3E-for-Option%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.Context.html" title="trait anyhow::Context">Context</a>&lt;T, <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a>&gt; for <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;T&gt;</h3><div class="docblock">
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>anyhow::{Context, <span class="prelude-ty">Result</span>};
<span class="kw">fn </span>maybe_get() -&gt; <span class="prelude-ty">Option</span>&lt;T&gt; {
...
}
<span class="kw">fn </span>demo() -&gt; <span class="prelude-ty">Result</span>&lt;()&gt; {
<span class="kw">let </span>t = maybe_get().context(<span class="string">"there is no T"</span>)<span class="question-mark">?</span>;
...
}</code></pre></div></div></section></summary><div class="impl-items"><section id="method.context" class="method trait-impl"><a class="src rightside" href="../src/anyhow/context.rs.html#91-101">Source</a><a href="#method.context" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.context" class="fn">context</a>&lt;C&gt;(self, context: C) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>&gt;<div class="where">where
C: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></h4></section><section id="method.with_context" class="method trait-impl"><a class="src rightside" href="../src/anyhow/context.rs.html#103-112">Source</a><a href="#method.with_context" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.with_context" class="fn">with_context</a>&lt;C, F&gt;(self, context: F) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>&gt;<div class="where">where
C: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -&gt; C,</div></h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Context%3CT,+E%3E-for-Result%3CT,+E%3E" class="impl"><a class="src rightside" href="../src/anyhow/context.rs.html#42-68">Source</a><a href="#impl-Context%3CT,+E%3E-for-Result%3CT,+E%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T, E&gt; <a class="trait" href="trait.Context.html" title="trait anyhow::Context">Context</a>&lt;T, E&gt; for <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, E&gt;<div class="where">where
E: StdError + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></h3></section></summary><div class="impl-items"><section id="method.context-1" class="method trait-impl"><a class="src rightside" href="../src/anyhow/context.rs.html#46-56">Source</a><a href="#method.context-1" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.context" class="fn">context</a>&lt;C&gt;(self, context: C) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>&gt;<div class="where">where
C: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></h4></section><section id="method.with_context-1" class="method trait-impl"><a class="src rightside" href="../src/anyhow/context.rs.html#58-67">Source</a><a href="#method.with_context-1" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.with_context" class="fn">with_context</a>&lt;C, F&gt;(self, context: F) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, <a class="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a>&gt;<div class="where">where
C: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -&gt; C,</div></h4></section></div></details><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"></div><script src="../trait.impl/anyhow/trait.Context.js" data-ignore-extern-crates="core" async></script></section></div></main></body></html>