free_function_pipes/lib.rs
1/// the point of this trait, is allow you to use, free functions, in composing manner
2///without being difficult to read with lots of nested parethensis
3trait FreeFunPiper : Sized {
4 #[inline(always)]
5 fn pipe<U,Fun>(self, f: Fun) -> U
6 where
7 Fun : FnOnce(Self) -> U,
8 U : Sized
9 {
10 f(self)
11 }
12
13 ///all function
14 #[inline(always)]
15 fn pipe2<U,A,Fun>(self, f: Fun, a : A) -> U
16 where
17 Fun : FnOnce(Self,A) -> U,
18 U : Sized
19 {
20 f(self,a)
21 }
22
23 #[inline(always)]
24 fn pipe3<A,B,U,Fun>(self, f : Fun,
25 a : A,
26 b : B) -> U
27 where
28 Fun : FnOnce(Self,A,B) -> U
29 {
30 f(self,a,b)
31 }
32
33 #[inline(always)]
34 fn pipe4<A,B,C,U,Fun>(self, f : Fun,
35 a : A,
36 b : B,
37 c : C) -> U
38 where
39 Fun : FnOnce(Self,A,B,C) -> U
40 {
41 f(self,a,b,c)
42 }
43
44 #[inline(always)]
45 fn pipe5<A,B,C,D,U,Fun>(self, f : Fun,
46 a : A,
47 b : B,
48 c : C,
49 d : D) -> U
50 where
51 Fun : FnOnce(Self,A,B,C,D) -> U
52 {
53 f(self,a,b,c,d)
54 }
55///truthfully if you need more data than this
56///you have a real problem, you should probably merge some
57///arguments under a structure
58 #[inline(always)]
59 fn pipe6<A,B,C,D,E,U,Fun>(self, f : Fun,
60 a : A,
61 b : B,
62 c : C,
63 d : D,
64 e : E) -> U
65 where
66 Fun : FnOnce(Self,A,B,C,D,E) -> U
67 {
68 f(self,a,b,c,d,e)
69 }
70}
71//add piper trait to nearly every type that it can
72impl<T> FreeFunPiper for T where T : Sized {}
73
74///this trait allows you to use functions,
75///that might normally might not be chainable,
76///using pipes from above trait!
77///in theory using this should be no slower than
78///pipe because, move ellision should eliminate most of the moves.
79///making pipm chaining just as fast explicit pipe writers
80trait FreeFunPipModer : Sized {
81
82 #[inline(always)]
83 fn pipm<Fun>(mut self, f: Fun) -> Self
84 where
85 Fun : FnOnce(&mut Self)
86 {
87 f(&mut self);
88 self
89 }
90
91 #[inline(always)]
92 fn pipm2<A,Fun>(mut self, f: Fun, a : A) -> Self
93 where
94 Fun : FnOnce(&mut Self,A)
95 {
96 f(&mut self,a);
97 self
98 }
99
100 #[inline(always)]
101 fn pipm3<A,B,Fun>(mut self, f : Fun,
102 a : A,
103 b : B) -> Self
104 where
105 Fun : FnOnce(&mut Self,A,B)
106 {
107 f(&mut self,a,b);
108 self
109 }
110
111 #[inline(always)]
112 fn pipm4<A,B,C,Fun>(mut self, f : Fun,
113 a : A,
114 b : B,
115 c : C) -> Self
116 where
117 Fun : FnOnce(&mut Self,A,B,C)
118 {
119 f(&mut self,a,b,c);
120 self
121 }
122
123 #[inline(always)]
124 fn pipm5<A,B,C,D,Fun>(mut self, f : Fun,
125 a : A,
126 b : B,
127 c : C,
128 d : D) -> Self
129 where
130 Fun : FnOnce(&mut Self,A,B,C,D)
131 {
132 f(&mut self,a,b,c,d);
133 self
134 }
135///truthfully if you need more data than this
136///you have a real problem, you should probably merge some
137///arguments under a structure
138 #[inline(always)]
139 fn pipm6<A,B,C,D,E,Fun>(mut self, f : Fun,
140 a : A,
141 b : B,
142 c : C,
143 d : D,
144 e : E) -> Self
145 where
146 Fun : FnOnce(&mut Self,A,B,C,D,E)
147 {
148 f(&mut self,a,b,c,d,e);
149 self
150
151 }
152}
153///blanket implement, because is really only limited,
154///by the fact that T must be sized to be passed or returned by value.
155impl<T> FreeFunPipModer for T where T : Sized {}
156
157