Expand description
§dcontext
Distributed context propagation for Rust.
dcontext provides a scoped, type-safe key-value store that travels with
the execution flow - across function calls, async/sync boundaries, thread
spawns, and even process boundaries via serialization.
§Architecture
A single thread_local! store is the source of truth. For async code,
the WithContext future wrapper swaps the store in/out on each poll,
making it effectively task-local without any runtime dependency.
Re-exports§
pub use error::ContextError;
Modules§
Macros§
- register_
contexts - Register multiple context types on a builder at once.
Structs§
- Attach
Guard - RAII guard that restores the previous thread-local store on drop.
- Context
Key - A typed handle to a registered context entry.
- Context
Snapshot - An immutable snapshot of the current context. Clone + Send + Sync.
- Context
Store - The active context state. Lives in
Cell<Option<ContextStore>>. - Registration
Options - Builder for configuring per-key registration options.
- Registry
Builder - Collects context registrations during application startup.
- Scope
Guard - RAII guard that reverts a scope on drop.
- With
Context - A future wrapped with a
ContextStorethat is swapped into the thread-local on each poll and swapped back out after.
Traits§
- Context
Future Ext - Extension trait providing context propagation for futures.
Functions§
- attach_
from_ bytes - Deserialize wire-format bytes and attach as root context.
- attach_
snapshot - Attach a snapshot as root context. Returns an
AttachGuardthat restores previous state. - attach_
snapshot_ with_ scope - Attach a snapshot and push a named scope in one call.
- attach_
store - Attach a
ContextStoreas root context. Returns anAttachGuard. - capture
- Capture a snapshot of the current context.
Local-only variables (registered with
.local_only()) are excluded. - capture_
serialized - Capture a snapshot of the current context and serialize it to wire-format bytes.
- clear
- Clear the context entirely.
- fork
- Fork the current context. Creates a child store with a frozen parent. Value lookups fall through to the frozen parent (cheap, Arc-shared). Writes are isolated in the child (copy-on-write).
- get_
context_ variable - Get a context variable. Returns
Noneif the key is not set. - initialize
- Freeze the registry. Consumes the builder and makes all reads lock-free.
- keys_
with_ metadata - Iterate over all registered keys that have metadata of type
M. - make_
wire_ bytes - Construct wire-format bytes with a single entry.
- make_
wire_ bytes_ v - Like
make_wire_bytesbut allows specifying the wire format version. - max_
context_ size - Get the current max context size limit. 0 means no limit.
- max_
scope_ chain_ len - Get the current max scope chain length. 0 means no limit. Default is 64.
- merge_
with - Merge values from another store into the current context. Only merges values, not scope chain.
- push_
scope - Push a named scope onto the context store.
Returns a
ScopeGuardthat pops the scope on drop. - scope_
chain - Get the current scope chain — the ordered list of named scopes from root to current.
- set_
context_ variable - Set a context variable.
- set_
max_ context_ size - Set the maximum serialized context size in bytes.
Serialization functions will return
ContextTooLargeif exceeded. Set to 0 to disable (default). - set_
max_ scope_ chain_ len - Set the maximum number of entries in the scope chain.
- try_
initialize - Try to freeze the registry. Returns
Errif already initialized. - update_
context_ variable - Update a context variable using a callback (read-modify-write).
- with_
metadata - Access typed metadata for a registered key via callback.