use std::future::Future;
use std::sync::Arc;
use crate::context::TreeContext;
use crate::global::global_registry;
use crate::registry::WeakRegistry;
use crate::Registry;
pub struct TreeRoot {
pub(crate) context: Arc<TreeContext>,
pub(crate) registry: WeakRegistry,
}
task_local::task_local! {
static ROOT: TreeRoot
}
pub(crate) fn current_context() -> Option<Arc<TreeContext>> {
ROOT.try_with(|r| r.context.clone()).ok()
}
pub(crate) fn current_registry() -> Option<Registry> {
let local = || ROOT.try_with(|r| r.registry.upgrade()).ok().flatten();
let global = global_registry;
local().or_else(global)
}
impl TreeRoot {
pub async fn instrument<F: Future>(self, future: F) -> F::Output {
ROOT.scope(self, future).await
}
}