1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use crate::TraceContext;
use std::cell::Cell;
use std::marker::PhantomData;
thread_local! {
static CURRENT: Cell<Option<TraceContext>> = Cell::new(None);
}
pub struct CurrentGuard {
prev: Option<TraceContext>,
_p: PhantomData<*const ()>,
}
unsafe impl Sync for CurrentGuard {}
impl Drop for CurrentGuard {
fn drop(&mut self) {
CURRENT.with(|c| c.set(self.prev));
}
}
pub fn set_current(context: TraceContext) -> CurrentGuard {
CurrentGuard {
prev: CURRENT.with(|c| c.replace(Some(context))),
_p: PhantomData,
}
}
pub fn current() -> Option<TraceContext> {
CURRENT.with(|c| c.get())
}