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.
This commit is contained in:
Hayden Stainsby 2024-07-29 20:54:48 +02:00
parent 07599738b3
commit 82a92dfd8d

View File

@ -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 {
@ -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<'_>) {}
@ -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);
@ -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()
}
@ -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>;
@ -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 {