Skip to main content

dptree/handler/
description.rs

1//! Built-in handler description types.
2
3mod interest_set;
4mod unspecified;
5
6use crate::send::{MaybeSend, MaybeSync};
7
8pub use interest_set::{EventKind, InterestSet};
9pub use unspecified::Unspecified;
10
11/// Handler description.
12///
13/// This trait allows information to flow "back up" the tree, allowing a user to
14/// check its structure.
15///
16/// ## Examples
17///
18/// Count how many branches are in the tree:
19///
20/// ```
21/// use dptree::{prelude::DependencyMap, Handler, HandlerDescription};
22///
23/// struct CountBranches(u32);
24///
25/// impl HandlerDescription for CountBranches {
26///     fn entry() -> Self {
27///         Self(0)
28///     }
29///
30///     fn user_defined() -> Self {
31///         Self(0)
32///     }
33///
34///     fn merge_chain(&self, other: &Self) -> Self {
35///         Self(self.0 + other.0)
36///     }
37///
38///     fn merge_branch(&self, other: &Self) -> Self {
39///         Self(self.0 + other.0 + 1)
40///     }
41/// }
42///
43/// #[track_caller]
44/// fn assert_count(count: u32, handler: Handler<(), CountBranches>) {
45///     assert_eq!(handler.description().0, count);
46/// }
47///
48/// assert_count(0, dptree::entry());
49/// assert_count(1, dptree::entry().branch(dptree::inspect(|| ())));
50/// assert_count(
51///     5,
52///     dptree::entry()
53///         .branch(
54///             dptree::entry()
55///                 .branch(dptree::entry().branch(dptree::filter(|| true)))
56///                 .branch(dptree::entry().chain(dptree::filter(|| false))),
57///         )
58///         .branch(dptree::inspect(|| ())),
59/// );
60/// ```
61pub trait HandlerDescription: Sized + MaybeSend + MaybeSync + 'static {
62    /// Description for [`entry`](crate::entry).
63    fn entry() -> Self;
64
65    /// Description for a user-defined handler that can do practically
66    /// everything.
67    fn user_defined() -> Self;
68
69    /// Merge descriptions to get a description for a chain handler.
70    fn merge_chain(&self, other: &Self) -> Self;
71
72    /// Merge descriptions to get a description for a branch handler.
73    fn merge_branch(&self, other: &Self) -> Self;
74
75    /// Description for [`map`](crate::map).
76    ///
77    /// ## Default implementation
78    ///
79    /// By default this returns the value from
80    /// [`user_defined`](HandlerDescription::user_defined).
81    #[track_caller]
82    fn map() -> Self {
83        Self::user_defined()
84    }
85
86    /// Description for [`map_async`](crate::map_async).
87    ///
88    /// ## Default implementation
89    ///
90    /// By default this returns the value from
91    /// [`user_defined`](HandlerDescription::user_defined).
92    #[track_caller]
93    fn map_async() -> Self {
94        Self::user_defined()
95    }
96
97    /// Description for [`filter`](crate::filter).
98    ///
99    /// ## Default implementation
100    ///
101    /// By default this returns the value from
102    /// [`user_defined`](HandlerDescription::user_defined).
103    #[track_caller]
104    fn filter() -> Self {
105        Self::user_defined()
106    }
107
108    /// Description for [`filter_async`](crate::filter_async).
109    ///
110    /// ## Default implementation
111    ///
112    /// By default this returns the value from
113    /// [`user_defined`](HandlerDescription::user_defined).
114    #[track_caller]
115    fn filter_async() -> Self {
116        Self::user_defined()
117    }
118
119    /// Description for [`filter_map`](crate::filter_map).
120    ///
121    /// ## Default implementation
122    ///
123    /// By default this returns the value from
124    /// [`user_defined`](HandlerDescription::user_defined).
125    #[track_caller]
126    fn filter_map() -> Self {
127        Self::user_defined()
128    }
129
130    /// Description for [`filter_map_async`](crate::filter_map_async).
131    ///
132    /// ## Default implementation
133    ///
134    /// By default this returns the value from
135    /// [`user_defined`](HandlerDescription::user_defined).
136    #[track_caller]
137    fn filter_map_async() -> Self {
138        Self::user_defined()
139    }
140
141    /// Description for [`inspect`](crate::inspect).
142    ///
143    /// ## Default implementation
144    ///
145    /// By default this returns the value from
146    /// [`user_defined`](HandlerDescription::user_defined).
147    #[track_caller]
148    fn inspect() -> Self {
149        Self::user_defined()
150    }
151
152    /// Description for [`inspect_async`](crate::inspect_async).
153    ///
154    /// ## Default implementation
155    ///
156    /// By default this returns the value from
157    /// [`user_defined`](HandlerDescription::user_defined).
158    #[track_caller]
159    fn inspect_async() -> Self {
160        Self::user_defined()
161    }
162
163    /// Description for [`endpoint`](crate::endpoint).
164    ///
165    /// ## Default implementation
166    ///
167    /// By default this returns the value from
168    /// [`user_defined`](HandlerDescription::user_defined).
169    #[track_caller]
170    fn endpoint() -> Self {
171        Self::user_defined()
172    }
173
174    /// Description for [`try_filter`](crate::try_filter).
175    ///
176    /// ## Default implementation
177    ///
178    /// By default this returns the value from
179    /// [`user_defined`](HandlerDescription::user_defined).
180    #[track_caller]
181    fn try_filter() -> Self {
182        Self::user_defined()
183    }
184
185    /// Description for [`try_filter_async`](crate::try_filter_async).
186    ///
187    /// ## Default implementation
188    ///
189    /// By default this returns the value from
190    /// [`user_defined`](HandlerDescription::user_defined).
191    #[track_caller]
192    fn try_filter_async() -> Self {
193        Self::user_defined()
194    }
195
196    /// Description for [`try_filter_map`](crate::try_filter_map).
197    ///
198    /// ## Default implementation
199    ///
200    /// By default this returns the value from
201    /// [`user_defined`](HandlerDescription::user_defined).
202    #[track_caller]
203    fn try_filter_map() -> Self {
204        Self::user_defined()
205    }
206
207    /// Description for [`try_filter_map_async`](crate::try_filter_map_async).
208    ///
209    /// ## Default implementation
210    ///
211    /// By default this returns the value from
212    /// [`user_defined`](HandlerDescription::user_defined).
213    #[track_caller]
214    fn try_filter_map_async() -> Self {
215        Self::user_defined()
216    }
217
218    /// Description for [`try_map`](crate::try_map).
219    ///
220    /// ## Default implementation
221    ///
222    /// By default this returns the value from
223    /// [`user_defined`](HandlerDescription::user_defined).
224    #[track_caller]
225    fn try_map() -> Self {
226        Self::user_defined()
227    }
228
229    /// Description for [`try_map_async`](crate::try_map_async).
230    ///
231    /// ## Default implementation
232    ///
233    /// By default this returns the value from
234    /// [`user_defined`](HandlerDescription::user_defined).
235    #[track_caller]
236    fn try_map_async() -> Self {
237        Self::user_defined()
238    }
239}