Files
GopherGate/target/doc/tracing/field/trait.Visit.html
2026-02-26 12:00:21 -05:00

98 lines
23 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="Visits typed values."><title>Visit in tracing::field - 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="tracing" 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="icon" href="https://raw.githubusercontent.com/tokio-rs/tracing/main/assets/favicon.ico"></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="#">Visit</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../../tracing/index.html"><img src="https://raw.githubusercontent.com/tokio-rs/tracing/main/assets/logo-type.png" alt="logo"></a><h2><a href="../../tracing/index.html">tracing</a><span class="version">0.1.44</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Visit</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.record_debug" title="record_debug">record_debug</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.record_bool" title="record_bool">record_bool</a></li><li><a href="#method.record_bytes" title="record_bytes">record_bytes</a></li><li><a href="#method.record_error" title="record_error">record_error</a></li><li><a href="#method.record_f64" title="record_f64">record_f64</a></li><li><a href="#method.record_i64" title="record_i64">record_i64</a></li><li><a href="#method.record_i128" title="record_i128">record_i128</a></li><li><a href="#method.record_str" title="record_str">record_str</a></li><li><a href="#method.record_u64" title="record_u64">record_u64</a></li><li><a href="#method.record_u128" title="record_u128">record_u128</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-Visit-for-DebugMap%3C'_,+'_%3E" title="DebugMap&#60;&#39;_, &#39;_&#62;">DebugMap&#60;&#39;_, &#39;_&#62;</a></li><li><a href="#impl-Visit-for-DebugStruct%3C'_,+'_%3E" title="DebugStruct&#60;&#39;_, &#39;_&#62;">DebugStruct&#60;&#39;_, &#39;_&#62;</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In tracing::<wbr>field</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">tracing</a>::<wbr><a href="index.html">field</a></div><h1>Trait <span class="trait">Visit</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/tracing_core/field.rs.html#275">Source</a> </span></div><pre class="rust item-decl"><code>pub trait Visit {
// Required method
fn <a href="#tymethod.record_debug" class="fn">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.93.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>);
// Provided methods
fn <a href="#method.record_f64" class="fn">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.93.1/std/primitive.f64.html">f64</a>) { ... }
<span class="item-spacer"></span> fn <a href="#method.record_i64" class="fn">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.93.1/std/primitive.i64.html">i64</a>) { ... }
<span class="item-spacer"></span> fn <a href="#method.record_u64" class="fn">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.93.1/std/primitive.u64.html">u64</a>) { ... }
<span class="item-spacer"></span> fn <a href="#method.record_i128" class="fn">record_i128</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.93.1/std/primitive.i128.html">i128</a>) { ... }
<span class="item-spacer"></span> fn <a href="#method.record_u128" class="fn">record_u128</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.93.1/std/primitive.u128.html">u128</a>) { ... }
<span class="item-spacer"></span> fn <a href="#method.record_bool" class="fn">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.93.1/std/primitive.bool.html">bool</a>) { ... }
<span class="item-spacer"></span> fn <a href="#method.record_str" class="fn">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.93.1/std/primitive.str.html">str</a>) { ... }
<span class="item-spacer"></span> fn <a href="#method.record_bytes" class="fn">record_bytes</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.93.1/std/primitive.u8.html">u8</a>]) { ... }
<span class="item-spacer"></span> fn <a href="#method.record_error" class="fn">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.93.1/core/error/trait.Error.html" title="trait core::error::Error">Error</a> + 'static)) { ... }
}</code></pre><details class="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" title="trait tracing::Value"><code>Value</code></a> is
<a href="../trait.Value.html#tymethod.record" title="method tracing::Value::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="../trait.Subscriber.html" title="trait tracing::Subscriber"><code>Subscriber</code></a> implementation <a href="../trait.Subscriber.html#tymethod.event" title="method tracing::Subscriber::event">records an <code>Event</code></a> or a
<a href="../trait.Subscriber.html#tymethod.record" title="method tracing::Subscriber::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" title="struct tracing::field::ValueSet"><code>ValueSet</code></a> or <a href="../struct.Event.html" title="struct tracing::Event"><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"><a class="doc-anchor" href="#examples">§</a>Examples</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>std::fmt::{<span class="self">self</span>, Write};
<span class="kw">use </span>tracing::field::{Value, Visit, Field};
<span class="kw">pub struct </span>StringVisitor&lt;<span class="lifetime">'a</span>&gt; {
string: <span class="kw-2">&amp;</span><span class="lifetime">'a </span><span class="kw-2">mut </span>String,
}
<span class="kw">impl</span>&lt;<span class="lifetime">'a</span>&gt; Visit <span class="kw">for </span>StringVisitor&lt;<span class="lifetime">'a</span>&gt; {
<span class="kw">fn </span>record_debug(<span class="kw-2">&amp;mut </span><span class="self">self</span>, field: <span class="kw-2">&amp;</span>Field, value: <span class="kw-2">&amp;</span><span class="kw">dyn </span>fmt::Debug) {
<span class="macro">write!</span>(<span class="self">self</span>.string, <span class="string">"{} = {:?}; "</span>, field.name(), value).unwrap();
}
}</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 struct </span>SumVisitor {
sum: i64,
}
<span class="kw">impl </span>Visit <span class="kw">for </span>SumVisitor {
<span class="kw">fn </span>record_i64(<span class="kw-2">&amp;mut </span><span class="self">self</span>, _field: <span class="kw-2">&amp;</span>Field, value: i64) {
<span class="self">self</span>.sum += value;
}
<span class="kw">fn </span>record_u64(<span class="kw-2">&amp;mut </span><span class="self">self</span>, _field: <span class="kw-2">&amp;</span>Field, value: u64) {
<span class="self">self</span>.sum += value <span class="kw">as </span>i64;
}
<span class="kw">fn </span>record_debug(<span class="kw-2">&amp;mut </span><span class="self">self</span>, _field: <span class="kw-2">&amp;</span>Field, _value: <span class="kw-2">&amp;</span><span class="kw">dyn </span>fmt::Debug) {
<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="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.record_debug" class="method"><a class="src rightside" href="../../src/tracing_core/field.rs.html#340">Source</a><h4 class="code-header">fn <a href="#tymethod.record_debug" class="fn">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.93.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>)</h4></section></summary><div class="docblock"><p>Visit a value implementing <code>fmt::Debug</code>.</p>
</div></details></div><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.record_f64" class="method"><a class="src rightside" href="../../src/tracing_core/field.rs.html#286">Source</a><h4 class="code-header">fn <a href="#method.record_f64" class="fn">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.93.1/std/primitive.f64.html">f64</a>)</h4></section></summary><div class="docblock"><p>Visit a double-precision floating point value.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.record_i64" class="method"><a class="src rightside" href="../../src/tracing_core/field.rs.html#291">Source</a><h4 class="code-header">fn <a href="#method.record_i64" class="fn">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.93.1/std/primitive.i64.html">i64</a>)</h4></section></summary><div class="docblock"><p>Visit a signed 64-bit integer value.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.record_u64" class="method"><a class="src rightside" href="../../src/tracing_core/field.rs.html#296">Source</a><h4 class="code-header">fn <a href="#method.record_u64" class="fn">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.93.1/std/primitive.u64.html">u64</a>)</h4></section></summary><div class="docblock"><p>Visit an unsigned 64-bit integer value.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.record_i128" class="method"><a class="src rightside" href="../../src/tracing_core/field.rs.html#301">Source</a><h4 class="code-header">fn <a href="#method.record_i128" class="fn">record_i128</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.93.1/std/primitive.i128.html">i128</a>)</h4></section></summary><div class="docblock"><p>Visit a signed 128-bit integer value.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.record_u128" class="method"><a class="src rightside" href="../../src/tracing_core/field.rs.html#306">Source</a><h4 class="code-header">fn <a href="#method.record_u128" class="fn">record_u128</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.93.1/std/primitive.u128.html">u128</a>)</h4></section></summary><div class="docblock"><p>Visit an unsigned 128-bit integer value.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.record_bool" class="method"><a class="src rightside" href="../../src/tracing_core/field.rs.html#311">Source</a><h4 class="code-header">fn <a href="#method.record_bool" class="fn">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.93.1/std/primitive.bool.html">bool</a>)</h4></section></summary><div class="docblock"><p>Visit a boolean value.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.record_str" class="method"><a class="src rightside" href="../../src/tracing_core/field.rs.html#316">Source</a><h4 class="code-header">fn <a href="#method.record_str" class="fn">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.93.1/std/primitive.str.html">str</a>)</h4></section></summary><div class="docblock"><p>Visit a string value.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.record_bytes" class="method"><a class="src rightside" href="../../src/tracing_core/field.rs.html#321">Source</a><h4 class="code-header">fn <a href="#method.record_bytes" class="fn">record_bytes</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.93.1/std/primitive.u8.html">u8</a>])</h4></section></summary><div class="docblock"><p>Visit a byte slice.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.record_error" class="method"><a class="src rightside" href="../../src/tracing_core/field.rs.html#335">Source</a><h4 class="code-header">fn <a href="#method.record_error" class="fn">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.93.1/core/error/trait.Error.html" title="trait core::error::Error">Error</a> + 'static))</h4></section></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></div></details></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-Visit-for-DebugMap%3C'_,+'_%3E" class="impl"><a class="src rightside" href="../../src/tracing_core/field.rs.html#425">Source</a><a href="#impl-Visit-for-DebugMap%3C'_,+'_%3E" class="anchor">§</a><h3 class="code-header">impl <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.93.1/core/fmt/builders/struct.DebugMap.html" title="struct core::fmt::builders::DebugMap">DebugMap</a>&lt;'_, '_&gt;</h3></section></summary><div class="impl-items"><section id="method.record_debug" class="method trait-impl"><a class="src rightside" href="../../src/tracing_core/field.rs.html#426">Source</a><a href="#method.record_debug" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.record_debug" class="fn">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.93.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>)</h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Visit-for-DebugStruct%3C'_,+'_%3E" class="impl"><a class="src rightside" href="../../src/tracing_core/field.rs.html#419">Source</a><a href="#impl-Visit-for-DebugStruct%3C'_,+'_%3E" class="anchor">§</a><h3 class="code-header">impl <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.93.1/core/fmt/builders/struct.DebugStruct.html" title="struct core::fmt::builders::DebugStruct">DebugStruct</a>&lt;'_, '_&gt;</h3></section></summary><div class="impl-items"><section id="method.record_debug-1" class="method trait-impl"><a class="src rightside" href="../../src/tracing_core/field.rs.html#420">Source</a><a href="#method.record_debug-1" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.record_debug" class="fn">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.93.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>)</h4></section></div></details><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-Visit-for-F" class="impl"><a class="src rightside" href="../../src/tracing_core/field.rs.html#431-433">Source</a><a href="#impl-Visit-for-F" class="anchor">§</a><h3 class="code-header">impl&lt;F&gt; <a class="trait" href="trait.Visit.html" title="trait tracing::field::Visit">Visit</a> for F<div class="where">where
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/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.93.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a>),</div></h3></section></div><script src="../../trait.impl/tracing_core/field/trait.Visit.js" data-ignore-extern-crates="core" async></script></section></div></main></body></html>