use std::sync::OnceLock;
use crate::{FunctionSink, FunctionSinkAlreadySet, ScopeKind};
static SINK: OnceLock<&'static dyn FunctionSink> = OnceLock::new();
pub fn set_function_sink(sink: impl FunctionSink) -> Result<(), FunctionSinkAlreadySet> {
let mut installed = false;
SINK.get_or_init(|| {
installed = true;
Box::leak(Box::new(sink))
});
if installed {
Ok(())
} else {
Err(FunctionSinkAlreadySet)
}
}
#[derive(Debug)]
pub(crate) struct SinkScope {
kind: ScopeKind,
token: u64,
}
impl SinkScope {
#[inline]
pub(crate) fn enter(name: &'static str, kind: ScopeKind, skipped: bool) -> Option<Self> {
if skipped {
return None;
}
let sink = SINK.get()?;
let token = sink.enter(name, kind);
Some(Self { kind, token })
}
#[inline]
pub(crate) fn exit(self, name: &'static str) {
if let Some(sink) = SINK.get() {
sink.exit(name, self.kind, self.token);
}
}
}
impl ScopeKind {
#[inline]
pub(crate) fn of_guard(caller_scoped: bool) -> Self {
if caller_scoped {
Self::Sync
} else {
Self::Async
}
}
}