use std::future::Future;
tokio::task_local! {
static CURRENT_RUN_ID: String;
}
pub async fn with_run_id<F: Future>(run_id: String, fut: F) -> F::Output {
CURRENT_RUN_ID.scope(run_id, fut).await
}
pub fn current_run_id() -> Option<String> {
CURRENT_RUN_ID.try_with(Clone::clone).ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn the_run_id_is_visible_inside_the_scope_only() {
assert_eq!(current_run_id(), None);
let seen = with_run_id("01RUN".into(), async { current_run_id() }).await;
assert_eq!(seen.as_deref(), Some("01RUN"));
assert_eq!(current_run_id(), None);
}
}