1use crate::{
2 di::{Asyncify, Injectable},
3 send::{MaybeSend, MaybeSync},
4 Fallible, Handler, HandlerDescription,
5};
6
7impl<'a, Output, Descr> Handler<'a, Output, Descr>
8where
9 Output: 'a,
10 Descr: HandlerDescription,
11{
12 #[must_use]
14 #[track_caller]
15 pub fn filter<Pred, FnArgs>(self, pred: Pred) -> Handler<'a, Output, Descr>
16 where
17 Asyncify<Pred>: Injectable<bool, FnArgs> + MaybeSend + MaybeSync + 'a,
18 {
19 self.chain(crate::filter(pred))
20 }
21
22 #[must_use]
24 #[track_caller]
25 pub fn filter_async<Pred, FnArgs>(self, pred: Pred) -> Handler<'a, Output, Descr>
26 where
27 Pred: Injectable<bool, FnArgs> + MaybeSend + MaybeSync + 'a,
28 {
29 self.chain(crate::filter_async(pred))
30 }
31
32 #[must_use]
34 #[track_caller]
35 pub fn filter_map<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Output, Descr>
36 where
37 Asyncify<Proj>: Injectable<Option<NewType>, Args> + MaybeSend + MaybeSync + 'a,
38 NewType: Send + Sync + 'static,
39 {
40 self.chain(crate::filter_map(proj))
41 }
42
43 #[must_use]
45 #[track_caller]
46 pub fn filter_map_async<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Output, Descr>
47 where
48 Proj: Injectable<Option<NewType>, Args> + MaybeSend + MaybeSync + 'a,
49 NewType: Send + Sync + 'static,
50 {
51 self.chain(crate::filter_map_async(proj))
52 }
53
54 #[must_use]
56 #[track_caller]
57 pub fn map<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Output, Descr>
58 where
59 Asyncify<Proj>: Injectable<NewType, Args> + MaybeSend + MaybeSync + 'a,
60 NewType: Send + Sync + 'static,
61 {
62 self.chain(crate::map(proj))
63 }
64
65 #[must_use]
67 #[track_caller]
68 pub fn map_async<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Output, Descr>
69 where
70 Proj: Injectable<NewType, Args> + MaybeSend + MaybeSync + 'a,
71 NewType: Send + Sync + 'static,
72 {
73 self.chain(crate::map_async(proj))
74 }
75
76 #[must_use]
78 #[track_caller]
79 pub fn inspect<F, Args>(self, f: F) -> Handler<'a, Output, Descr>
80 where
81 Asyncify<F>: Injectable<(), Args> + MaybeSend + MaybeSync + 'a,
82 {
83 self.chain(crate::inspect(f))
84 }
85
86 #[must_use]
88 #[track_caller]
89 pub fn inspect_async<F, Args>(self, f: F) -> Handler<'a, Output, Descr>
90 where
91 F: Injectable<(), Args> + MaybeSend + MaybeSync + 'a,
92 {
93 self.chain(crate::inspect_async(f))
94 }
95
96 #[must_use]
98 #[track_caller]
99 pub fn endpoint<F, FnArgs>(self, f: F) -> Handler<'a, Output, Descr>
100 where
101 F: Injectable<Output, FnArgs> + MaybeSend + MaybeSync + 'a,
102 Output: 'static,
103 {
104 self.chain(crate::endpoint(f))
105 }
106
107 #[must_use]
116 #[track_caller]
117 pub fn try_filter<Pred, FnArgs>(self, pred: Pred) -> Handler<'a, Output, Descr>
118 where
119 Asyncify<Pred>:
120 Injectable<Result<bool, Output::Error>, FnArgs> + MaybeSend + MaybeSync + 'a,
121 Output: Fallible,
122 Output::Error: MaybeSend,
123 {
124 self.chain(crate::try_filter(pred))
125 }
126
127 #[must_use]
131 #[track_caller]
132 pub fn try_filter_async<Pred, FnArgs>(self, pred: Pred) -> Handler<'a, Output, Descr>
133 where
134 Pred: Injectable<Result<bool, Output::Error>, FnArgs> + MaybeSend + MaybeSync + 'a,
135 Output: Fallible,
136 Output::Error: MaybeSend,
137 {
138 self.chain(crate::try_filter_async(pred))
139 }
140
141 #[must_use]
150 #[track_caller]
151 pub fn try_filter_map<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Output, Descr>
152 where
153 Asyncify<Proj>:
154 Injectable<Result<Option<NewType>, Output::Error>, Args> + MaybeSend + MaybeSync + 'a,
155 Output: Fallible,
156 Output::Error: MaybeSend,
157 NewType: Send + Sync + 'static,
158 {
159 self.chain(crate::try_filter_map(proj))
160 }
161
162 #[must_use]
166 #[track_caller]
167 pub fn try_filter_map_async<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Output, Descr>
168 where
169 Proj: Injectable<Result<Option<NewType>, Output::Error>, Args> + MaybeSend + MaybeSync + 'a,
170 Output: Fallible,
171 Output::Error: MaybeSend,
172 NewType: Send + Sync + 'static,
173 {
174 self.chain(crate::try_filter_map_async(proj))
175 }
176
177 #[must_use]
184 #[track_caller]
185 pub fn try_map<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Output, Descr>
186 where
187 Asyncify<Proj>:
188 Injectable<Result<NewType, Output::Error>, Args> + MaybeSend + MaybeSync + 'a,
189 Output: Fallible,
190 Output::Error: MaybeSend,
191 NewType: Send + Sync + 'static,
192 {
193 self.chain(crate::try_map(proj))
194 }
195
196 #[must_use]
200 #[track_caller]
201 pub fn try_map_async<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Output, Descr>
202 where
203 Proj: Injectable<Result<NewType, Output::Error>, Args> + MaybeSend + MaybeSync + 'a,
204 Output: Fallible,
205 Output::Error: MaybeSend,
206 NewType: Send + Sync + 'static,
207 {
208 self.chain(crate::try_map_async(proj))
209 }
210}
211
212#[cfg(test)]
213mod tests {
214 use std::ops::ControlFlow;
215
216 use crate::{deps, help_inference};
217
218 crate::cross_test! {
219 async fn test_methods() {
221 let value = 42;
222
223 let _: ControlFlow<(), _> =
224 help_inference(crate::entry()).filter(|| true).dispatch(deps![value]).await;
225
226 let _: ControlFlow<(), _> = help_inference(crate::entry())
227 .filter_async(|| async { true })
228 .dispatch(deps![value])
229 .await;
230
231 let _: ControlFlow<(), _> =
232 help_inference(crate::entry()).filter_map(|| Some("abc")).dispatch(deps![value]).await;
233
234 let _: ControlFlow<(), _> = help_inference(crate::entry())
235 .filter_map_async(|| async { Some("abc") })
236 .dispatch(deps![value])
237 .await;
238
239 let _: ControlFlow<(), _> =
240 help_inference(crate::entry()).map(|| "abc").dispatch(deps![value]).await;
241
242 let _: ControlFlow<(), _> = help_inference(crate::entry())
243 .map_async(|| async { "abc" })
244 .dispatch(deps![value])
245 .await;
246
247 let _: ControlFlow<(), _> =
248 help_inference(crate::entry()).inspect(|| {}).dispatch(deps![value]).await;
249
250 let _: ControlFlow<(), _> =
251 help_inference(crate::entry()).inspect_async(|| async {}).dispatch(deps![value]).await;
252
253 let _: ControlFlow<(), _> =
254 help_inference(crate::entry()).endpoint(|| async {}).dispatch(deps![value]).await;
255
256 let _: ControlFlow<Result<(), &str>, _> = help_inference(crate::entry())
258 .try_filter(|| Ok::<bool, &str>(true))
259 .dispatch(deps![value])
260 .await;
261
262 let _: ControlFlow<Result<(), &str>, _> = help_inference(crate::entry())
263 .try_filter_async(|| async { Ok::<bool, &str>(true) })
264 .dispatch(deps![value])
265 .await;
266
267 let _: ControlFlow<Result<(), &str>, _> = help_inference(crate::entry())
268 .try_filter_map(|| Ok::<Option<()>, &str>(Some(())))
269 .dispatch(deps![value])
270 .await;
271
272 let _: ControlFlow<Result<(), &str>, _> = help_inference(crate::entry())
273 .try_filter_map_async(|| async { Ok::<Option<()>, &str>(Some(())) })
274 .dispatch(deps![value])
275 .await;
276
277 let _: ControlFlow<Result<(), &str>, _> = help_inference(crate::entry())
278 .try_map(|| Ok::<(), &str>(()))
279 .dispatch(deps![value])
280 .await;
281
282 let _: ControlFlow<Result<(), &str>, _> = help_inference(crate::entry())
283 .try_map_async(|| async { Ok::<(), &str>(()) })
284 .dispatch(deps![value])
285 .await;
286 }
287 }
288}