Skip to main content

dptree/handler/
methods.rs

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    /// Chain this handler with the filter predicate `pred`.
13    #[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    /// Chain this handler with the async filter predicate `pred`.
23    #[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    /// Chain this handler with the filter projection `proj`.
33    #[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    /// Chain this handler with the async filter projection `proj`.
44    #[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    /// Chain this handler with the map projection `proj`.
55    #[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    /// Chain this handler with the async map projection `proj`.
66    #[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    /// Chain this handler with the inspection function `f`.
77    #[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    /// Chain this handler with the async inspection function `f`.
87    #[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    /// Chain this handler with the endpoint handler `f`.
97    #[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    /// Chain this handler with the fallible filter predicate `pred`.
108    ///
109    /// `pred` returns [`Result<bool, E>`] (where `E` is the error type of this
110    /// handler's `Output`). On `Ok(true)` execution continues; on `Ok(false)`
111    /// the handler returns [`ControlFlow::Continue`](std::ops::ControlFlow::Continue)
112    /// (try the next branch); on `Err(e)` the handler short-circuits with
113    /// [`ControlFlow::Break`](std::ops::ControlFlow::Break) carrying
114    /// the error, without falling through to sibling branches.
115    #[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    /// Chain this handler with the async fallible filter predicate `pred`.
128    ///
129    /// See [`try_filter`](Handler::try_filter).
130    #[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    /// Chain this handler with the fallible filter projection `proj`.
142    ///
143    /// `proj` returns [`Result<Option<NewType>, E>`]. On `Ok(Some(v))` `v` is
144    /// inserted into the container and execution continues; on `Ok(None)` the
145    /// handler returns [`ControlFlow::Continue`](std::ops::ControlFlow::Continue)
146    /// (try the next branch); on `Err(e)` the handler short-circuits with
147    /// [`ControlFlow::Break`](std::ops::ControlFlow::Break) carrying
148    /// the error.
149    #[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    /// Chain this handler with the async fallible filter projection `proj`.
163    ///
164    /// See [`try_filter_map`](Handler::try_filter_map).
165    #[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    /// Chain this handler with the fallible map projection `proj`.
178    ///
179    /// `proj` returns [`Result<NewType, E>`]. On `Ok(v)` `v` is inserted into
180    /// the container and execution continues; on `Err(e)` the handler
181    /// short-circuits with [`ControlFlow::Break`](std::ops::ControlFlow::Break)
182    /// carrying the error, without calling the continuation.
183    #[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    /// Chain this handler with the async fallible map projection `proj`.
197    ///
198    /// See [`try_map`](Handler::try_map).
199    #[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        // Test that these methods just do compile.
220        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            // Fallible handlers require a fallible (`Result` in here) output.
257            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}