Skip to main content

tide_server_timing/
span_ext.rs

1use crate::{timing_layer::WithContext, SpanRootTiming, SpanTiming};
2
3/// A trait ext to cast a Span down to a Registry.
4pub(crate) trait SpanExt {
5    /// Take an item from the Span's typemap.
6    fn take_ext<F>(&self, f: F)
7    where
8        F: FnMut(SpanTiming);
9
10    /// Insert a SpanRootTiming into the Span's typemap.
11    fn insert_ext(&self, item: SpanRootTiming);
12}
13
14impl SpanExt for tracing::Span {
15    fn take_ext<F>(&self, mut f: F)
16    where
17        F: FnMut(SpanTiming),
18    {
19        self.with_subscriber(|(id, subscriber)| {
20            if let Some(ctx) = subscriber.downcast_ref::<WithContext>() {
21                ctx.take(subscriber, id, &mut f)
22            }
23        });
24    }
25
26    fn insert_ext(&self, item: SpanRootTiming) {
27        self.with_subscriber(|(id, subscriber)| {
28            if let Some(ctx) = subscriber.downcast_ref::<WithContext>() {
29                ctx.set(subscriber, id, item)
30            }
31        });
32    }
33}