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.
§Quick Start
use dcontext::{RegistryBuilder, initialize, enter_named_scope, get_context, set_context, scope_chain};
use serde::{Serialize, Deserialize};
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
struct RequestId(String);
let mut builder = RegistryBuilder::new();
builder.register::<RequestId>("request_id");
initialize(builder); // freeze registry — all reads are lock-free
let _guard = enter_named_scope("ingress");
set_context("request_id", RequestId("req-123".into()));
let rid: RequestId = get_context("request_id");
assert_eq!(rid.0, "req-123");
// Query the scope chain — returns names of all active named scopes
let chain = scope_chain(); // vec!["ingress"]See also: enter_scope for unnamed scopes, named_scope_async for
async named scopes, and scope_chain for querying the full distributed
call path (including remote prefix from cross-process propagation).
Re-exports§
pub use error::ContextError;
Modules§
Macros§
- register_
contexts - Register multiple context types on a builder at once.
- with_
scope - Enter a scope, set values, execute a block, and auto-revert.
Structs§
- Context
Key - A typed handle to a registered context entry.
- Context
Snapshot - An immutable snapshot of the current context. Clone + Send + Sync.
- 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. Not Send/Sync — scopes are bound to their storage context.
Functions§
- attach
- Restore a snapshot by pushing a new scope with its values.
- deserialize_
context - Restore context from bytes. Pushes a new scope with deserialized values.
- deserialize_
context_ string - Restore context from a base64-encoded string.
- enter_
named_ scope - Push a new named scope and return a guard.
- enter_
scope - Push a new scope and return a guard.
- force_
thread_ local - Escape hatch: explicitly use thread-local storage even inside an async runtime. Panic-safe and nesting-safe via depth counter + RAII guard.
- get_
context - Get a context value. Returns
T::default()if not set. Panics if the key is not registered. - initialize
- Freeze the registry. Consumes the builder and makes all reads lock-free.
- make_
wire_ bytes - Construct wire-format bytes with a single entry. This is a helper for testing version migration — it lets you create wire bytes as if they came from a sender running an older schema version.
- 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.
- named_
scope_ async - Async version of
enter_named_scope: execute a future in a new named scope. - scope
- Execute
fin a new scope. Changes revert whenfreturns. - scope_
async - Async version: execute a future in a new scope. The scope is entered before polling and exited after the future completes.
- scope_
chain - Return the current scope chain: remote prefix + local named scope names.
- serialize_
context - Serialize the current context (all scopes merged) into bytes.
- serialize_
context_ string - Serialize the current context into a base64-encoded string.
- set_
context - Set a context value in the current scope. Panics if the key is not registered or type doesn’t match.
- set_
context_ local - Set a local-only context value. The type does NOT need Serialize/DeserializeOwned. Panics if the key is not registered or type doesn’t match.
- 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.
- snapshot
- Capture a snapshot of the current effective context.
- spawn_
with_ context - Spawn a std::thread that inherits the current context.
Returns
io::Resultinstead of panicking on spawn failure. - spawn_
with_ context_ async - Spawn a Tokio task that inherits the current context.
- try_
get_ context - Get a context value. Returns
Ok(Some(T))if set,Ok(None)if registered but not set,Errif not registered. - try_
initialize - Try to freeze the registry. Returns
Errif already initialized. - try_
set_ context - Set a context value. Returns Err on type mismatch or if key is not registered.
- try_
set_ context_ local - Set a local-only context value. Returns Err on type mismatch or if key is not registered.
- with_
context - Run an async block with the given snapshot established as task-local context.
- wrap_
with_ context - Wrap a FnOnce closure so context is captured now and restored when called.
- wrap_
with_ context_ fn - Wrap an Fn closure so context is captured now and restored on each call.