/rust/registry/src/index.crates.io-6f17d22bba15001f/tracing-subscriber-0.3.19/src/field/debug.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! `MakeVisitor` wrappers for working with `fmt::Debug` fields. |
2 | | use super::{MakeVisitor, VisitFmt, VisitOutput}; |
3 | | use tracing_core::field::{Field, Visit}; |
4 | | |
5 | | use core::fmt; |
6 | | |
7 | | /// A visitor wrapper that ensures any `fmt::Debug` fields are formatted using |
8 | | /// the alternate (`:#`) formatter. |
9 | | #[derive(Debug, Clone)] |
10 | | pub struct Alt<V>(V); |
11 | | |
12 | | // TODO(eliza): When `error` as a primitive type is stable, add a |
13 | | // `DisplayErrors` wrapper... |
14 | | |
15 | | // === impl Alt === |
16 | | // |
17 | | impl<V> Alt<V> { |
18 | | /// Wraps the provided visitor so that any `fmt::Debug` fields are formatted |
19 | | /// using the alternative (`:#`) formatter. |
20 | 0 | pub fn new(inner: V) -> Self { |
21 | 0 | Alt(inner) |
22 | 0 | } |
23 | | } |
24 | | |
25 | | impl<T, V> MakeVisitor<T> for Alt<V> |
26 | | where |
27 | | V: MakeVisitor<T>, |
28 | | { |
29 | | type Visitor = Alt<V::Visitor>; |
30 | | |
31 | | #[inline] |
32 | 0 | fn make_visitor(&self, target: T) -> Self::Visitor { |
33 | 0 | Alt(self.0.make_visitor(target)) |
34 | 0 | } |
35 | | } |
36 | | |
37 | | impl<V> Visit for Alt<V> |
38 | | where |
39 | | V: Visit, |
40 | | { |
41 | | #[inline] |
42 | 0 | fn record_f64(&mut self, field: &Field, value: f64) { |
43 | 0 | self.0.record_f64(field, value) |
44 | 0 | } |
45 | | |
46 | | #[inline] |
47 | 0 | fn record_i64(&mut self, field: &Field, value: i64) { |
48 | 0 | self.0.record_i64(field, value) |
49 | 0 | } |
50 | | |
51 | | #[inline] |
52 | 0 | fn record_u64(&mut self, field: &Field, value: u64) { |
53 | 0 | self.0.record_u64(field, value) |
54 | 0 | } |
55 | | |
56 | | #[inline] |
57 | 0 | fn record_bool(&mut self, field: &Field, value: bool) { |
58 | 0 | self.0.record_bool(field, value) |
59 | 0 | } |
60 | | |
61 | | /// Visit a string value. |
62 | 0 | fn record_str(&mut self, field: &Field, value: &str) { |
63 | 0 | self.0.record_str(field, value) |
64 | 0 | } |
65 | | |
66 | | // TODO(eliza): add RecordError when stable |
67 | | // fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) { |
68 | | // self.record_debug(field, &format_args!("{}", value)) |
69 | | // } |
70 | | |
71 | | #[inline] |
72 | 0 | fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { |
73 | 0 | self.0.record_debug(field, &format_args!("{:#?}", value)) |
74 | 0 | } |
75 | | } |
76 | | |
77 | | impl<V, O> VisitOutput<O> for Alt<V> |
78 | | where |
79 | | V: VisitOutput<O>, |
80 | | { |
81 | | #[inline] |
82 | 0 | fn finish(self) -> O { |
83 | 0 | self.0.finish() |
84 | 0 | } |
85 | | } |
86 | | |
87 | | feature! { |
88 | | #![feature = "std"] |
89 | | use super::VisitWrite; |
90 | | use std::io; |
91 | | |
92 | | impl<V> VisitWrite for Alt<V> |
93 | | where |
94 | | V: VisitWrite, |
95 | | { |
96 | | #[inline] |
97 | 0 | fn writer(&mut self) -> &mut dyn io::Write { |
98 | 0 | self.0.writer() |
99 | 0 | } |
100 | | } |
101 | | } |
102 | | |
103 | | impl<V> VisitFmt for Alt<V> |
104 | | where |
105 | | V: VisitFmt, |
106 | | { |
107 | | #[inline] |
108 | 0 | fn writer(&mut self) -> &mut dyn fmt::Write { |
109 | 0 | self.0.writer() |
110 | 0 | } |
111 | | } |