use dactor::ActorContext;
use dcontext::ContextFutureExt;
use dcontext::ContextSnapshot;
use crate::header::{ContextHeader, ContextSnapshotHeader};
pub fn extract_context(ctx: &ActorContext) -> Option<ContextSnapshot> {
if let Some(h) = ctx.headers.get::<ContextSnapshotHeader>() {
return Some(h.snapshot.clone());
}
if let Some(h) = ctx.headers.get::<ContextHeader>() {
return bytes_to_snapshot(&h.bytes);
}
None
}
#[deprecated(
since = "0.2.0",
note = "ContextInboundInterceptor now restores context automatically via wrap_handler. \
Use this only for manual context restoration outside the interceptor pipeline."
)]
pub async fn with_propagated_context<F, R>(ctx: &ActorContext, f: F) -> R
where
F: std::future::Future<Output = R>,
{
match extract_context(ctx) {
Some(snap) => f.attach(snap).await,
None => f.await,
}
}
pub(crate) fn bytes_to_snapshot(bytes: &[u8]) -> Option<ContextSnapshot> {
dcontext::ContextSnapshot::deserialize(bytes).ok()
}