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