Skip to content

Commit 2e523f6

Browse files
committed
tweak and document Chain::with_inner
I'm fucking collapsing I should go to sleep or something
1 parent eda48f4 commit 2e523f6

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/chain.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,44 @@ where
3333
self.as_mut()
3434
}
3535

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+
/// ```
3668
#[inline]
37-
fn with_inner<F>(mut self, f: F) -> Self
69+
fn with_inner<F, Void>(mut self, f: F) -> Self
3870
where
39-
F: FnOnce(&mut Self::Inner)
71+
F: FnOnce(&mut Self::Inner) -> Void
4072
{
41-
f(self.as_inner_mut());
73+
let _void = f(self.as_inner_mut());
4274
self
4375
}
4476
}

0 commit comments

Comments
 (0)