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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use core::borrow::{Borrow, BorrowMut};
use core::ops::{Deref, DerefMut};
pub trait Tap
where
Self: Sized,
{
/// Immutable access to a value.
///
/// This function permits a value to be viewed by some inspecting function
/// without affecting the overall shape of the expression that contains this
/// method call. It is useful for attaching assertions or logging points
/// into a multi-part expression.
///
/// # Examples
///
/// Here we use `.tap()` to attach logging tracepoints to each stage of a
/// value-processing pipeline.
///
/// ```rust
/// use gearbox::rails::ext::blocking::Tap;
/// # struct Tmp;
/// # impl Tmp { fn process_value(self) -> Self { self } }
/// # fn make_value() -> Tmp { Tmp }
/// # macro_rules! log { ($msg:literal, $x:ident) => {{}}; }
///
/// let end = make_value()
/// // this line has no effect on the rest of the code
/// .tap(|v| log!("The produced value was: {}", v))
/// .process_value();
/// ```
#[inline(always)]
fn tap(self, func: impl FnOnce(&Self)) -> Self {
func(&self);
self
}
/// Mutable access to a value.
///
/// This function permits a value to be modified by some function without
/// affecting the overall shape of the expression that contains this method
/// call. It is useful for attaching modifier functions that have an
/// `&mut Self -> ()` signature to an expression, without requiring an
/// explicit `let mut` binding.
///
/// # Examples
///
/// Here we use `.tap_mut()` to sort an array without requring multiple
/// bindings.
///
/// ```rust
/// use gearbox::rails::ext::blocking::Tap;
///
/// let sorted = [1i32, 5, 2, 4, 3]
/// .tap_mut(|arr| arr.sort());
/// assert_eq!(sorted, [1, 2, 3, 4, 5]);
/// ```
///
/// Without tapping, this would be written as
///
/// ```rust
/// let mut received = [1, 5, 2, 4, 3];
/// received.sort();
/// let sorted = received;
/// ```
///
/// The mutable tap is a convenient alternative when the expression to
/// produce the collection is more complex, for example, an iterator
/// pipeline collected into a vector.
#[inline(always)]
fn tap_mut(mut self, func: impl FnOnce(&mut Self)) -> Self {
func(&mut self);
self
}
/// Immutable access to the `Borrow<B>` of a value.
///
/// This function is identcal to [`Tap::tap`], except that the effect
/// function recevies an `&B` produced by `Borrow::<B>::borrow`, rather than
/// an `&Self`.
///
/// [`Tap::tap`]: trait.Tap.html#method.tap
#[inline(always)]
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where
Self: Borrow<B>,
B: ?Sized,
{
func(Borrow::<B>::borrow(&self));
self
}
/// Mutable access to the `BorrowMut<B>` of a value.
///
/// This function is identical to [`Tap::tap_mut`], except that the effect
/// function receives an `&mut B` produced by `BorrowMut::<B>::borrow_mut`,
/// rather than an `&mut Self`.
///
/// [`Tap::tap_mut`]: trait.Tap.html#method.tap_mut
#[inline(always)]
fn tap_borrow_mut<B>(mut self, func: impl FnOnce(&mut B)) -> Self
where
Self: BorrowMut<B>,
B: ?Sized,
{
func(BorrowMut::<B>::borrow_mut(&mut self));
self
}
/// Immutable access to the `AsRef<R>` view of a value.
///
/// This function is identical to [`Tap::tap`], except that the effect
/// function receives an `&R` produced by `AsRef::<R>::as_ref`, rather than
/// an `&Self`.
///
/// [`Tap::tap`]: trait.Tap.html#method.tap
#[inline(always)]
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where
Self: AsRef<R>,
R: ?Sized,
{
func(AsRef::<R>::as_ref(&self));
self
}
/// Mutable access to the `AsMut<R>` view of a value.
///
/// This function is identical to [`Tap::tap_mut`], except that the effect
/// function receives an `&mut R` produced by `AsMut::<R>::as_mut`, rather
/// than an `&mut Self`.
///
/// [`Tap::tap_mut`]: trait.Tap.html#method.tap_mut
#[inline(always)]
fn tap_ref_mut<R>(mut self, func: impl FnOnce(&mut R)) -> Self
where
Self: AsMut<R>,
R: ?Sized,
{
func(AsMut::<R>::as_mut(&mut self));
self
}
/// Immutable access to the `Deref::Target` of a value.
///
/// This function is identical to [`Tap::tap`], except that the effect
/// function receives an `&Self::Target` produced by `Deref::deref`, rather
/// than an `&Self`.
///
/// [`Tap::tap`]: trait.Tap.html#method.tap
#[inline(always)]
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where
Self: Deref<Target = T>,
T: ?Sized,
{
func(Deref::deref(&self));
self
}
/// Mutable access to the `Deref::Target` of a value.
///
/// This function is identical to [`Tap::tap_mut`], except that the effect
/// function receives an `&mut Self::Target` produced by
/// `DerefMut::deref_mut`, rather than an `&mut Self`.
///
/// [`Tap::tap_mut`]: trait.Tap.html#method.tap_mut
#[inline(always)]
fn tap_deref_mut<T>(mut self, func: impl FnOnce(&mut T)) -> Self
where
Self: DerefMut + Deref<Target = T>,
T: ?Sized,
{
func(DerefMut::deref_mut(&mut self));
self
}
// debug-build-only copies of the above methods
/// Calls `.tap()` only in debug builds, and is erased in release builds.
#[inline(always)]
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self {
if cfg!(debug_assertions) {
func(&self);
}
self
}
/// Calls `.tap_mut()` only in debug builds, and is erased in release
/// builds.
#[inline(always)]
fn tap_mut_dbg(mut self, func: impl FnOnce(&mut Self)) -> Self {
if cfg!(debug_assertions) {
func(&mut self);
}
self
}
/// Calls `.tap_borrow()` only in debug builds, and is erased in release
/// builds.
#[inline(always)]
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where
Self: Borrow<B>,
B: ?Sized,
{
if cfg!(debug_assertions) {
func(Borrow::<B>::borrow(&self));
}
self
}
/// Calls `.tap_borrow_mut()` only in debug builds, and is erased in release
/// builds.
#[inline(always)]
fn tap_borrow_mut_dbg<B>(mut self, func: impl FnOnce(&mut B)) -> Self
where
Self: BorrowMut<B>,
B: ?Sized,
{
if cfg!(debug_assertions) {
func(BorrowMut::<B>::borrow_mut(&mut self));
}
self
}
/// Calls `.tap_ref()` only in debug builds, and is erased in release
/// builds.
#[inline(always)]
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where
Self: AsRef<R>,
R: ?Sized,
{
if cfg!(debug_assertions) {
func(AsRef::<R>::as_ref(&self));
}
self
}
/// Calls `.tap_ref_mut()` only in debug builds, and is erased in release
/// builds.
#[inline(always)]
fn tap_ref_mut_dbg<R>(mut self, func: impl FnOnce(&mut R)) -> Self
where
Self: AsMut<R>,
R: ?Sized,
{
if cfg!(debug_assertions) {
func(AsMut::<R>::as_mut(&mut self));
}
self
}
/// Calls `.tap_deref()` only in debug builds, and is erased in release
/// builds.
#[inline(always)]
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where
Self: Deref<Target = T>,
T: ?Sized,
{
if cfg!(debug_assertions) {
func(Deref::deref(&self));
}
self
}
/// Calls `.tap_deref_mut()` only in debug builds, and is erased in release
/// builds.
#[inline(always)]
fn tap_deref_mut_dbg<T>(mut self, func: impl FnOnce(&mut T)) -> Self
where
Self: DerefMut + Deref<Target = T>,
T: ?Sized,
{
if cfg!(debug_assertions) {
func(DerefMut::deref_mut(&mut self));
}
self
}
}
impl<T> Tap for T where T: Sized {}
#[cfg(test)]
mod test_tap {
use crate::rails::ext::blocking::Tap;
#[test]
fn test_tap() {
let res = "hello".to_string();
res.tap(|t| assert_eq!(t, &"hello".to_string()));
}
#[test]
fn test_tap_mut() {
let res = "hello".to_string();
assert_eq!(
res.tap_mut(|t| *t = "world".to_string()),
"world".to_string()
);
}
}