Skip to main content

BindCtx

Struct BindCtx 

Source
pub struct BindCtx<'a> { /* private fields */ }
Expand description

Initialization-only access to a service’s readiness dependencies.

A pipeline or crate::Ctx::create supplies this borrowed context to Service::bind before the new service’s readiness checks, calls, or shutdown. Subscribe every notification source and bind every existing child used by the service, including call-only children. Dependencies must be available from the service itself. Binding cannot perform runtime operations.

The context cannot be constructed by services or retained for later use. Runtime crate::Ctx cannot register dependencies directly; its explicit create method initializes and binds new children.

use ntex_service::{BindCtx, Ctx, ReadyCallback, ReadyNotifier, Service};

struct Forward<S> {
    inner: S,
    changed: ReadyNotifier,
    on_notify: ReadyCallback,
}

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

    fn bind(&self, ctx: BindCtx<'_>) {
        ctx.subscribe(&self.changed);
        ctx.on_notify(&self.on_notify);
        ctx.bind(&self.inner);
    }

    async fn ready(&self, ctx: Ctx<'_, Self>) -> Result<(), Self::Error> {
        ctx.ready(&self.inner).await
    }

    async fn call(&self, req: u8, ctx: Ctx<'_, Self>) -> Result<Self::Res, Self::Error> {
        ctx.call(&self.inner, req).await
    }
}
ⓘ
use ntex_service::{Ctx, ReadyNotifier};
fn register_later(ctx: Ctx<'_, ()>, changed: &ReadyNotifier) {
    ctx.subscribe(changed);
}
ⓘ
use ntex_service::{BindCtx, Ctx, Service};
fn bind_later<S: Service<(), ()>>(ctx: Ctx<'_, ()>, service: &S) {
    service.bind(ctx);
}
ⓘ
use ntex_service::{BindCtx, Ctx, Service};
struct Retained(std::cell::RefCell<Option<BindCtx<'static>>>);
impl Service<(), ()> for Retained {
    type Res = ();
    type Error = ();
    fn bind(&self, ctx: BindCtx<'_>) {
        *self.0.borrow_mut() = Some(ctx);
    }
    async fn call(&self, _: (), _: Ctx<'_, Self>) -> Result<(), ()> {
        Ok(())
    }
}

Implementations§

Source§

impl<'a> BindCtx<'a>

Source

pub fn subscribe(&self, source: &ReadyNotifier)

Subscribes this scope to a source of readiness changes.

Subscriptions are appended without deduplication and persist across completed or cancelled checks. Repeated subscriptions cause repeated invalidations and callback delivery. Entries for dropped scopes are ignored and remain stored until the source is dropped.

§Panics

Panics if the source is currently delivering a notification.

Source

pub fn subscribe_pipeline(&self, link: &ReadyLink)

Subscribes this scope to another pipeline’s readiness invalidations.

Obtain the endpoint with crate::Pipeline::readiness_link. Repeated registration is idempotent and survives dropping the link handle. This does not run a readiness check: the service must still await the source pipeline’s readiness. A dropped source makes the link inert.

Source

pub fn on_notify(&self, callback: &ReadyCallback)

Registers a callback for notifications reaching this readiness scope.

Raw services share this scope; a crate::ReadyBinding owns its own. Repeated registration of the same handle is idempotent. See ReadyCallback for notification and lifetime semantics. The callback receives this scope’s data from crate::ReadyBinding::with_data, or 0 by default.

§Panics

Panics if this scope is currently executing notification callbacks.

Source

pub fn bind<S, St, Req>(&self, service: &S)
where S: Service<St, Req>,

Binds a child service to this readiness scope.

Raw services share this scope. A crate::ReadyBinding uses its own scope and registers it as a dependency of this one. Use explicit type arguments when the child has multiple Service implementations. For pipeline children, use crate::Pipeline::bind_to instead.

Trait Implementations§

Source§

impl<'a> Debug for BindCtx<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for BindCtx<'a>

§

impl<'a> !Send for BindCtx<'a>

§

impl<'a> !Sync for BindCtx<'a>

§

impl<'a> !UnwindSafe for BindCtx<'a>

§

impl<'a> Freeze for BindCtx<'a>

§

impl<'a> Unpin for BindCtx<'a>

§

impl<'a> UnsafeUnpin for BindCtx<'a>

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