pub struct Context<S> { /* private fields */ }
Expand description
Context passed to and between services as input.
See crate::context
for more information.
Implementations§
§impl<S> Context<S>
impl<S> Context<S>
pub fn with_state(state: S) -> Context<S>
pub fn with_state(state: S) -> Context<S>
Create a new Context
with the given state and default extension.
pub fn map_state<F, W>(self, f: F) -> Context<W>where
F: FnOnce(S) -> W,
pub fn map_state<F, W>(self, f: F) -> Context<W>where
F: FnOnce(S) -> W,
Map the state from one type to another.
pub fn swap_state<W>(self, state: W) -> (Context<W>, S)
pub fn swap_state<W>(self, state: W) -> (Context<W>, S)
Swap the state from one type to another, returning the new object as well as the previously defined state.
pub fn clone_map_state<F, W>(&self, f: F) -> Context<W>
pub fn clone_map_state<F, W>(&self, f: F) -> Context<W>
Clones the internals of this Context
to provide a new context, but while mapping the state
into a new state.
pub fn clone_with_state<W>(&self, state: W) -> Context<W>
pub fn clone_with_state<W>(&self, state: W) -> Context<W>
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<S>) -> T,
) -> &mut T
pub fn get_or_insert_with_ctx<T>( &mut self, f: impl FnOnce(&Context<S>) -> 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::with_state(Arc::new(State{ mul: 2 }));
ctx.insert(true);
let value: &i32 = ctx.get_or_insert_with_ctx(|ctx| ctx.state().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<S>) -> Result<T, E>,
) -> Result<&mut T, E>
pub fn get_or_try_insert_with_ctx<T, E>( &mut self, f: impl FnOnce(&Context<S>) -> 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.
§impl<S> Context<S>where
S: Clone,
impl<S> Context<S>where
S: Clone,
pub fn state_clone(&self) -> S
pub fn state_clone(&self) -> S
Get a cloned reference to the state.
Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for Context<S>where
S: Freeze,
impl<S> !RefUnwindSafe for Context<S>
impl<S> Send for Context<S>where
S: Send,
impl<S> Sync for Context<S>where
S: Sync,
impl<S> Unpin for Context<S>where
S: Unpin,
impl<S> !UnwindSafe for Context<S>
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§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> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<S, P, B, E>(self, other: P) -> And<T, P>
fn and<S, P, B, E>(self, other: P) -> And<T, P>
Policy
that returns Action::Follow
only if self
and other
return
Action::Follow
. Read more§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.