pub struct Context { /* private fields */ }Expand description
A type-safe container for request-scoped data.
Context allows you to pass metadata through policy execution without modifying function signatures.
§Examples
use do_over::context::{Context, ContextKey};
static CORRELATION_ID: ContextKey<String> = ContextKey::new("correlation_id");
static RETRY_COUNT: ContextKey<u32> = ContextKey::new("retry_count");
let mut ctx = Context::new();
ctx.insert(&CORRELATION_ID, "abc-123".to_string());
ctx.insert(&RETRY_COUNT, 0u32);
assert_eq!(ctx.get(&CORRELATION_ID), Some(&"abc-123".to_string()));
assert_eq!(ctx.get(&RETRY_COUNT), Some(&0u32));Implementations§
Source§impl Context
impl Context
Sourcepub fn insert<T: Send + Sync + 'static>(
&mut self,
key: &ContextKey<T>,
value: T,
)
pub fn insert<T: Send + Sync + 'static>( &mut self, key: &ContextKey<T>, value: T, )
Sourcepub fn get<T: Send + Sync + 'static>(&self, key: &ContextKey<T>) -> Option<&T>
pub fn get<T: Send + Sync + 'static>(&self, key: &ContextKey<T>) -> Option<&T>
Get a value from the context.
§Arguments
key- The context key
§Returns
A reference to the value if it exists, or None.
§Examples
use do_over::context::{Context, ContextKey};
static KEY: ContextKey<String> = ContextKey::new("key");
let mut ctx = Context::new();
ctx.insert(&KEY, "value".to_string());
assert_eq!(ctx.get(&KEY), Some(&"value".to_string()));Sourcepub fn remove<T: Send + Sync + Clone + 'static>(
&mut self,
key: &ContextKey<T>,
) -> Option<T>
pub fn remove<T: Send + Sync + Clone + 'static>( &mut self, key: &ContextKey<T>, ) -> Option<T>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Context
impl !RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more