Skip to main content

Ctx

Struct Ctx 

Source
pub struct Ctx<'a, Svc: ?Sized, St = ()> { /* private fields */ }
Expand description

Context provided to Service lifecycle methods.

A context gives a service access to pipeline state and coordinates calls to inner services with the pipeline’s readiness and shutdown machinery. It borrows the current readiness scope from a pipeline or service binding. Copying or mapping the context preserves that scope; calling a binding selects the binding’s own scope without changing the caller’s context. Existing dependencies must be registered in Service::bind using BindCtx. Lazily created children can be initialized with Ctx::create; readiness checks, calls, and shutdown never register dependencies implicitly.

Implementations§

Source§

impl<'a, Svc, St> Ctx<'a, Svc, St>

Source

pub fn id(&self) -> u32

Returns the identifier of the current pipeline binding.

Contexts of calls made through a pipeline or a pipeline binding are not bound to a waiter slot and share a placeholder identifier.

Source

pub fn st(&self) -> &'a St

Returns the pipeline state.

Source

pub async fn create<Sf, Req>( &self, factory: &Sf, ) -> Result<Sf::Service, Sf::InitError>
where Sf: ServiceFactory<St, Req>,

Creates a child service and binds it to this context’s readiness scope.

Uses the current pipeline state to create the service, then invokes its Service::bind hook before returning it. Successful creation invalidates this scope’s cached readiness and its ancestors. Failed or cancelled creation does not bind a service or change this scope’s dependencies.

The service is returned unchanged, without checking readiness or adding a cache. Use crate::ready_factory for independent child readiness. Retain the created service for subsequent calls rather than recreating it on each readiness check. Other scopes must bind it separately before use.

Source

pub fn ready<'b, S, Req>( &self, svc: &'b S, ) -> impl Future<Output = Result<(), S::Error>> + use<'a, 'b, Svc, St, S, Req>
where S: Service<St, Req>,

Waits until svc is ready to process a request.

A crate::ReadyBinding reuses successful readiness until a subscribed source changes. A raw service is checked on every invocation.

Source

pub fn call<'b, S, Req>( &self, svc: &'b S, req: Req, ) -> impl Future<Output = Result<S::Res, S::Error>> + use<'a, 'b, Svc, St, S, Req>
where S: Service<St, Req>,

Waits for svc to become ready, then calls it.

Uses the nearest upstream scope carried by this context. A valid cache allows immediate dispatch; otherwise, only svc is checked, without marking the enclosing scope ready. A crate::ReadyBinding also checks its own cache when called directly, independently of this scope. Calls do not consume cached readiness.

Calling after an explicit readiness check is cheap: a valid cache avoids another readiness check or readiness allocation, while invalidation before dispatch is still observed. Raw Self::ready calls do not populate the upstream cache; use crate::ready() for independent child caching.

Readiness checks made while handling a call, including this one, do not wait for another binding’s pipeline-level readiness check, which may itself be waiting for resources held by the call. They may run concurrently with it.

use ntex_service::{Ctx, Service};

struct Forward<S> {
    inner: S,
}

impl<S: Service<(), usize>> Service<(), usize> for Forward<S> {
    type Res = S::Res;
    type Error = S::Error;

    ntex_service::forward!((), inner);

    async fn call(&self, req: usize, ctx: Ctx<'_, Self>) -> Result<S::Res, S::Error> {
        ctx.call(&self.inner, req).await
    }
}
Source

pub fn shutdown<'b, S, Req>( &self, svc: &'b S, ) -> impl Future<Output = ()> + use<'a, 'b, Svc, St, S, Req>
where S: Service<St, Req>,

Shuts down svc.

Source

pub fn map_state<NewSt>(&'a self, st: &'a NewSt) -> Ctx<'a, Self, NewSt>

Returns a context that uses st as its state.

Trait Implementations§

Source§

impl<S, St> Clone for Ctx<'_, S, St>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<S, St> Copy for Ctx<'_, S, St>

Source§

impl<S, St> Debug for Ctx<'_, S, St>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<S, St> Deref for Ctx<'_, S, St>

Source§

type Target = St

The resulting type after dereferencing.
Source§

fn deref(&self) -> &St

Dereferences the value.

Auto Trait Implementations§

§

impl<'a, Svc, St = ()> !RefUnwindSafe for Ctx<'a, Svc, St>

§

impl<'a, Svc, St = ()> !Send for Ctx<'a, Svc, St>

§

impl<'a, Svc, St = ()> !Sync for Ctx<'a, Svc, St>

§

impl<'a, Svc, St = ()> !UnwindSafe for Ctx<'a, Svc, St>

§

impl<'a, Svc, St> Freeze for Ctx<'a, Svc, St>
where Svc: ?Sized,

§

impl<'a, Svc, St> Unpin for Ctx<'a, Svc, St>
where Svc: ?Sized,

§

impl<'a, Svc, St> UnsafeUnpin for Ctx<'a, Svc, St>
where Svc: ?Sized,

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
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.

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.