Skip to content

Commit 5776262

Browse files
committed
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`.
1 parent 1898311 commit 5776262

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

tracing-serde/src/lib.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@
7171
//! use tracing_serde::AsSerde;
7272
//! use serde_json::json;
7373
//!
74-
//! pub struct JsonSubscriber {
74+
//! pub struct JsonCollector {
7575
//! next_id: AtomicUsize, // you need to assign span IDs, so you need a counter
7676
//! }
7777
//!
78-
//! impl Collect for JsonSubscriber {
78+
//! impl JsonCollector {
79+
//! fn new() -> Self {
80+
//! Self { next_id: 1.into() }
81+
//! }
82+
//! }
83+
//!
84+
//! impl Collect for JsonCollector {
7985
//!
8086
//! fn new_span(&self, attrs: &Attributes<'_>) -> Id {
8187
//! let id = self.next_id.fetch_add(1, Ordering::Relaxed);
@@ -97,7 +103,7 @@
97103
//! }
98104
//!
99105
//! // ...
100-
//! # fn enabled(&self, _: &Metadata<'_>) -> bool { false }
106+
//! # fn enabled(&self, _: &Metadata<'_>) -> bool { true }
101107
//! # fn enter(&self, _: &Id) {}
102108
//! # fn exit(&self, _: &Id) {}
103109
//! # fn record(&self, _: &Id, _: &Record<'_>) {}
@@ -107,7 +113,7 @@
107113
//! ```
108114
//!
109115
//! After you implement your `Collector`, you can use your `tracing`
110-
//! subscriber (`JsonSubscriber` in the above example) to record serialized
116+
//! collector (`JsonCollector` in the above example) to record serialized
111117
//! trace data.
112118
//!
113119
//! ## Crate Feature Flags
@@ -188,9 +194,9 @@ use tracing_core::{
188194
pub mod fields;
189195

190196
#[derive(Debug)]
191-
pub struct SerializeField(Field);
197+
pub struct SerializeField<'a>(&'a Field);
192198

193-
impl Serialize for SerializeField {
199+
impl<'a> Serialize for SerializeField<'a> {
194200
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
195201
where
196202
S: Serializer,
@@ -209,7 +215,7 @@ impl<'a> Serialize for SerializeFieldSet<'a> {
209215
{
210216
let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
211217
for element in self.0 {
212-
seq.serialize_element(element.name())?;
218+
seq.serialize_element(&SerializeField(&element))?;
213219
}
214220
seq.end()
215221
}
@@ -532,6 +538,14 @@ impl<'a> AsSerde<'a> for Level {
532538
}
533539
}
534540

541+
impl<'a> AsSerde<'a> for Field {
542+
type Serializable = SerializeField<'a>;
543+
544+
fn as_serde(&'a self) -> Self::Serializable {
545+
SerializeField(self)
546+
}
547+
}
548+
535549
impl<'a> AsSerde<'a> for FieldSet {
536550
type Serializable = SerializeFieldSet<'a>;
537551

@@ -552,6 +566,8 @@ impl<'a> self::sealed::Sealed for Record<'a> {}
552566

553567
impl<'a> self::sealed::Sealed for Metadata<'a> {}
554568

569+
impl self::sealed::Sealed for Field {}
570+
555571
impl self::sealed::Sealed for FieldSet {}
556572

557573
mod sealed {

0 commit comments

Comments
 (0)