1use std::future::Future;
16use std::sync::Arc;
17
18use crate::context::TreeContext;
19use crate::global::global_registry;
20use crate::registry::WeakRegistry;
21use crate::Registry;
22
23pub struct TreeRoot {
25 pub(crate) context: Arc<TreeContext>,
26 pub(crate) registry: WeakRegistry,
27}
28
29task_local::task_local! {
30 static ROOT: TreeRoot
31}
32
33pub(crate) fn current_context() -> Option<Arc<TreeContext>> {
34 ROOT.try_with(|r| r.context.clone()).ok()
35}
36
37pub(crate) fn current_registry() -> Option<Registry> {
38 let local = || ROOT.try_with(|r| r.registry.upgrade()).ok().flatten();
39 let global = global_registry;
40
41 local().or_else(global)
42}
43
44impl TreeRoot {
45 pub async fn instrument<F: Future>(self, future: F) -> F::Output {
47 ROOT.scope(self, future).await
48 }
49}