apply/also.rs
1/// Represents a type that you can apply arbitrary functions to.
2/// Useful for when a method doesn't return the receiver but you want
3/// to apply several of them to the object.
4pub trait Also : Sized {
5
6 /// Apply a function to this value and return the (possibly) modified value.
7 fn also<F: FnOnce(&mut Self)>(mut self, block: F) -> Self {
8 block(&mut self);
9 self
10 }
11}
12
13impl <T: Sized> Also for T {}
14
15#[cfg(test)]
16mod test {
17 use super::*;
18
19 #[test]
20 fn test_also() {
21 let it = vec![3, 2, 1, 5]
22 .also(|it| it.sort())
23 .also(|it| it.push(7));
24 assert_eq!(it, vec![1, 2, 3, 5, 7]);
25 }
26}