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

pub fn new(executor: Executor) -> Context

Create a new Context with the given state.

pub fn from_parts(parts: Parts) -> Context

pub fn into_parts(self) -> Parts

pub fn executor(&self) -> &Executor

Get a reference to the executor.

pub fn set_executor(&mut self, exec: Executor) -> &mut Context

Set a new Executor to the Context.

pub fn with_executor(self, exec: Executor) -> Context

Set a new Executor to the Context.

pub fn spawn<F>(&self, future: F) -> JoinHandle<<F as Future>::Output>
where F: Future + Send + 'static, <F as Future>::Output: Send + 'static,

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
where T: Send + Sync + 'static,

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>
where T: Send + Sync + 'static,

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>
where T: Send + Sync + 'static,

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
where T: Clone + Send + Sync + 'static,

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
where T: Clone + Send + Sync + 'static,

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>
where T: Clone + Send + Sync + 'static,

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
where T: Clone + Send + Sync + 'static, U: Into<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
where T: Send + Sync + Clone + 'static,

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
where T: Clone + Default + Send + Sync + 'static,

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>
where T: Clone + Send + Sync + 'static,

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>
where T: Clone + Send + Sync + 'static,

Insert a type only into this Context, if the extension is Some(T).

See Self::insert for more information.

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

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)

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)

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 remove<T>(&mut self) -> Option<T>
where T: Clone + Send + Sync + 'static,

Remove an extension from this Context

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§

§

impl Clone for Context

§

fn clone(&self) -> Context

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Context

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Default for Context

§

fn default() -> Context

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

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

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

fn in_current_span(self) -> Instrumented<Self>

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

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

Source§

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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
§

impl<T, U> RamaInto<U> for T
where U: RamaFrom<T>,

§

fn rama_into(self) -> U

§

impl<T, U> RamaInto<U> for T
where U: RamaFrom<T>,

§

fn rama_into(self) -> U

§

impl<T, U> RamaTryInto<U> for T
where U: RamaTryFrom<T>,

§

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

§

fn rama_try_into(self) -> Result<U, <U as RamaTryFrom<T>>::Error>

§

impl<T, U> RamaTryInto<U> for T
where U: RamaTryFrom<T>,

§

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

§

fn rama_try_into(self) -> Result<U, <U as RamaTryFrom<T>>::Error>

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> ErasedDestructor for T
where T: 'static,