Skip to content

Commit

Permalink
fix: correct SerializeField definition and doc formatting (#3040)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hds committed Nov 22, 2024
1 parent 0759973 commit 82a92df
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions tracing-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
//! next_id: AtomicUsize, // you need to assign span IDs, so you need a counter
//! }
//!
//! impl JsonSubscriber {
//! fn new() -> Self {
//! Self { next_id: 1.into() }
//! }
//! }
//!
//! impl Subscriber for JsonSubscriber {
//!
//! fn new_span(&self, attrs: &Attributes<'_>) -> Id {
Expand All @@ -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<'_>) {}
Expand Down Expand Up @@ -199,6 +205,18 @@ use tracing_core::{

pub mod fields;

#[derive(Debug)]
pub struct SerializeField<'a>(&'a Field);

impl<'a> Serialize for SerializeField<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.0.name())
}
}

#[derive(Debug)]
pub struct SerializeFieldSet<'a>(&'a FieldSet);

Expand All @@ -209,7 +227,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()
}
Expand Down Expand Up @@ -552,6 +570,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>;

Expand All @@ -572,6 +598,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 {
Expand Down

0 comments on commit 82a92df

Please sign in to comment.