Files
rapr-rs/docs/tracing_core/field/trait.Visit.html
T
2021-03-14 20:06:11 +00:00

97 lines
21 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 `Visit` trait in crate `tracing_core`."><meta name="keywords" content="rust, rustlang, rust-lang, Visit"><title>tracing_core::field::Visit - 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="../../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled ><script id="default-settings"></script><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../../favicon.svg">
<link rel="alternate icon" type="image/png" href="../../favicon-16x16.png">
<link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><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='../../tracing_core/index.html'><div class='logo-container'><img src='https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png' alt='logo'></div></a><p class="location">Trait Visit</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.record_debug">record_debug</a></div><a class="sidebar-title" href="#provided-methods">Provided Methods</a><div class="sidebar-links"><a href="#method.record_bool">record_bool</a><a href="#method.record_error">record_error</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><a class="sidebar-title" href="#foreign-impls">Implementations on Foreign Types</a><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><a class="sidebar-title" href="#implementors">Implementors</a></div><p class="location"><a href="../index.html">tracing_core</a>::<wbr><a href="index.html">field</a></p><script>window.sidebarCurrent = {name: "Visit", 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!" aria-haspopup="menu"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></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><button type="button" class="help-button">?</button>
<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/tracing_core/field.rs.html#189-228" title="goto source code">[src]</a></span><span class="in-band">Trait <a href="../index.html">tracing_core</a>::<wbr><a href="index.html">field</a>::<wbr><a class="trait" href="">Visit</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class="rust trait">pub trait Visit {
fn <a href="#tymethod.record_debug" class="fnname">record_debug</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: &amp;dyn <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>);
fn <a href="#method.record_i64" class="fnname">record_i64</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/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="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/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="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/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="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: &amp;<a class="primitive" href="https://doc.rust-lang.org/nightly/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="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: &amp;(dyn <a class="trait" href="https://doc.rust-lang.org/nightly/std/error/trait.Error.html" title="trait std::error::Error">Error</a> + 'static)) { ... }
}</pre></div><div class="docblock"><p>Visits typed values.</p>
<p>An instance of <code>Visit</code> (&quot;a visitor&quot;) 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>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<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">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fmt</span>::{<span class="self">self</span>, <span class="ident">Write</span>};
<span class="kw">use</span> <span class="ident">tracing</span>::<span class="ident">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;</span><span class="kw-2">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</span>::<span class="ident">Debug</span>) {
<span class="macro">write</span><span class="macro">!</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>();
}
}</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">
<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;</span><span class="kw-2">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;</span><span class="kw-2">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;</span><span class="kw-2">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</span>::<span class="ident">Debug</span>) {
<span class="comment">// Do nothing</span>
}
}</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="information">
<div class="tooltip ignore" style=""><span class="tooltiptext">Note</span></div>
</div>
<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><h2 id="required-methods" class="small-section-header">Required methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><h3 id="tymethod.record_debug" class="method"><code>fn <a href="#tymethod.record_debug" class="fnname">record_debug</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: &amp;dyn <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>)</code><a class="srclink" href="../../src/tracing_core/field.rs.html#227" title="goto source code">[src]</a></h3><div class="docblock"><p>Visit a value implementing <code>fmt::Debug</code>.</p>
</div></div><span class="loading-content">Loading content...</span><h2 id="provided-methods" class="small-section-header">Provided methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><h3 id="method.record_i64" class="method"><code>fn <a href="#method.record_i64" class="fnname">record_i64</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i64.html">i64</a>)</code><a class="srclink" href="../../src/tracing_core/field.rs.html#191-193" title="goto source code">[src]</a></h3><div class="docblock"><p>Visit a signed 64-bit integer value.</p>
</div><h3 id="method.record_u64" class="method"><code>fn <a href="#method.record_u64" class="fnname">record_u64</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a>)</code><a class="srclink" href="../../src/tracing_core/field.rs.html#196-198" title="goto source code">[src]</a></h3><div class="docblock"><p>Visit an unsigned 64-bit integer value.</p>
</div><h3 id="method.record_bool" class="method"><code>fn <a href="#method.record_bool" class="fnname">record_bool</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>)</code><a class="srclink" href="../../src/tracing_core/field.rs.html#201-203" title="goto source code">[src]</a></h3><div class="docblock"><p>Visit a boolean value.</p>
</div><h3 id="method.record_str" class="method"><code>fn <a href="#method.record_str" class="fnname">record_str</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: &amp;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.str.html">str</a>)</code><a class="srclink" href="../../src/tracing_core/field.rs.html#206-208" title="goto source code">[src]</a></h3><div class="docblock"><p>Visit a string value.</p>
</div><h3 id="method.record_error" class="method"><code>fn <a href="#method.record_error" class="fnname">record_error</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: &amp;(dyn <a class="trait" href="https://doc.rust-lang.org/nightly/std/error/trait.Error.html" title="trait std::error::Error">Error</a> + 'static))</code><a class="srclink" href="../../src/tracing_core/field.rs.html#222-224" title="goto source code">[src]</a></h3><div class="docblock"><p>Records a type implementing <code>Error</code>.</p>
<div class="information">
<div class="tooltip ignore" style=""><span class="tooltiptext">Note</span></div>
</div>
<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></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-Visit-for-DebugStruct%3C%27a%2C%20%27b%3E" class="impl"><code class="in-band">impl&lt;'a, 'b&gt; <a class="trait" href="../../tracing_core/field/trait.Visit.html" title="trait tracing_core::field::Visit">Visit</a> for <a class="struct" href="https://doc.rust-lang.org/nightly/core/fmt/builders/struct.DebugStruct.html" title="struct core::fmt::builders::DebugStruct">DebugStruct</a>&lt;'a, 'b&gt;</code><a href="#impl-Visit-for-DebugStruct%3C%27a%2C%20%27b%3E" class="anchor"></a><a class="srclink" href="../../src/tracing_core/field.rs.html#273-277" title="goto source code">[src]</a></h3><div class="impl-items"><h4 id="method.record_debug" class="method hidden"><code>fn <a href="#method.record_debug" class="fnname">record_debug</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: &amp;dyn <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>)</code><a class="srclink" href="../../src/tracing_core/field.rs.html#274-276" title="goto source code">[src]</a></h4></div><h3 id="impl-Visit-for-DebugMap%3C%27a%2C%20%27b%3E" class="impl"><code class="in-band">impl&lt;'a, 'b&gt; <a class="trait" href="../../tracing_core/field/trait.Visit.html" title="trait tracing_core::field::Visit">Visit</a> for <a class="struct" href="https://doc.rust-lang.org/nightly/core/fmt/builders/struct.DebugMap.html" title="struct core::fmt::builders::DebugMap">DebugMap</a>&lt;'a, 'b&gt;</code><a href="#impl-Visit-for-DebugMap%3C%27a%2C%20%27b%3E" class="anchor"></a><a class="srclink" href="../../src/tracing_core/field.rs.html#279-283" title="goto source code">[src]</a></h3><div class="impl-items"><h4 id="method.record_debug-1" class="method hidden"><code>fn <a href="#method.record_debug" class="fnname">record_debug</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: &amp;dyn <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>)</code><a class="srclink" href="../../src/tracing_core/field.rs.html#280-282" 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"><h3 id="impl-Visit" class="impl"><code class="in-band">impl&lt;F&gt; Visit for F <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(&amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, &amp;dyn <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>),&nbsp;</span></code><a href="#impl-Visit" class="anchor"></a><a class="srclink" href="../../src/tracing_core/field.rs.html#285-292" title="goto source code">[src]</a></h3><div class="impl-items"><h4 id="method.record_debug-2" class="method hidden"><code>fn <a href="#method.record_debug-2" class="fnname">record_debug</a>(&amp;mut self, field: &amp;<a class="struct" href="../../tracing_core/struct.Field.html" title="struct tracing_core::Field">Field</a>, value: &amp;dyn <a class="trait" href="https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>)</code><a class="srclink" href="../../src/tracing_core/field.rs.html#289-291" title="goto source code">[src]</a></h4></div></div><span class="loading-content">Loading content...</span><script type="text/javascript" src="../../implementors/tracing_core/field/trait.Visit.js" async></script></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../";window.currentCrate = "tracing_core";</script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>