use std::future::Future; use tokio::task_local; task_local! { static REQUEST_ID: String; } pub async fn with_request_id(request_id: Option, future: F) -> F::Output where F: Future, S: AsRef, { match request_id { Some(request_id) => { REQUEST_ID .scope(request_id.as_ref().to_owned(), future) .await } None => future.await, } } pub fn request_id() -> Option { REQUEST_ID.try_with(Clone::clone).ok() } /// Prefers an explicitly passed id, then the id of the current request scope. pub fn resolve_request_id(explicit: Option<&str>) -> String { explicit .filter(|value| !value.is_empty()) .map(str::to_owned) .or_else(request_id) .unwrap_or_default() }