@@ -26,13 +26,31 @@ static NOOP_SPAN: Lazy<SynchronizedSpan> = Lazy::new(|| SynchronizedSpan {
26
26
pub struct SpanRef < ' a > ( & ' a SynchronizedSpan ) ;
27
27
28
28
#[ derive( Debug ) ]
29
- struct SynchronizedSpan {
29
+ pub ( crate ) struct SynchronizedSpan {
30
30
/// Immutable span context
31
31
span_context : SpanContext ,
32
32
/// Mutable span inner that requires synchronization
33
33
inner : Option < Mutex < global:: BoxedSpan > > ,
34
34
}
35
35
36
+ impl From < SpanContext > for SynchronizedSpan {
37
+ fn from ( value : SpanContext ) -> Self {
38
+ Self {
39
+ span_context : value,
40
+ inner : None ,
41
+ }
42
+ }
43
+ }
44
+
45
+ impl < T : Span + Send + Sync + ' static > From < T > for SynchronizedSpan {
46
+ fn from ( value : T ) -> Self {
47
+ Self {
48
+ span_context : value. span_context ( ) . clone ( ) ,
49
+ inner : Some ( Mutex :: new ( global:: BoxedSpan :: new ( value) ) ) ,
50
+ }
51
+ }
52
+ }
53
+
36
54
impl SpanRef < ' _ > {
37
55
fn with_inner_mut < F : FnOnce ( & mut global:: BoxedSpan ) > ( & self , f : F ) {
38
56
if let Some ( ref inner) = self . 0 . inner {
@@ -253,36 +271,27 @@ pub trait TraceContextExt {
253
271
254
272
impl TraceContextExt for Context {
255
273
fn current_with_span < T : crate :: trace:: Span + Send + Sync + ' static > ( span : T ) -> Self {
256
- Context :: current_with_value ( SynchronizedSpan {
257
- span_context : span. span_context ( ) . clone ( ) ,
258
- inner : Some ( Mutex :: new ( global:: BoxedSpan :: new ( span) ) ) ,
259
- } )
274
+ Context :: current_with_synchronized_span ( span. into ( ) )
260
275
}
261
276
262
277
fn with_span < T : crate :: trace:: Span + Send + Sync + ' static > ( & self , span : T ) -> Self {
263
- self . with_value ( SynchronizedSpan {
264
- span_context : span. span_context ( ) . clone ( ) ,
265
- inner : Some ( Mutex :: new ( global:: BoxedSpan :: new ( span) ) ) ,
266
- } )
278
+ self . with_synchronized_span ( span. into ( ) )
267
279
}
268
280
269
281
fn span ( & self ) -> SpanRef < ' _ > {
270
- if let Some ( span) = self . get :: < SynchronizedSpan > ( ) {
282
+ if let Some ( span) = self . span . as_ref ( ) {
271
283
SpanRef ( span)
272
284
} else {
273
285
SpanRef ( & NOOP_SPAN )
274
286
}
275
287
}
276
288
277
289
fn has_active_span ( & self ) -> bool {
278
- self . get :: < SynchronizedSpan > ( ) . is_some ( )
290
+ self . span . is_some ( )
279
291
}
280
292
281
293
fn with_remote_span_context ( & self , span_context : crate :: trace:: SpanContext ) -> Self {
282
- self . with_value ( SynchronizedSpan {
283
- span_context,
284
- inner : None ,
285
- } )
294
+ self . with_synchronized_span ( span_context. into ( ) )
286
295
}
287
296
}
288
297
0 commit comments