| <!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" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.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="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../tracing/index.html"><div class="logo-container"><img src="https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../tracing/index.html"><div class="logo-container"> |
| <img src="https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png" alt="logo"></div></a><h2 class="location"><a href="#">Visit</a></h2><div class="sidebar-elems"><section><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.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">record_bool</a></li><li><a href="#method.record_error">record_error</a></li><li><a href="#method.record_f64">record_f64</a></li><li><a href="#method.record_i128">record_i128</a></li><li><a href="#method.record_i64">record_i64</a></li><li><a href="#method.record_str">record_str</a></li><li><a href="#method.record_u128">record_u128</a></li><li><a href="#method.record_u64">record_u64</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%27a%2C%20%27b%3E">DebugMap<'a, 'b></a></li><li><a href="#impl-Visit-for-DebugStruct%3C%27a%2C%20%27b%3E">DebugStruct<'a, 'b></a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In tracing::field</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">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></h1><span class="out-of-band"><a class="srclink" href="../../src/tracing_core/field.rs.html#266">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub trait Visit { |
| fn <a href="#tymethod.record_debug" class="fnname">record_debug</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &dyn Debug); |
| |
| fn <a href="#method.record_f64" class="fnname">record_f64</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: f64) { ... } |
| <span class="item-spacer"></span> fn <a href="#method.record_i64" class="fnname">record_i64</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: i64) { ... } |
| <span class="item-spacer"></span> fn <a href="#method.record_u64" class="fnname">record_u64</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: u64) { ... } |
| <span class="item-spacer"></span> fn <a href="#method.record_i128" class="fnname">record_i128</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: i128) { ... } |
| <span class="item-spacer"></span> fn <a href="#method.record_u128" class="fnname">record_u128</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: u128) { ... } |
| <span class="item-spacer"></span> fn <a href="#method.record_bool" class="fnname">record_bool</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: bool) { ... } |
| <span class="item-spacer"></span> fn <a href="#method.record_str" class="fnname">record_str</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &str) { ... } |
| <span class="item-spacer"></span> fn <a href="#method.record_error" class="fnname">record_error</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &(dyn Error + '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" title="Value"><code>Value</code></a> is |
| <a href="../trait.Value.html#tymethod.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"><code>Subscriber</code></a> implementation <a href="../trait.Subscriber.html#tymethod.event">records an <code>Event</code></a> or a |
| <a href="../trait.Subscriber.html#tymethod.record">set of <code>Value</code>s added to a <code>Span</code></a>, it can pass an <code>&mut Visit</code> to the |
| <code>record</code> method on the provided <a href="struct.ValueSet.html" title="ValueSet"><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"><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>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<<span class="lifetime">'a</span>> { |
| string: <span class="kw-2">&</span><span class="lifetime">'a </span><span class="kw-2">mut </span>String, |
| } |
| |
| <span class="kw">impl</span><<span class="lifetime">'a</span>> Visit <span class="kw">for </span>StringVisitor<<span class="lifetime">'a</span>> { |
| <span class="kw">fn </span>record_debug(<span class="kw-2">&mut </span><span class="self">self</span>, field: <span class="kw-2">&</span>Field, value: <span class="kw-2">&</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">&mut </span><span class="self">self</span>, _field: <span class="kw-2">&</span>Field, value: i64) { |
| <span class="self">self</span>.sum += value; |
| } |
| |
| <span class="kw">fn </span>record_u64(<span class="kw-2">&mut </span><span class="self">self</span>, _field: <span class="kw-2">&</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">&mut </span><span class="self">self</span>, _field: <span class="kw-2">&</span>Field, _value: <span class="kw-2">&</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="small-section-header">Required Methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="tymethod.record_debug" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#326">source</a><h4 class="code-header">fn <a href="#tymethod.record_debug" class="fnname">record_debug</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &dyn Debug)</h4></section></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 method-toggle" open><summary><section id="method.record_f64" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#277">source</a><h4 class="code-header">fn <a href="#method.record_f64" class="fnname">record_f64</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: f64)</h4></section></summary><div class="docblock"><p>Visit a double-precision floating point value.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.record_i64" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#282">source</a><h4 class="code-header">fn <a href="#method.record_i64" class="fnname">record_i64</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: i64)</h4></section></summary><div class="docblock"><p>Visit a signed 64-bit integer value.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.record_u64" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#287">source</a><h4 class="code-header">fn <a href="#method.record_u64" class="fnname">record_u64</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: u64)</h4></section></summary><div class="docblock"><p>Visit an unsigned 64-bit integer value.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.record_i128" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#292">source</a><h4 class="code-header">fn <a href="#method.record_i128" class="fnname">record_i128</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: i128)</h4></section></summary><div class="docblock"><p>Visit a signed 128-bit integer value.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.record_u128" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#297">source</a><h4 class="code-header">fn <a href="#method.record_u128" class="fnname">record_u128</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: u128)</h4></section></summary><div class="docblock"><p>Visit an unsigned 128-bit integer value.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.record_bool" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#302">source</a><h4 class="code-header">fn <a href="#method.record_bool" class="fnname">record_bool</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: bool)</h4></section></summary><div class="docblock"><p>Visit a boolean value.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.record_str" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#307">source</a><h4 class="code-header">fn <a href="#method.record_str" class="fnname">record_str</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &str)</h4></section></summary><div class="docblock"><p>Visit a string value.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.record_error" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#321">source</a><h4 class="code-header">fn <a href="#method.record_error" class="fnname">record_error</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &(dyn Error + '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="small-section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor"></a></h2><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Visit-for-DebugStruct%3C%27a%2C%20%27b%3E" class="impl has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#385">source</a><a href="#impl-Visit-for-DebugStruct%3C%27a%2C%20%27b%3E" class="anchor"></a><h3 class="code-header">impl<'a, 'b> <a class="trait" href="trait.Visit.html" title="trait tracing::field::Visit">Visit</a> for DebugStruct<'a, 'b></h3></section></summary><div class="impl-items"><section id="method.record_debug" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#386">source</a><a href="#method.record_debug" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.record_debug" class="fnname">record_debug</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &dyn Debug)</h4></section></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Visit-for-DebugMap%3C%27a%2C%20%27b%3E" class="impl has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#391">source</a><a href="#impl-Visit-for-DebugMap%3C%27a%2C%20%27b%3E" class="anchor"></a><h3 class="code-header">impl<'a, 'b> <a class="trait" href="trait.Visit.html" title="trait tracing::field::Visit">Visit</a> for DebugMap<'a, 'b></h3></section></summary><div class="impl-items"><section id="method.record_debug-1" class="method trait-impl has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#392">source</a><a href="#method.record_debug-1" class="anchor"></a><h4 class="code-header">fn <a href="#tymethod.record_debug" class="fnname">record_debug</a>(&mut self, field: &<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, value: &dyn Debug)</h4></section></div></details><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"><section id="impl-Visit-for-F" class="impl has-srclink"><a class="srclink rightside" href="../../src/tracing_core/field.rs.html#397">source</a><a href="#impl-Visit-for-F" class="anchor"></a><h3 class="code-header">impl<F> <a class="trait" href="trait.Visit.html" title="trait tracing::field::Visit">Visit</a> for F<span class="where fmt-newline">where<br> F: FnMut(&<a class="struct" href="struct.Field.html" title="struct tracing::field::Field">Field</a>, &dyn Debug),</span></h3></section></div><script src="../../implementors/tracing_core/field/trait.Visit.js" data-ignore-extern-crates="core" async></script></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.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |