EventContext

Struct EventContext 

Source
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

§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 context

Implementations§

Source§

impl EventContext

Source

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 thread
Source

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 stack
Source

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 gone
Source

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 value
  • value - 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();
Source

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_id

Trait Implementations§

Source§

impl Drop for EventContext

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more