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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use ;
/// Calls two services in sequence, passing the first response to the second.
///
/// Created by [`Service::and_then`] and [`crate::ServiceChain::and_then`].
/// Both services use the same state and error types. The first service accepts
/// the input request, and the second service produces the final response.
/// If the first call fails, its error is returned without calling the second
/// service. Errors from the second call are also returned unchanged.
///
/// To pass both successful responses and errors to the next service, use
/// [`crate::ServiceChain::then`] instead.
///
/// # Readiness and lifecycle
///
/// Binding is forwarded to both children. Readiness succeeds only when both
/// children are ready; a readiness error from either child is returned
/// unchanged. Shutdown drives both children's shutdown futures to completion.
///
/// Children share the enclosing readiness scope unless explicitly wrapped
/// with [`crate::ready()`]. Calls forward directly to each child without
/// another readiness check for raw services. Wrap a child when it needs an
/// independent readiness cache and a check at call time, especially when the
/// second child's readiness can change while the first call is awaiting.
///
/// Cloning clones both children; any shared state or readiness depends on
/// their clone implementations.
///
/// # Example
///
/// ```
/// use ntex_service::{Service, fn_service};
///
/// # async fn example() -> Result<(), ()> {
/// let pipeline = fn_service(async |req: u32| Ok::<_, ()>(req + 1))
/// .and_then(fn_service(async |value: u32| Ok::<_, ()>(value.to_string())))
/// .pipeline(());
///
/// assert_eq!(pipeline.call(41).await?, "42");
/// # Ok(())
/// # }
/// ```
///
/// See the framework guide's [Service Pipelines](https://docs.ntex.rs/2-pipeline.html)
/// chapter for composition and readiness semantics.
/// Creates an [`AndThen`] service from two service factories.
///
/// Created by [`ServiceFactory::and_then`],
/// [`crate::ServiceChainFactory::and_then`], or [`Self::new`].
/// Both factories use the same state, service-error, and initialization-error
/// types. The first factory's response type is the second factory's request
/// type.
///
/// Creation is sequential: the first factory completes before the second
/// starts, and both receive the same state reference. If the first factory
/// fails, the second is not invoked. Initialization errors from either
/// factory are returned unchanged.
///
/// Created services use the call, binding, readiness, and shutdown behavior
/// described in [`AndThen`]. Wrap a child factory with [`crate::ready_factory()`]
/// to give each of its created services an independent readiness cache.
///
/// Cloning clones both factories without creating any services.