Skip to content

Commit 82a92df

Browse files
committed
fix: correct SerializeField definition and doc formatting (#3040)
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`. Some additional doc formatting issues in `tracing-subscriber` were fixed so that list items that run to multiple lines are correctly indented.
1 parent 0759973 commit 82a92df

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

tracing-serde/src/lib.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@
7575
//! next_id: AtomicUsize, // you need to assign span IDs, so you need a counter
7676
//! }
7777
//!
78+
//! impl JsonSubscriber {
79+
//! fn new() -> Self {
80+
//! Self { next_id: 1.into() }
81+
//! }
82+
//! }
83+
//!
7884
//! impl Subscriber for JsonSubscriber {
7985
//!
8086
//! fn new_span(&self, attrs: &Attributes<'_>) -> Id {
@@ -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<'_>) {}
@@ -199,6 +205,18 @@ use tracing_core::{
199205

200206
pub mod fields;
201207

208+
#[derive(Debug)]
209+
pub struct SerializeField<'a>(&'a Field);
210+
211+
impl<'a> Serialize for SerializeField<'a> {
212+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
213+
where
214+
S: Serializer,
215+
{
216+
serializer.serialize_str(self.0.name())
217+
}
218+
}
219+
202220
#[derive(Debug)]
203221
pub struct SerializeFieldSet<'a>(&'a FieldSet);
204222

@@ -209,7 +227,7 @@ impl<'a> Serialize for SerializeFieldSet<'a> {
209227
{
210228
let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
211229
for element in self.0 {
212-
seq.serialize_element(element.name())?;
230+
seq.serialize_element(&SerializeField(&element))?;
213231
}
214232
seq.end()
215233
}
@@ -552,6 +570,14 @@ impl<'a> AsSerde<'a> for Level {
552570
}
553571
}
554572

573+
impl<'a> AsSerde<'a> for Field {
574+
type Serializable = SerializeField<'a>;
575+
576+
fn as_serde(&'a self) -> Self::Serializable {
577+
SerializeField(self)
578+
}
579+
}
580+
555581
impl<'a> AsSerde<'a> for FieldSet {
556582
type Serializable = SerializeFieldSet<'a>;
557583

@@ -572,6 +598,8 @@ impl<'a> self::sealed::Sealed for Record<'a> {}
572598

573599
impl<'a> self::sealed::Sealed for Metadata<'a> {}
574600

601+
impl self::sealed::Sealed for Field {}
602+
575603
impl self::sealed::Sealed for FieldSet {}
576604

577605
mod sealed {

0 commit comments

Comments
 (0)