1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::;
/// 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`](crate::Ctx::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
/// }
/// }
/// ```
///
/// ```compile_fail
/// use ntex_service::{Ctx, ReadyNotifier};
/// fn register_later(ctx: Ctx<'_, ()>, changed: &ReadyNotifier) {
/// ctx.subscribe(changed);
/// }
/// ```
///
/// ```compile_fail
/// use ntex_service::{BindCtx, Ctx, Service};
/// fn bind_later<S: Service<(), ()>>(ctx: Ctx<'_, ()>, service: &S) {
/// service.bind(ctx);
/// }
/// ```
///
/// ```compile_fail
/// 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(())
/// }
/// }
/// ```