pub struct EventContext { /* private fields */ }Expand description
Thread-local event context for tracking metadata throughout event sourcing operations.
EventContext provides a way to attach contextual information (like request IDs, audit info,
or operation metadata) to events as they are created and persisted. The context is managed
as a thread-local stack, allowing for nested contexts within the same thread.
§Thread Safety
This struct is deliberately !Send to ensure thread-local safety. It uses Rc for reference
counting which is not thread-safe. For propagating context across async boundaries or threads,
use the WithEventContext trait which safely transfers context data.
§Usage Patterns
- Same thread: Use
fork()to create isolated child contexts - Async tasks: Use
with_event_context()from theWithEventContexttrait - New threads: Use
seed()with data fromdata()to transfer context
§Examples
use es_entity::context::EventContext;
// Create or get current context
let mut ctx = EventContext::current();
ctx.insert("user_id", &"123").unwrap();
// Fork for isolated scope
{
let mut child = EventContext::fork();
child.insert("operation", &"update").unwrap();
// Both user_id and operation are available here
}
// Only user_id remains in parent contextImplementations§
Source§impl EventContext
impl EventContext
Sourcepub fn current() -> Self
pub fn current() -> Self
Gets the current event context or creates a new one if none exists.
This function is thread-local and will return a handle to the topmost context on the current thread’s context stack. If no context exists, it will create a new empty context and push it onto the stack.
§Examples
use es_entity::context::EventContext;
let ctx = EventContext::current();
// Context is now available for the current threadSourcepub fn seed(data: ContextData) -> Self
pub fn seed(data: ContextData) -> Self
Creates a new event context seeded with the provided data.
This creates a completely new context stack entry with the given context data, independent of any existing context. This is useful for starting fresh contexts in new threads or async tasks.
§Arguments
data- The initial context data for the new context
§Examples
use es_entity::context::{EventContext, ContextData};
let data = EventContext::current().data();
let new_ctx = EventContext::seed(data);
// new_ctx now has its own independent context stackSourcepub fn fork() -> Self
pub fn fork() -> Self
Creates a new isolated context that inherits data from the current context.
This method creates a child context that starts with a copy of the current context’s data. Changes made to the forked context will not affect the parent context, and when the forked context is dropped, the parent context remains unchanged. This is useful for creating isolated scopes within the same thread.
§Examples
use es_entity::context::EventContext;
let mut parent = EventContext::current();
parent.insert("shared", &"value").unwrap();
{
let mut child = EventContext::fork();
child.insert("child_only", &"data").unwrap();
// child context has both "shared" and "child_only"
}
// parent context only has "shared" - "child_only" is goneSourcepub fn insert<T: Serialize>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Error>
pub fn insert<T: Serialize>( &mut self, key: &'static str, value: &T, ) -> Result<(), Error>
Inserts a key-value pair into the current context.
The value will be serialized to JSON and stored in the context data.
This data will be available to all code running within this context
and any child contexts created via fork().
§Arguments
key- A static string key to identify the valuevalue- Any serializable value to store in the context
§Returns
Returns Ok(()) on success or a serde_json::Error if serialization fails.
§Examples
use es_entity::context::EventContext;
let mut ctx = EventContext::current();
ctx.insert("user_id", &"12345").unwrap();
ctx.insert("operation", &"transfer").unwrap();Sourcepub fn data(&self) -> ContextData
pub fn data(&self) -> ContextData
Returns a copy of the current context data.
This method returns a snapshot of all key-value pairs stored in this context.
The returned ContextData can be used to seed new contexts or passed to
async tasks to maintain context across thread boundaries.
§Examples
use es_entity::context::EventContext;
let mut ctx = EventContext::current();
ctx.insert("request_id", &"abc123").unwrap();
let data = ctx.data();
// data now contains a copy of the context with request_idTrait Implementations§
Auto Trait Implementations§
impl Freeze for EventContext
impl RefUnwindSafe for EventContext
impl !Send for EventContext
impl !Sync for EventContext
impl Unpin for EventContext
impl UnwindSafe for EventContext
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more