From 57762622e1404baddc0cd557a36218b420de27c2 Mon Sep 17 00:00:00 2001 From: Hayden Stainsby Date: Fri, 26 Jul 2024 16:47:56 +0200 Subject: [PATCH] fix: correct SerializeField definition Clippy in 1.80.0 alerted us to the fact that `SerializeField` was never constructed (and due to its non-`pub` member, it can't be constructed outside the `tracing-serde` crate where it's from). This change fixes the definition to hold a reference to a `Field`, which is what the other `Serialize*` types do. It also implements `AsSerde` for this type and uses it inside the `SerializeFieldSet` type. As a bonus, Clippy is now also happy that the type is constructed. The example collector in the `tracing-serde` crate was also renamed from `JsonSubscriber` to `JsonCollector`. --- tracing-serde/src/lib.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/tracing-serde/src/lib.rs b/tracing-serde/src/lib.rs index 97b78d55c0..0692f7d645 100644 --- a/tracing-serde/src/lib.rs +++ b/tracing-serde/src/lib.rs @@ -71,11 +71,17 @@ //! use tracing_serde::AsSerde; //! use serde_json::json; //! -//! pub struct JsonSubscriber { +//! pub struct JsonCollector { //! next_id: AtomicUsize, // you need to assign span IDs, so you need a counter //! } //! -//! impl Collect for JsonSubscriber { +//! impl JsonCollector { +//! fn new() -> Self { +//! Self { next_id: 1.into() } +//! } +//! } +//! +//! impl Collect for JsonCollector { //! //! fn new_span(&self, attrs: &Attributes<'_>) -> Id { //! let id = self.next_id.fetch_add(1, Ordering::Relaxed); @@ -97,7 +103,7 @@ //! } //! //! // ... -//! # fn enabled(&self, _: &Metadata<'_>) -> bool { false } +//! # fn enabled(&self, _: &Metadata<'_>) -> bool { true } //! # fn enter(&self, _: &Id) {} //! # fn exit(&self, _: &Id) {} //! # fn record(&self, _: &Id, _: &Record<'_>) {} @@ -107,7 +113,7 @@ //! ``` //! //! After you implement your `Collector`, you can use your `tracing` -//! subscriber (`JsonSubscriber` in the above example) to record serialized +//! collector (`JsonCollector` in the above example) to record serialized //! trace data. //! //! ## Crate Feature Flags @@ -188,9 +194,9 @@ use tracing_core::{ pub mod fields; #[derive(Debug)] -pub struct SerializeField(Field); +pub struct SerializeField<'a>(&'a Field); -impl Serialize for SerializeField { +impl<'a> Serialize for SerializeField<'a> { fn serialize(&self, serializer: S) -> Result where S: Serializer, @@ -209,7 +215,7 @@ impl<'a> Serialize for SerializeFieldSet<'a> { { let mut seq = serializer.serialize_seq(Some(self.0.len()))?; for element in self.0 { - seq.serialize_element(element.name())?; + seq.serialize_element(&SerializeField(&element))?; } seq.end() } @@ -532,6 +538,14 @@ impl<'a> AsSerde<'a> for Level { } } +impl<'a> AsSerde<'a> for Field { + type Serializable = SerializeField<'a>; + + fn as_serde(&'a self) -> Self::Serializable { + SerializeField(self) + } +} + impl<'a> AsSerde<'a> for FieldSet { type Serializable = SerializeFieldSet<'a>; @@ -552,6 +566,8 @@ impl<'a> self::sealed::Sealed for Record<'a> {} impl<'a> self::sealed::Sealed for Metadata<'a> {} +impl self::sealed::Sealed for Field {} + impl self::sealed::Sealed for FieldSet {} mod sealed {