context
Context management patterns
This module provides thread-safe context management for WASM and native environments. Contexts are used to propagate information across function calls without explicit parameter passing.
Key Features
Section titled “Key Features”- Thread-Local Context: WASM-compatible thread-local storage
- Scoped Context: Automatic context cleanup with RAII
- Context Inheritance: Child contexts inherit from parent contexts
- Type Safety: Strongly typed context values
- Performance: Zero-cost abstractions with compile-time optimization
Traits
Section titled “Traits”ContextManager
Section titled “ContextManager”Context manager trait for different context implementations
This trait provides a common interface for different types of context storage.
Required / Provided Methods
fn set_string(&self, key: &str, value: String)Set a string value in the context
fn get_string(&self, key: &str) -> Option<String>Get a string value from the context
fn remove_string(&self, key: &str) -> boolRemove a string value from the context
fn clear_all(&self)Clear all context values
Structs
Section titled “Structs”ThreadLocalContext
Section titled “ThreadLocalContext”Thread-local context storage
This provides WASM-compatible thread-local context storage that automatically cleans up when contexts go out of scope.
Methods
fn new() -> SelfCreate a new thread-local context
fn set<T>(&self, value: T)Set a typed value in the context
fn get<T>(&self) -> Option<T>Get a typed value from the context
remove
Section titled “remove”fn remove<T>(&self) -> Option<T>Remove a typed value from the context
fn clear(&self)Clear all context values
scoped
Section titled “scoped”fn scoped<T>(&self, value: T) -> Guard<T, impl FnOnce + ?>Create a scoped context guard for a typed value
GlobalContext
Section titled “GlobalContext”Global context manager for shared state
This provides a global context that can be shared across threads in environments that support it (not available in WASM).
Methods
fn new() -> SelfCreate a new global context
fn set<T>(&self, value: T)Set a typed value in the global context
fn get<T>(&self) -> Option<T>Get a typed value from the global context
remove
Section titled “remove”fn remove<T>(&self) -> boolRemove a typed value from the global context
fn clear(&self)Clear all global context values
HashMapContext
Section titled “HashMapContext”Simple hash map based context manager
This is useful for basic context management where thread safety is handled externally.
Methods
fn new() -> SelfCreate a new hash map context
ContextKey
Section titled “ContextKey”Context key for type-safe context access
This provides a type-safe way to access context values without relying on string keys.
Methods
const fn new(name: &''static str) -> SelfCreate a new context key
fn name(&self) -> &''static strGet the key name
Functions
Section titled “Functions”set_context
Section titled “set_context”fn set_context<T>(value: T)Set a typed value in the global thread-local context
get_context
Section titled “get_context”fn get_context<T>() -> Option<T>Get a typed value from the global thread-local context
remove_context
Section titled “remove_context”fn remove_context<T>() -> Option<T>Remove a typed value from the global thread-local context
clear_context
Section titled “clear_context”fn clear_context()Clear all values from the global thread-local context
scoped_context
Section titled “scoped_context”fn scoped_context<T>(value: T) -> impl DropCreate a scoped context guard for the global thread-local context
with_context_value
Section titled “with_context_value”fn with_context_value<T, F, R>(value: T, f: F) -> Rwhere T: ? + Clone, F: FnOnceExecute a function with a scoped context value