dcontext_dactor/header.rs
1use std::any::Any;
2
3use dactor::HeaderValue;
4use dcontext::ContextSnapshot;
5
6/// Header carrying serialized context bytes for wire transport.
7///
8/// Attached by [`ContextOutboundInterceptor`](crate::ContextOutboundInterceptor)
9/// on the sender side. Deserialized on the receiver side by
10/// [`ContextInboundInterceptor`](crate::ContextInboundInterceptor) or
11/// the handler via [`extract_context`](crate::extract_context).
12///
13/// For remote transport, register the deserializer with
14/// [`register_context_headers`](crate::register_context_headers) at startup.
15pub struct ContextHeader {
16 /// Serialized dcontext bytes (bincode wire format).
17 pub(crate) bytes: Vec<u8>,
18}
19
20impl ContextHeader {
21 /// Create a `ContextHeader` from raw wire bytes.
22 ///
23 /// Used by the `HeaderRegistry` deserializer to reconstruct this header
24 /// from bytes received over the wire.
25 pub fn from_bytes(bytes: Vec<u8>) -> Self {
26 Self { bytes }
27 }
28}
29
30impl HeaderValue for ContextHeader {
31 fn header_name(&self) -> &'static str {
32 "dcontext.wire"
33 }
34
35 /// Returns the serialized bytes for remote transport.
36 fn to_bytes(&self) -> Option<Vec<u8>> {
37 Some(self.bytes.clone())
38 }
39
40 fn as_any(&self) -> &dyn Any {
41 self
42 }
43}
44
45/// Header carrying a `ContextSnapshot` for in-process (local) propagation.
46///
47/// This preserves local-only context values that would be lost during
48/// serialization. Preferred over [`ContextHeader`] for same-process actors.
49pub struct ContextSnapshotHeader {
50 pub(crate) snapshot: ContextSnapshot,
51}
52
53impl HeaderValue for ContextSnapshotHeader {
54 fn header_name(&self) -> &'static str {
55 "dcontext.snapshot"
56 }
57
58 /// Returns `None` — snapshots are local-only and not sent over the wire.
59 fn to_bytes(&self) -> Option<Vec<u8>> {
60 None
61 }
62
63 fn as_any(&self) -> &dyn Any {
64 self
65 }
66}