@@ -33,12 +33,44 @@ where
33
33
self . as_mut ( )
34
34
}
35
35
36
+ /// Takes a closure that is called, passing in a reference to the inner value
37
+ ///
38
+ /// This is useful for if there is something we have not implemented, or you
39
+ /// otherwise need to borrow the inner struct for something, but are in the
40
+ /// middle of some chain. It lets you do otherwise nonchainable operations
41
+ /// inline with other chaining operations, so no need to break the chain c:
42
+ ///
43
+ /// The closure passed in is allowed to return anything, but the return value
44
+ /// is simply ignored. This makes it so you can call a function for its side
45
+ /// effect, even if that function returns something you wouldn't have needed
46
+ /// anyways.
47
+ ///
48
+ /// For example, [`MaybeUninit::write`] returns `&mut T`, but you might not
49
+ /// need that reference, so instead of writing `
50
+ /// .with_inner(|val| { val.write(...); })`, you can simply write
51
+ /// `.with_inner(|val| val.write(...))`.
52
+ ///
53
+ /// # Examples
54
+ ///
55
+ /// ```
56
+ /// # use wiwi::chain::{ Chain as _, VecChain };
57
+ /// let chain = VecChain::<usize>::new();
58
+ ///
59
+ /// // let's pretend `push` and `reserve` don't already have chainable versions...
60
+ /// let chain = chain
61
+ /// .with_inner(|v| v.reserve(10))
62
+ /// .with_inner(|v| v.push(1))
63
+ /// .with_inner(|v| v.push(2));
64
+ ///
65
+ /// assert!(chain.as_inner().len() == 2);
66
+ /// assert!(chain.as_inner().capacity() >= 10);
67
+ /// ```
36
68
#[ inline]
37
- fn with_inner < F > ( mut self , f : F ) -> Self
69
+ fn with_inner < F , Void > ( mut self , f : F ) -> Self
38
70
where
39
- F : FnOnce ( & mut Self :: Inner )
71
+ F : FnOnce ( & mut Self :: Inner ) -> Void
40
72
{
41
- f ( self . as_inner_mut ( ) ) ;
73
+ let _void = f ( self . as_inner_mut ( ) ) ;
42
74
self
43
75
}
44
76
}
0 commit comments