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