Struct Context
pub struct Context { /* private fields */ }
Expand description
Context passed to and between services as input.
See crate::context
for more information.
Implementations§
§impl Context
impl Context
pub fn from_parts(parts: Parts) -> Context
pub fn into_parts(self) -> Parts
pub fn set_executor(&mut self, exec: Executor) -> &mut Context
pub fn set_executor(&mut self, exec: Executor) -> &mut Context
pub fn with_executor(self, exec: Executor) -> Context
pub fn with_executor(self, exec: Executor) -> Context
pub fn spawn<F>(&self, future: F) -> JoinHandle<<F as Future>::Output> ⓘ
pub fn spawn<F>(&self, future: F) -> JoinHandle<<F as Future>::Output> ⓘ
Spawn a future on the current executor, this is spawned gracefully in case a shutdown guard has been registered.
pub fn contains<T>(&self) -> bool
pub fn contains<T>(&self) -> bool
Returns true if the Context
contains the given type.
Use Self::get
in case you want to have access to the type
or Self::get_mut
if you also need to mutate it.
pub fn get<T>(&self) -> Option<&T>
pub fn get<T>(&self) -> Option<&T>
Get a shared reference to an extension.
An extension is a type that implements Send + Sync + 'static
,
and can be used to inject dynamic data into the Context
.
Extensions are specific to this Context
. It will be cloned when the Context
is cloned,
but extensions inserted using Context::insert
will not be visible the
original Context
, or any other cloned Context
.
Please use the statically typed state (see [Context::state
]) for data that is shared between
all context clones.
§Example
assert_eq!(ctx.get::<i32>(), Some(&5i32));
pub fn get_mut<T>(&mut self) -> Option<&mut T>
pub fn get_mut<T>(&mut self) -> Option<&mut T>
Get an exclusive reference to an extension.
An extension is a type that implements Send + Sync + 'static
,
and can be used to inject dynamic data into the Context
.
Extensions are specific to this Context
. It will be cloned when the Context
is cloned,
but extensions inserted using Context::insert
will not be visible the
original Context
, or any other cloned Context
.
Please use the statically typed state (see [Context::state
]) for data that is shared between
all context clones.
§Example
let x = ctx.get_mut::<i32>().unwrap();
assert_eq!(*x, 5i32);
*x = 8;
assert_eq!(*x, 8i32);
assert_eq!(ctx.get::<i32>(), Some(&8i32));
pub fn get_or_insert_with<T>(&mut self, f: impl FnOnce() -> T) -> &mut T
pub fn get_or_insert_with<T>(&mut self, f: impl FnOnce() -> T) -> &mut T
Inserts a value into the map computed from f
into if it is None
,
then returns an exclusive reference to the contained value.
§Example
let mut ctx = Context::default();
let value: &i32 = ctx.get_or_insert_with(|| 42);
assert_eq!(*value, 42);
let existing_value: &mut i32 = ctx.get_or_insert_with(|| 0);
assert_eq!(*existing_value, 42);
pub fn get_or_insert_with_ctx<T>(
&mut self,
f: impl FnOnce(&Context) -> T,
) -> &mut T
pub fn get_or_insert_with_ctx<T>( &mut self, f: impl FnOnce(&Context) -> T, ) -> &mut T
Inserts a value into the map computed from f
into if it is None
,
then returns an exclusive reference to the contained value.
Use the cheaper Self::get_or_insert_with
in case you do not need access to
the Context
for the creation of T
, as this function comes with
an extra cost.
§Example
struct State {
mul: i32,
}
let mut ctx = Context::default();
ctx.insert(State{ mul: 2 });
let value: &i32 = ctx.get_or_insert_with_ctx(|ctx| ctx.get::<State>().unwrap().mul * 21);
assert_eq!(*value, 42);
let existing_value: &mut i32 = ctx.get_or_insert_default();
assert_eq!(*existing_value, 42);
pub fn get_or_try_insert_with_ctx<T, E>(
&mut self,
f: impl FnOnce(&Context) -> Result<T, E>,
) -> Result<&mut T, E>
pub fn get_or_try_insert_with_ctx<T, E>( &mut self, f: impl FnOnce(&Context) -> Result<T, E>, ) -> Result<&mut T, E>
Try to insert a value into the map computed from f
into if it is None
,
then returns an exclusive reference to the contained value.
Similar to Self::get_or_insert_with_ctx
but fallible.
pub fn get_or_insert_from<T, U>(&mut self, src: U) -> &mut T
pub fn get_or_insert_from<T, U>(&mut self, src: U) -> &mut T
Inserts a value into the map computed from converting U
into T if no value was already inserted is [
None`],
then returns an exclusive reference to the contained value.
pub fn get_or_insert<T>(&mut self, fallback: T) -> &mut T
pub fn get_or_insert<T>(&mut self, fallback: T) -> &mut T
Retrieves a value of type T
from the context.
If the value does not exist, the provided value is inserted and an exclusive reference to it is returned.
See Context::get
for more details.
§Example
let mut ctx = Context::default();
ctx.insert(5i32);
assert_eq!(*ctx.get_or_insert::<i32>(10), 5);
assert_eq!(*ctx.get_or_insert::<f64>(2.5), 2.5);
pub fn get_or_insert_default<T>(&mut self) -> &mut T
pub fn get_or_insert_default<T>(&mut self) -> &mut T
Get an extension or T
’s Default
.
See Context::get
for more details.
§Example
assert_eq!(*ctx.get_or_insert_default::<i32>(), 5i32);
assert_eq!(*ctx.get_or_insert_default::<f64>(), 0f64);
pub fn insert<T>(&mut self, extension: T) -> Option<T>
pub fn insert<T>(&mut self, extension: T) -> Option<T>
Insert an extension into the Context
.
If a extension of this type already existed, it will be returned.
See Context::get
for more details regarding extensions.
§Example
let mut ctx = Context::default();
assert_eq!(ctx.insert(5i32), None);
assert_eq!(ctx.get::<i32>(), Some(&5i32));
assert_eq!(ctx.insert(4i32), Some(5i32));
assert_eq!(ctx.get::<i32>(), Some(&4i32));
pub fn maybe_insert<T>(&mut self, extension: Option<T>) -> Option<T>
pub fn maybe_insert<T>(&mut self, extension: Option<T>) -> Option<T>
Insert a type only into this Context
, if the extension is Some(T)
.
See Self::insert
for more information.
pub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Return the entire dynamic state of the Context
by reference.
Useful only in case you have a function which works with Extensions
rather
then the Context
itself. In case you want to have access to a specific dynamic state,
it is more suitable to use Context::get
directly.
pub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Return the entire dynamic state of the Context
by mutable reference.
Useful only in case you have a function which works with Extensions
rather
then the Context
itself. In case you want to have access to a specific dynamic state,
it is more suitable to use Context::get_or_insert
or Context::insert
directly.
§Rollback
Extensions do not have “rollback” support. In case you are not yet certain if you want to keep
the to be inserted Extensions
, you are better to create a new Extensions
object using
Extensions::default
and use Context::extend
once you wish to commit the new
dynamic data into the Context
.
pub fn extend(&mut self, extensions: Extensions)
pub fn extend(&mut self, extensions: Extensions)
Extend The Context
Extensions
with another Extensions
.
§Example
let mut ctx = Context::default();
let mut ext = Extensions::default();
ctx.insert(true);
ext.insert(5i32);
ctx.extend(ext);
assert_eq!(ctx.get::<bool>(), Some(&true));
assert_eq!(ctx.get::<i32>(), Some(&5i32));
pub fn clear(&mut self)
pub fn clear(&mut self)
Clear the Context
of all inserted Extensions
.
§Example
let mut ctx = Context::default();
ctx.insert(5i32);
assert_eq!(ctx.get::<i32>(), Some(&5i32));
ctx.clear();
assert_eq!(ctx.get::<i32>(), None);
pub fn guard(&self) -> Option<&ShutdownGuard>
pub fn guard(&self) -> Option<&ShutdownGuard>
Get a reference to the shutdown guard, if and only if the context was created within a graceful environment.
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§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§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§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy
that returns Action::Follow
only if self
and other
return
Action::Follow
. Read more