apply_if/lib.rs
1#![doc=include_str!("../README.md")]
2#![no_std]
3#[warn(missing_docs)]
4#[cfg(test)]
5mod test;
6
7pub trait ApplyIf {
8 /// Takes a closure, and calls it on ``self`` if ``cond`` is true.
9 ///
10 /// Equivalent to the following:
11 /// ```
12 /// # struct Foo;
13 /// # impl Foo {
14 /// # fn foo(self, cond: bool) -> Self {
15 /// # let f = |x| x;
16 /// if cond {
17 /// f(self)
18 /// } else {
19 /// self
20 /// }
21 /// # }}
22 /// ```
23 /// **Note** this method only works on sized objects
24 fn apply_if<F>(self, cond: bool, f: F) -> Self
25 where
26 Self: Sized,
27 F: FnOnce(Self) -> Self;
28
29 /// Takes a closure, and calls it a mutable reference to `Self`.
30 /// Use this method with the mutable builder pattern
31 ///
32 /// Equivalent to the following for `self : &mut Self`:
33 /// ```
34 /// # struct Foo;
35 /// # impl Foo {
36 /// # fn foo(&mut self, cond:bool) -> &mut Self {
37 /// # let f = |x| x;
38 /// if cond {
39 /// f(self)
40 /// } else {
41 /// self
42 /// }
43 /// # }}
44 /// ```
45 fn apply_if_mut<F>(&mut self, cond: bool, f: F) -> &mut Self
46 where
47 F: FnOnce(&mut Self) -> &mut Self;
48
49 /// Apply the closure only if the given optional argument contains
50 /// a value.
51 /// Use this method with the immutable builder pattern
52 ///
53 /// ```
54 /// # struct Foo;
55 /// # impl Foo {
56 /// # fn foo(self, cond:bool) -> Self {
57 /// # let f = |x,u| x;
58 /// # let optional_u = Some(1);
59 /// if let Some(u) = optional_u {
60 /// f(self,u)
61 /// } else {
62 /// self
63 /// }
64 /// # }}
65 /// ```
66 fn apply_if_some<F, U>(self, u: Option<U>, f: F) -> Self
67 where
68 Self: Sized,
69 F: FnOnce(Self, U) -> Self;
70
71 /// Apply the closure only if the given optional argument contains
72 /// a value.
73 /// Use this method with the mutable builder pattern
74 ///
75 /// ```
76 /// # struct Foo;
77 /// # impl Foo {
78 /// # fn foo(&mut self, cond:bool) -> &mut Self {
79 /// # let f = |x,u| x;
80 /// # let optional_u = Some(1);
81 /// if let Some(u) = optional_u {
82 /// f(self,u)
83 /// } else {
84 /// self
85 /// }
86 /// # }}
87 /// ```
88 fn apply_if_some_mut<F, U>(&mut self, u: Option<U>, f: F) -> &mut Self
89 where
90 F: FnOnce(&mut Self, U) -> &mut Self;
91}
92
93impl<T> ApplyIf for T {
94 #[inline]
95 fn apply_if<F: FnOnce(Self) -> Self>(self, cond: bool, f: F) -> Self
96 where
97 Self: Sized,
98 {
99 if cond {
100 f(self)
101 } else {
102 self
103 }
104 }
105
106 #[inline]
107 fn apply_if_mut<F>(&mut self, cond: bool, f: F) -> &mut Self
108 where
109 F: FnOnce(&mut Self) -> &mut Self,
110 {
111 if cond {
112 f(self)
113 } else {
114 self
115 }
116 }
117
118 #[inline]
119 fn apply_if_some<F, U>(self, u: Option<U>, f: F) -> Self
120 where
121 Self: Sized,
122 F: FnOnce(Self, U) -> Self,
123 {
124 if let Some(u) = u {
125 f(self, u)
126 } else {
127 self
128 }
129 }
130
131 #[inline]
132 fn apply_if_some_mut<F, U>(&mut self, u: Option<U>, f: F) -> &mut Self
133 where
134 F: FnOnce(&mut Self, U) -> &mut Self,
135 {
136 if let Some(u) = u {
137 f(self, u)
138 } else {
139 self
140 }
141 }
142}