Skip to content

Commit

Permalink
core: add ValueSet::len and Record::len (#2152)
Browse files Browse the repository at this point in the history
## Motivation

This PR adds two new accessor functions that are useful for creating a
structured serde implementation for tracing.

This is a sort of "distilled" version of
#2113, based on the `v0.1.x`
branch.

As it is unlikely that "structured serde" will be 1:1 compatible with
the existing JSON-based `tracing-serde` (or at least - I'm not sure how
to do it in a reasonable amount of effort), these functions will allow
me to make a separate crate to hold me over until breaking formatting
changes are possible in `tracing-serde`.

CC @hawkw, as we've discussed this pretty extensively
  • Loading branch information
jamesmunns committed Jun 7, 2022
1 parent 1133a08 commit 2f23ecc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tracing-core/src/field.rs
Expand Up @@ -935,6 +935,19 @@ impl<'a> ValueSet<'a> {
}
}

/// Returns the number of fields in this `ValueSet` that would be visited
/// by a given [visitor] to the [`ValueSet::record()`] method.
///
/// [visitor]: Visit
/// [`ValueSet::record()`]: ValueSet::record()
pub fn len(&self) -> usize {
let my_callsite = self.callsite();
self.values
.iter()
.filter(|(field, _)| field.callsite() == my_callsite)
.count()
}

/// Returns `true` if this `ValueSet` contains a value for the given `Field`.
pub(crate) fn contains(&self, field: &Field) -> bool {
field.callsite() == self.callsite()
Expand All @@ -945,7 +958,7 @@ impl<'a> ValueSet<'a> {
}

/// Returns true if this `ValueSet` contains _no_ values.
pub(crate) fn is_empty(&self) -> bool {
pub fn is_empty(&self) -> bool {
let my_callsite = self.callsite();
self.values
.iter()
Expand Down
8 changes: 8 additions & 0 deletions tracing-core/src/span.rs
Expand Up @@ -225,6 +225,14 @@ impl<'a> Record<'a> {
self.values.record(visitor)
}

/// Returns the number of fields that would be visited from this `Record`
/// when [`Record::record()`] is called
///
/// [`Record::record()`]: Record::record()
pub fn len(&self) -> usize {
self.values.len()
}

/// Returns `true` if this `Record` contains a value for the given `Field`.
pub fn contains(&self, field: &field::Field) -> bool {
self.values.contains(field)
Expand Down

0 comments on commit 2f23ecc

Please sign in to comment.