Skip to content

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.

  • 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

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) -> bool

Remove a string value from the context

fn clear_all(&self)

Clear all context values

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() -> Self

Create 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

fn remove<T>(&self) -> Option<T>

Remove a typed value from the context

fn clear(&self)

Clear all context values

fn scoped<T>(&self, value: T) -> Guard<T, impl FnOnce + ?>

Create a scoped context guard for a typed value

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() -> Self

Create 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

fn remove<T>(&self) -> bool

Remove a typed value from the global context

fn clear(&self)

Clear all global context values

Simple hash map based context manager

This is useful for basic context management where thread safety is handled externally.

Methods

fn new() -> Self

Create a new hash map context

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) -> Self

Create a new context key

fn name(&self) -> &''static str

Get the key name

fn set_context<T>(value: T)

Set a typed value in the global thread-local context

fn get_context<T>() -> Option<T>

Get a typed value from the global thread-local context

fn remove_context<T>() -> Option<T>

Remove a typed value from the global thread-local context

fn clear_context()

Clear all values from the global thread-local context

fn scoped_context<T>(value: T) -> impl Drop

Create a scoped context guard for the global thread-local context

fn with_context_value<T, F, R>(value: T, f: F) -> R
where
T: ? + Clone,
F: FnOnce

Execute a function with a scoped context value