🛈 Note: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem.

For the release documentation, please see docs.rs, instead.

tracing/
field.rs

1//! Structured data associated with `Span`s and `Event`s.
2pub use tracing_core::field::*;
3
4use crate::Metadata;
5
6/// Trait implemented to allow a type to be used as a field key.
7///
8/// <div class="example-wrap" style="display:inline-block">
9/// <pre class="ignore" style="white-space:normal;font:inherit;">
10///
11/// **Note**: Although this is implemented for both the [`Field`] type
12/// *and* any type that can be borrowed as an `&str`, only `Field` allows *O*(1) access.
13/// Indexing a field with a string results in an iterative search that performs
14/// string comparisons. Thus, if possible, once the key for a field is known, it
15/// should be used whenever possible.
16///
17/// </pre>
18/// </div>
19pub trait AsField: crate::sealed::Sealed {
20    /// Attempts to convert `&self` into a `Field` with the specified `metadata`.
21    ///
22    /// If `metadata` defines this field, then the field is returned. Otherwise,
23    /// this returns `None`.
24    fn as_field(&self, metadata: &Metadata<'_>) -> Option<Field>;
25}
26
27// ===== impl AsField =====
28
29impl AsField for Field {
30    #[inline]
31    fn as_field(&self, metadata: &Metadata<'_>) -> Option<Field> {
32        if self.callsite() == metadata.callsite() {
33            Some(self.clone())
34        } else {
35            None
36        }
37    }
38}
39
40impl AsField for &Field {
41    #[inline]
42    fn as_field(&self, metadata: &Metadata<'_>) -> Option<Field> {
43        if self.callsite() == metadata.callsite() {
44            Some((*self).clone())
45        } else {
46            None
47        }
48    }
49}
50
51impl AsField for str {
52    #[inline]
53    fn as_field(&self, metadata: &Metadata<'_>) -> Option<Field> {
54        metadata.fields().field(&self)
55    }
56}
57
58impl crate::sealed::Sealed for Field {}
59impl crate::sealed::Sealed for &Field {}
60impl crate::sealed::Sealed for str {}