This repository has been archived on 2022-04-04. You can view files and clone it, but cannot push or open issues or pull requests.
ludum-dare-50/rustdoc/tracing/field/trait.Visit.html

91 lines
23 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="Visits typed values."><meta name="keywords" content="rust, rustlang, rust-lang, Visit"><title>Visit in tracing::field - 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">&#9776;</div><a class="sidebar-logo" href="../../tracing/index.html"><div class="logo-container"><img src="https:&#x2F;&#x2F;raw.githubusercontent.com&#x2F;tokio-rs&#x2F;tracing&#x2F;master&#x2F;assets&#x2F;logo-type.png" alt="logo"></div>
</a><h2 class="location">Trait Visit</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.record_debug">record_debug</a></div><h3 class="sidebar-title"><a href="#provided-methods">Provided Methods</a></h3><div class="sidebar-links"><a href="#method.record_bool">record_bool</a><a href="#method.record_error">record_error</a><a href="#method.record_f64">record_f64</a><a href="#method.record_i64">record_i64</a><a href="#method.record_str">record_str</a><a href="#method.record_u64">record_u64</a></div><h3 class="sidebar-title"><a href="#foreign-impls">Implementations on Foreign Types</a></h3><div class="sidebar-links"><a href="#impl-Visit-for-DebugMap%3C%27a%2C%20%27b%3E">DebugMap&lt;&#39;a, &#39;b&gt;</a><a href="#impl-Visit-for-DebugStruct%3C%27a%2C%20%27b%3E">DebugStruct&lt;&#39;a, &#39;b&gt;</a></div><h3 class="sidebar-title"><a href="#implementors">Implementors</a></h3></div><h2 class="location">Other items in<br><a href="../index.html">tracing</a>::<wbr><a href="index.html">field</a></h2><div id="sidebar-vars" data-name="Visit" 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="../../tracing/index.html"><img src="https:&#x2F;&#x2F;raw.githubusercontent.com&#x2F;tokio-rs&#x2F;tracing&#x2F;master&#x2F;assets&#x2F;logo-type.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">tracing</a>::<wbr><a href="index.html">field</a>::<wbr><a class="trait" href="#">Visit</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">&#x2212;</span>]</a></span><a class="srclink" href="../../src/tracing_core/field.rs.html#259-309" title="goto source code">[src]</a></span></h1><div class="docblock item-decl"><pre class="rust trait"><code>pub trait Visit {
fn <a href="#tymethod.record_debug" class="fnname">record_debug</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &amp;dyn <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>);
fn <a href="#method.record_f64" class="fnname">record_f64</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.f64.html">f64</a>) { ... }
<div class="item-spacer"></div> fn <a href="#method.record_i64" class="fnname">record_i64</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.i64.html">i64</a>) { ... }
<div class="item-spacer"></div> fn <a href="#method.record_u64" class="fnname">record_u64</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.u64.html">u64</a>) { ... }
<div class="item-spacer"></div> fn <a href="#method.record_bool" class="fnname">record_bool</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.bool.html">bool</a>) { ... }
<div class="item-spacer"></div> fn <a href="#method.record_str" class="fnname">record_str</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &amp;<a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.str.html">str</a>) { ... }
<div class="item-spacer"></div> fn <a href="#method.record_error" class="fnname">record_error</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &amp;(dyn <a class="trait" href="https://doc.rust-lang.org/1.59.0/std/error/trait.Error.html" title="trait std::error::Error">Error</a> + 'static)) { ... }
}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Visits typed values.</p>
<p>An instance of <code>Visit</code> (“a visitor”) represents the logic necessary to
record field values of various types. When an implementor of <a href="trait.Value.html"><code>Value</code></a> is
<a href="trait.Value.html#method.record">recorded</a>, it calls the appropriate method on the provided visitor to
indicate the type that value should be recorded as.</p>
<p>When a <a href="../subscriber/trait.Subscriber.html"><code>Subscriber</code></a> implementation <a href="../subscriber/trait.Subscriber.html#method.event">records an <code>Event</code></a> or a
<a href="../subscriber/trait.Subscriber.html#method.record">set of <code>Value</code>s added to a <code>Span</code></a>, it can pass an <code>&amp;mut Visit</code> to the
<code>record</code> method on the provided <a href="struct.ValueSet.html"><code>ValueSet</code></a> or <a href="../event/struct.Event.html"><code>Event</code></a>. This visitor
will then be used to record all the field-value pairs present on that
<code>Event</code> or <code>ValueSet</code>.</p>
<h2 id="examples" class="section-header"><a href="#examples">Examples</a></h2>
<p>A simple visitor that writes to a string might be implemented like so:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">std::fmt</span>::{<span class="self">self</span>, <span class="ident">Write</span>};
<span class="kw">use</span> <span class="ident">tracing::field</span>::{<span class="ident">Value</span>, <span class="ident">Visit</span>, <span class="ident">Field</span>};
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">StringVisitor</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span> {
<span class="ident">string</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;a</span> <span class="kw-2">mut</span> <span class="ident">String</span>,
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span> <span class="ident">Visit</span> <span class="kw">for</span> <span class="ident">StringVisitor</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span> {
<span class="kw">fn</span> <span class="ident">record_debug</span>(<span class="kw-2">&amp;mut</span> <span class="self">self</span>, <span class="ident">field</span>: <span class="kw-2">&amp;</span><span class="ident">Field</span>, <span class="ident">value</span>: <span class="kw-2">&amp;</span><span class="kw">dyn</span> <span class="ident">fmt::Debug</span>) {
<span class="macro">write!</span>(<span class="self">self</span>.<span class="ident">string</span>, <span class="string">&quot;{} = {:?}; &quot;</span>, <span class="ident">field</span>.<span class="ident">name</span>(), <span class="ident">value</span>).<span class="ident">unwrap</span>();
}
}</code></pre></div>
<p>This visitor will format each recorded value using <code>fmt::Debug</code>, and
append the field name and formatted value to the provided string,
regardless of the type of the recorded value. When all the values have
been recorded, the <code>StringVisitor</code> may be dropped, allowing the string
to be printed or stored in some other data structure.</p>
<p>The <code>Visit</code> trait provides default implementations for <code>record_i64</code>,
<code>record_u64</code>, <code>record_bool</code>, <code>record_str</code>, and <code>record_error</code>, which simply
forward the recorded value to <code>record_debug</code>. Thus, <code>record_debug</code> is the
only method which a <code>Visit</code> implementation <em>must</em> implement. However,
visitors may override the default implementations of these functions in
order to implement type-specific behavior.</p>
<p>Additionally, when a visitor receives a value of a type it does not care
about, it is free to ignore those values completely. For example, a
visitor which only records numeric data might look like this:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">SumVisitor</span> {
<span class="ident">sum</span>: <span class="ident">i64</span>,
}
<span class="kw">impl</span> <span class="ident">Visit</span> <span class="kw">for</span> <span class="ident">SumVisitor</span> {
<span class="kw">fn</span> <span class="ident">record_i64</span>(<span class="kw-2">&amp;mut</span> <span class="self">self</span>, <span class="ident">_field</span>: <span class="kw-2">&amp;</span><span class="ident">Field</span>, <span class="ident">value</span>: <span class="ident">i64</span>) {
<span class="self">self</span>.<span class="ident">sum</span> <span class="op">+</span><span class="op">=</span> <span class="ident">value</span>;
}
<span class="kw">fn</span> <span class="ident">record_u64</span>(<span class="kw-2">&amp;mut</span> <span class="self">self</span>, <span class="ident">_field</span>: <span class="kw-2">&amp;</span><span class="ident">Field</span>, <span class="ident">value</span>: <span class="ident">u64</span>) {
<span class="self">self</span>.<span class="ident">sum</span> <span class="op">+</span><span class="op">=</span> <span class="ident">value</span> <span class="kw">as</span> <span class="ident">i64</span>;
}
<span class="kw">fn</span> <span class="ident">record_debug</span>(<span class="kw-2">&amp;mut</span> <span class="self">self</span>, <span class="ident">_field</span>: <span class="kw-2">&amp;</span><span class="ident">Field</span>, <span class="ident">_value</span>: <span class="kw-2">&amp;</span><span class="ident">fmt::Debug</span>) {
<span class="comment">// Do nothing</span>
}
}</code></pre></div>
<p>This visitor (which is probably not particularly useful) keeps a running
sum of all the numeric values it records, and ignores all other values. A
more practical example of recording typed values is presented in
<code>examples/counters.rs</code>, which demonstrates a very simple metrics system
implemented using <code>tracing</code>.</p>
<div class="example-wrap" style="display:inline-block">
<pre class="ignore" style="white-space:normal;font:inherit;">
<strong>Note</strong>: The <code>record_error</code> trait method is only
available when the Rust standard library is present, as it requires the
<code>std::error::Error</code> trait.
</pre></div>
</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.record_debug" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#308" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#tymethod.record_debug" class="fnname">record_debug</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &amp;dyn <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>)</h4></div></summary><div class="docblock"><p>Visit a value implementing <code>fmt::Debug</code>.</p>
</div></details></div><h2 id="provided-methods" class="small-section-header">Provided methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle" open><summary><div id="method.record_f64" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#270" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#method.record_f64" class="fnname">record_f64</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.f64.html">f64</a>)</h4></div></summary><div class="docblock"><p>Visit a double-precision floating point value.</p>
</div></details><details class="rustdoc-toggle" open><summary><div id="method.record_i64" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#275" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#method.record_i64" class="fnname">record_i64</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.i64.html">i64</a>)</h4></div></summary><div class="docblock"><p>Visit a signed 64-bit integer value.</p>
</div></details><details class="rustdoc-toggle" open><summary><div id="method.record_u64" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#280" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#method.record_u64" class="fnname">record_u64</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.u64.html">u64</a>)</h4></div></summary><div class="docblock"><p>Visit an unsigned 64-bit integer value.</p>
</div></details><details class="rustdoc-toggle" open><summary><div id="method.record_bool" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#285" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#method.record_bool" class="fnname">record_bool</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.bool.html">bool</a>)</h4></div></summary><div class="docblock"><p>Visit a boolean value.</p>
</div></details><details class="rustdoc-toggle" open><summary><div id="method.record_str" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#290" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#method.record_str" class="fnname">record_str</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &amp;<a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.str.html">str</a>)</h4></div></summary><div class="docblock"><p>Visit a string value.</p>
</div></details><details class="rustdoc-toggle" open><summary><div id="method.record_error" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#303" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#method.record_error" class="fnname">record_error</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &amp;(dyn <a class="trait" href="https://doc.rust-lang.org/1.59.0/std/error/trait.Error.html" title="trait std::error::Error">Error</a> + 'static))</h4></div></summary><div class="docblock"><p>Records a type implementing <code>Error</code>.</p>
<div class="example-wrap" style="display:inline-block">
<pre class="ignore" style="white-space:normal;font:inherit;">
<strong>Note</strong>: This is only enabled when the Rust standard library is
present.
</pre></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-Visit-for-DebugMap%3C%27a%2C%20%27b%3E" class="impl has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#373-377" title="goto source code">[src]</a></div><a href="#impl-Visit-for-DebugMap%3C%27a%2C%20%27b%3E" class="anchor"></a><h3 class="code-header in-band">impl&lt;'a, 'b&gt; <a class="trait" href="trait.Visit.html" title="trait tracing::field::Visit">Visit</a> for <a class="struct" href="https://doc.rust-lang.org/1.59.0/core/fmt/builders/struct.DebugMap.html" title="struct core::fmt::builders::DebugMap">DebugMap</a>&lt;'a, 'b&gt;</h3></div></summary><div class="impl-items"><div id="method.record_debug" class="method trait-impl has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#374" title="goto source code">[src]</a></div><a href="#method.record_debug" class="anchor"></a><h4 class="code-header">pub fn <a href="#tymethod.record_debug" class="fnname">record_debug</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &amp;dyn <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>)</h4></div></div></details><details class="rustdoc-toggle implementors-toggle"><summary><div id="impl-Visit-for-DebugStruct%3C%27a%2C%20%27b%3E" class="impl has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#367-371" title="goto source code">[src]</a></div><a href="#impl-Visit-for-DebugStruct%3C%27a%2C%20%27b%3E" class="anchor"></a><h3 class="code-header in-band">impl&lt;'a, 'b&gt; <a class="trait" href="trait.Visit.html" title="trait tracing::field::Visit">Visit</a> for <a class="struct" href="https://doc.rust-lang.org/1.59.0/core/fmt/builders/struct.DebugStruct.html" title="struct core::fmt::builders::DebugStruct">DebugStruct</a>&lt;'a, 'b&gt;</h3></div></summary><div class="impl-items"><div id="method.record_debug-1" class="method trait-impl has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#368" title="goto source code">[src]</a></div><a href="#method.record_debug-1" class="anchor"></a><h4 class="code-header">pub fn <a href="#tymethod.record_debug" class="fnname">record_debug</a>(&amp;mut self, field: &amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &amp;dyn <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>)</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 id="impl-Visit" class="impl has-srclink"><div class="rightside"><a class="srclink" href="../../src/tracing_core/field.rs.html#379-386" title="goto source code">[src]</a></div><a href="#impl-Visit" class="anchor"></a><h3 class="code-header in-band">impl&lt;F&gt; <a class="trait" href="trait.Visit.html" title="trait tracing::field::Visit">Visit</a> for F <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&amp;<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, &amp;dyn <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>),&nbsp;</span></h3></div></div><script type="text/javascript" src="../../implementors/tracing_core/field/trait.Visit.js" async></script></section><section id="search" class="content hidden"></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="tracing" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.59.0 (9d1b2106e 2022-02-23)" ></div>
</body></html>