Skip to content

Commit f1d8f41

Browse files
committed
core: change metadata::Kind to a bitflag (#1891)
This changes the `Kind` type to a bitflag, in order to represent callsites that are hints but also count as spans or events. The goal here is to allow variants of the `enabled!` macro that specifically check if a span would be enabled, or if an event would be enabled. See [this comment][1] for details. This does not actually implement the `enabled!` variants, just the `Kind` representation change. This way, we can add to the `enabled!` macro in a subsequent `tracing` release without having to change `tracing-core` again. I went with the bitflag representation rather than adding a bool to the `KindInner::Span` and `KindInner::Event` enum variants because it felt a bit simpler and maybe more performant, although I don't think it's particularly important to micro-optimize here. I'd consider changing this to an enum-based representation if people think it's significantly easier to understand. [1]: #1821 (comment)
1 parent 8147827 commit f1d8f41

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

tracing-core/src/metadata.rs

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ pub struct Metadata<'a> {
8989
}
9090

9191
/// Indicates whether the callsite is a span or event.
92-
#[derive(Clone, Debug, Eq, PartialEq)]
93-
pub struct Kind(KindInner);
92+
#[derive(Clone, Eq, PartialEq)]
93+
pub struct Kind(u8);
94+
9495
/// Describes the level of verbosity of a span or event.
9596
///
9697
/// # Comparing Levels
@@ -367,38 +368,78 @@ impl<'a> fmt::Debug for Metadata<'a> {
367368
}
368369
}
369370

370-
#[derive(Clone, Debug, Eq, PartialEq)]
371-
enum KindInner {
372-
Event,
373-
Span,
374-
Hint,
375-
}
376-
377371
impl Kind {
372+
const EVENT_BIT: u8 = 1 << 0;
373+
const SPAN_BIT: u8 = 1 << 1;
374+
const HINT_BIT: u8 = 1 << 2;
375+
378376
/// `Event` callsite
379-
pub const EVENT: Kind = Kind(KindInner::Event);
377+
pub const EVENT: Kind = Kind(Self::EVENT_BIT);
380378

381379
/// `Span` callsite
382-
pub const SPAN: Kind = Kind(KindInner::Span);
380+
pub const SPAN: Kind = Kind(Self::SPAN_BIT);
383381

384382
/// `enabled!` callsite. [`Subscriber`][`crate::subscriber::Subscriber`]s can assume
385383
/// this `Kind` means they will never recieve a
386384
/// full event with this [`Metadata`].
387-
pub const HINT: Kind = Kind(KindInner::Hint);
385+
pub const HINT: Kind = Kind(Self::HINT_BIT);
388386

389387
/// Return true if the callsite kind is `Span`
390388
pub fn is_span(&self) -> bool {
391-
matches!(self, Kind(KindInner::Span))
389+
self.0 & Self::SPAN_BIT == Self::SPAN_BIT
392390
}
393391

394392
/// Return true if the callsite kind is `Event`
395393
pub fn is_event(&self) -> bool {
396-
matches!(self, Kind(KindInner::Event))
394+
self.0 & Self::EVENT_BIT == Self::EVENT_BIT
397395
}
398396

399397
/// Return true if the callsite kind is `Hint`
400398
pub fn is_hint(&self) -> bool {
401-
matches!(self, Kind(KindInner::Hint))
399+
self.0 & Self::HINT_BIT == Self::HINT_BIT
400+
}
401+
402+
/// Sets that this `Kind` is a [hint](Self::HINT).
403+
///
404+
/// This can be called on [`SPAN`](Self::SPAN) and [`EVENT`](Self::EVENT)
405+
/// kinds to construct a hint callsite that also counts as a span or event.
406+
pub const fn hint(self) -> Self {
407+
Self(self.0 | Self::HINT_BIT)
408+
}
409+
}
410+
411+
impl fmt::Debug for Kind {
412+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
413+
f.write_str("Kind(")?;
414+
let mut has_bits = false;
415+
let mut write_bit = |name: &str| {
416+
if has_bits {
417+
f.write_str(" | ")?;
418+
}
419+
f.write_str(name)?;
420+
has_bits = true;
421+
Ok(())
422+
};
423+
424+
if self.is_event() {
425+
write_bit("EVENT")?;
426+
}
427+
428+
if self.is_span() {
429+
write_bit("SPAN")?;
430+
}
431+
432+
if self.is_hint() {
433+
write_bit("HINT")?;
434+
}
435+
436+
// if none of the expected bits were set, something is messed up, so
437+
// just print the bits for debugging purposes
438+
if !has_bits {
439+
write!(f, "{:#b}", self.0)?;
440+
}
441+
442+
f.write_str(")")
402443
}
403444
}
404445

0 commit comments

Comments
 (0)