stry_common/utils/fenn/
peep.rs

1// Based off https://www.reddit.com/r/rust/comments/6nmw07/method_naming_convention/dkbr285/
2
3/// This trait allows for 'peeping' at values in chained function calls.
4///
5/// ```rust
6/// use fenn::Peep;
7///
8/// let vec = vec![1, 2, 3]
9///     .into_iter()
10///     .map(|i| i * 2)
11///     .peep(|vec| {
12///         // This is only for the assert and isn't normally needed
13///         let temp_vec: Vec<_> = vec.clone().collect();
14///
15///          assert_eq!(temp_vec, [2, 4, 6]);
16///      })
17///     .map(|i| i / 2)
18///     .peep(|vec| {
19///         // Again this is only for the assert
20///         let temp_vec: Vec<_> = vec.clone().collect();
21///
22///          assert_eq!(temp_vec, [1, 2, 3]);
23///      })
24///     .collect::<Vec<usize>>();
25/// ```
26pub trait Peep: Sized {
27    fn peep<F, R>(self, run: F) -> Self
28    where
29        F: FnOnce(&Self) -> R,
30        R: Sized,
31    {
32        run(&self);
33
34        self
35    }
36
37    #[allow(unused_variables)]
38    fn peep_dbg<F, R>(self, run: F) -> Self
39    where
40        F: FnOnce(&Self) -> R,
41        R: Sized,
42    {
43        #[cfg(debug_assertions)]
44        return self.peep(run);
45
46        #[cfg(not(debug_assertions))]
47        return self;
48    }
49
50    fn peep_mut<F, R>(mut self, run: F) -> Self
51    where
52        F: FnOnce(&mut Self) -> R,
53        R: Sized,
54    {
55        run(&mut self);
56
57        self
58    }
59
60    #[allow(unused_variables)]
61    fn peep_mut_dbg<F, R>(self, run: F) -> Self
62    where
63        F: FnOnce(&mut Self) -> R,
64        R: Sized,
65    {
66        #[cfg(debug_assertions)]
67        return self.peep_mut(run);
68
69        #[cfg(not(debug_assertions))]
70        return self;
71    }
72}
73
74impl<T: Sized> Peep for T {}
75
76/// [`Peep`](./trait.Peep.html), but specifically for
77/// [`Option`](https://doc.rust-lang.org/std/option/enum.Option.html)'s.
78pub trait PeekOption<T: Sized>: Sized {
79    fn peep_some<F, R>(self, run: F) -> Self
80    where
81        F: FnOnce(&T) -> R,
82        R: Sized;
83
84    #[allow(unused_variables)]
85    fn peep_some_dbg<F, R>(self, run: F) -> Self
86    where
87        F: FnOnce(&T) -> R,
88        R: Sized,
89    {
90        #[cfg(debug_assertions)]
91        return self.peep_some(run);
92
93        #[cfg(not(debug_assertions))]
94        return self;
95    }
96
97    fn peep_some_mut<F, R>(self, run: F) -> Self
98    where
99        F: FnOnce(&mut T) -> R,
100        R: Sized;
101
102    #[allow(unused_variables)]
103    fn peep_some_mut_dbg<F, R>(self, run: F) -> Self
104    where
105        F: FnOnce(&mut T) -> R,
106        R: Sized,
107    {
108        #[cfg(debug_assertions)]
109        return self.peep_some_mut(run);
110
111        #[cfg(not(debug_assertions))]
112        return self;
113    }
114
115    fn peep_none<F, R>(self, run: F) -> Self
116    where
117        F: FnOnce() -> R,
118        R: Sized;
119
120    #[allow(unused_variables)]
121    fn peep_none_dbg<F, R>(self, run: F) -> Self
122    where
123        F: FnOnce() -> R,
124        R: Sized,
125    {
126        #[cfg(debug_assertions)]
127        return self.peep_none(run);
128
129        #[cfg(not(debug_assertions))]
130        return self;
131    }
132}
133
134impl<T: Sized> PeekOption<T> for Option<T> {
135    fn peep_some<F, R>(self, run: F) -> Self
136    where
137        F: FnOnce(&T) -> R,
138        R: Sized,
139    {
140        if let Some(inner) = self.as_ref() {
141            run(inner);
142        }
143
144        self
145    }
146
147    fn peep_some_mut<F, R>(mut self, run: F) -> Self
148    where
149        F: FnOnce(&mut T) -> R,
150        R: Sized,
151    {
152        if let Some(inner) = self.as_mut() {
153            run(inner);
154        }
155
156        self
157    }
158
159    fn peep_none<F, R>(self, run: F) -> Self
160    where
161        F: FnOnce() -> R,
162        R: Sized,
163    {
164        if self.is_none() {
165            run();
166        }
167
168        self
169    }
170}
171
172/// [`Peep`](./trait.Peep.html), but specifically for
173/// [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html)'s.
174pub trait PeepResult<T: Sized, E: Sized>: Sized {
175    fn peek_ok<F, R>(self, run: F) -> Self
176    where
177        F: FnOnce(&T) -> R,
178        R: Sized;
179
180    #[allow(unused_variables)]
181    fn peek_ok_dbg<F, R>(self, run: F) -> Self
182    where
183        F: FnOnce(&T) -> R,
184        R: Sized,
185    {
186        #[cfg(debug_assertions)]
187        return self.peek_ok(run);
188
189        #[cfg(not(debug_assertions))]
190        return self;
191    }
192
193    fn peek_ok_mut<F, R>(self, run: F) -> Self
194    where
195        F: FnOnce(&mut T) -> R,
196        R: Sized;
197
198    #[allow(unused_variables)]
199    fn peek_ok_mut_dbg<F, R>(self, run: F) -> Self
200    where
201        F: FnOnce(&mut T) -> R,
202        R: Sized,
203    {
204        #[cfg(debug_assertions)]
205        return self.peek_ok_mut(run);
206
207        #[cfg(not(debug_assertions))]
208        return self;
209    }
210
211    fn peek_err<F, R>(self, run: F) -> Self
212    where
213        F: FnOnce(&E) -> R,
214        R: Sized;
215
216    #[allow(unused_variables)]
217    fn peek_err_dbg<F, R>(self, run: F) -> Self
218    where
219        F: FnOnce(&E) -> R,
220        R: Sized,
221    {
222        #[cfg(debug_assertions)]
223        return self.peek_err(run);
224
225        #[cfg(not(debug_assertions))]
226        return self;
227    }
228
229    fn peek_err_mut<F, R>(self, run: F) -> Self
230    where
231        F: FnOnce(&mut E) -> R,
232        R: Sized;
233
234    #[allow(unused_variables)]
235    fn peek_err_mut_dbg<F, R>(self, run: F) -> Self
236    where
237        F: FnOnce(&mut E) -> R,
238        R: Sized,
239    {
240        #[cfg(debug_assertions)]
241        return self.peek_err_mut(run);
242
243        #[cfg(not(debug_assertions))]
244        return self;
245    }
246}
247
248impl<T: Sized, E: Sized> PeepResult<T, E> for Result<T, E> {
249    fn peek_ok<F, R>(self, run: F) -> Self
250    where
251        F: FnOnce(&T) -> R,
252        R: Sized,
253    {
254        if let Ok(inner) = self.as_ref() {
255            run(inner);
256        }
257
258        self
259    }
260
261    fn peek_ok_mut<F, R>(mut self, run: F) -> Self
262    where
263        F: FnOnce(&mut T) -> R,
264        R: Sized,
265    {
266        if let Ok(inner) = self.as_mut() {
267            run(inner);
268        }
269
270        self
271    }
272
273    fn peek_err<F, R>(self, run: F) -> Self
274    where
275        F: FnOnce(&E) -> R,
276        R: Sized,
277    {
278        if let Err(inner) = self.as_ref() {
279            run(inner);
280        }
281
282        self
283    }
284
285    fn peek_err_mut<F, R>(mut self, run: F) -> Self
286    where
287        F: FnOnce(&mut E) -> R,
288        R: Sized,
289    {
290        if let Err(inner) = self.as_mut() {
291            run(inner);
292        }
293
294        self
295    }
296}