diff --git a/examples/examples/valuable.rs b/examples/examples/valuable.rs index 53913a52c6..1d463e6f02 100644 --- a/examples/examples/valuable.rs +++ b/examples/examples/valuable.rs @@ -12,8 +12,6 @@ //! //! Therefore, when `valuable` support is not enabled, this example falls back to using //! `fmt::Debug` to record fields that implement `valuable::Valuable`. -#[cfg(tracing_unstable)] -use tracing::field::valuable; use tracing::{info, info_span}; use valuable::Valuable; @@ -49,7 +47,7 @@ fn main() { // If the `valuable` feature is enabled, record `user` using its' // `valuable::Valuable` implementation: #[cfg(tracing_unstable)] - let span = info_span!("Processing", user = valuable(&user)); + let span = info_span!("Processing", user = user.as_value()); // Otherwise, record `user` using its `fmt::Debug` implementation: #[cfg(not(tracing_unstable))] diff --git a/tracing-core/src/field.rs b/tracing-core/src/field.rs index 26b1006261..a57962f577 100644 --- a/tracing-core/src/field.rs +++ b/tracing-core/src/field.rs @@ -189,7 +189,7 @@ pub trait Visit { /// [`valuable`]: https://docs.rs/valuable #[cfg(all(tracing_unstable, feature = "valuable"))] #[cfg_attr(docsrs, doc(cfg(all(tracing_unstable, feature = "valuable"))))] - fn record_value(&mut self, field: &Field, value: &dyn valuable::Valuable) { + fn record_value(&mut self, field: &Field, value: &valuable::Value<'_>) { self.record_debug(field, &value) } @@ -258,14 +258,6 @@ pub struct DisplayValue(T); #[derive(Clone)] pub struct DebugValue(T); -/// A `Value` which serializes using [`Valuable`]. -/// -/// [`Valuable`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html -#[derive(Clone)] -#[cfg(all(tracing_unstable, feature = "valuable"))] -#[cfg_attr(docsrs, doc(cfg(all(tracing_unstable, feature = "valuable"))))] -pub struct ValuableValue(T); - /// Wraps a type implementing `fmt::Display` as a `Value` that can be /// recorded using its `Display` implementation. pub fn display(t: T) -> DisplayValue @@ -290,11 +282,11 @@ where /// [`Valuable`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html #[cfg(all(tracing_unstable, feature = "valuable"))] #[cfg_attr(docsrs, doc(cfg(all(tracing_unstable, feature = "valuable"))))] -pub fn valuable(t: T) -> ValuableValue +pub fn valuable(t: &T) -> valuable::Value<'_> where T: valuable::Valuable, { - ValuableValue(t) + t.as_value() } // ===== impl Visit ===== @@ -572,21 +564,24 @@ impl fmt::Debug for DebugValue { // ===== impl ValuableValue ===== #[cfg(all(tracing_unstable, feature = "valuable"))] -impl crate::sealed::Sealed for ValuableValue {} +impl crate::sealed::Sealed for valuable::Value<'_> {} #[cfg(all(tracing_unstable, feature = "valuable"))] #[cfg_attr(docsrs, doc(cfg(all(tracing_unstable, feature = "valuable"))))] -impl Value for ValuableValue { +impl Value for valuable::Value<'_> { fn record(&self, key: &Field, visitor: &mut dyn Visit) { - visitor.record_value(key, &self.0) + visitor.record_value(key, self) } } +#[cfg(all(tracing_unstable, feature = "valuable"))] +impl crate::sealed::Sealed for &'_ dyn valuable::Valuable {} + #[cfg(all(tracing_unstable, feature = "valuable"))] #[cfg_attr(docsrs, doc(cfg(all(tracing_unstable, feature = "valuable"))))] -impl fmt::Debug for ValuableValue { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", &self.0 as &dyn valuable::Valuable) +impl Value for &'_ dyn valuable::Valuable { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + visitor.record_value(key, &self.as_value()) } }