|
10 | 10 |
|
11 | 11 | //! Operations on tuples
|
12 | 12 |
|
13 |
| -use clone::Clone; |
14 | 13 | use kinds::Copy;
|
15 | 14 | use vec;
|
16 | 15 |
|
17 |
| -#[cfg(not(test))] use cmp::{Eq, Ord}; |
18 |
| - |
19 |
| -pub use self::getters::*; |
| 16 | +pub use self::inner::*; |
20 | 17 |
|
21 | 18 | pub trait CopyableTuple<T, U> {
|
22 | 19 | fn first(&self) -> T;
|
@@ -46,16 +43,6 @@ impl<T:Copy,U:Copy> CopyableTuple<T, U> for (T, U) {
|
46 | 43 | let (t, u) = *self;
|
47 | 44 | return (u, t);
|
48 | 45 | }
|
49 |
| - |
50 |
| -} |
51 |
| - |
52 |
| -impl<T:Clone,U:Clone> Clone for (T, U) { |
53 |
| - fn clone(&self) -> (T, U) { |
54 |
| - let (a, b) = match *self { |
55 |
| - (ref a, ref b) => (a, b) |
56 |
| - }; |
57 |
| - (a.clone(), b.clone()) |
58 |
| - } |
59 | 46 | }
|
60 | 47 |
|
61 | 48 | pub trait ImmutableTuple<T, U> {
|
@@ -124,158 +111,81 @@ impl<A:Copy,B:Copy> ExtendedTupleOps<A,B> for (~[A], ~[B]) {
|
124 | 111 | }
|
125 | 112 | }
|
126 | 113 |
|
127 |
| -#[cfg(not(test))] |
128 |
| -impl<A:Eq> Eq for (A,) { |
129 |
| - #[inline(always)] |
130 |
| - fn eq(&self, other: &(A,)) -> bool { |
131 |
| - match (*self) { |
132 |
| - (ref self_a,) => match other { |
133 |
| - &(ref other_a,) => { |
134 |
| - (*self_a).eq(other_a) |
135 |
| - } |
136 |
| - } |
| 114 | +// macro for implementing n-ary tuple functions and operations |
| 115 | + |
| 116 | +macro_rules! tuple_impls( |
| 117 | + ($( |
| 118 | + $name:ident { |
| 119 | + $(fn $get_fn:ident -> $T:ident { $get_pattern:pat => $ret:expr })+ |
137 | 120 | }
|
138 |
| - } |
139 |
| - #[inline(always)] |
140 |
| - fn ne(&self, other: &(A,)) -> bool { !(*self).eq(other) } |
141 |
| -} |
| 121 | + )+) => ( |
| 122 | + pub mod inner { |
| 123 | + use clone::Clone; |
| 124 | + #[cfg(not(test))] use cmp::{Eq, Ord}; |
142 | 125 |
|
143 |
| -#[cfg(not(test))] |
144 |
| -impl<A:Ord> Ord for (A,) { |
145 |
| - #[inline(always)] |
146 |
| - fn lt(&self, other: &(A,)) -> bool { |
147 |
| - match (*self) { |
148 |
| - (ref self_a,) => { |
149 |
| - match (*other) { |
150 |
| - (ref other_a,) => { |
151 |
| - if (*self_a).lt(other_a) { return true; } |
152 |
| - return false; |
153 |
| - } |
| 126 | + $( |
| 127 | + pub trait $name<$($T),+> { |
| 128 | + $(fn $get_fn<'a>(&'a self) -> &'a $T;)+ |
154 | 129 | }
|
155 |
| - } |
156 |
| - } |
157 |
| - } |
158 |
| - #[inline(always)] |
159 |
| - fn le(&self, other: &(A,)) -> bool { !other.lt(&(*self)) } |
160 |
| - #[inline(always)] |
161 |
| - fn ge(&self, other: &(A,)) -> bool { !self.lt(other) } |
162 |
| - #[inline(always)] |
163 |
| - fn gt(&self, other: &(A,)) -> bool { other.lt(&(*self)) } |
164 |
| -} |
165 | 130 |
|
166 |
| -#[cfg(not(test))] |
167 |
| -impl<A:Eq,B:Eq> Eq for (A, B) { |
168 |
| - #[inline(always)] |
169 |
| - fn eq(&self, other: &(A, B)) -> bool { |
170 |
| - match (*self) { |
171 |
| - (ref self_a, ref self_b) => match other { |
172 |
| - &(ref other_a, ref other_b) => { |
173 |
| - (*self_a).eq(other_a) && (*self_b).eq(other_b) |
| 131 | + impl<$($T),+> $name<$($T),+> for ($($T),+) { |
| 132 | + $( |
| 133 | + #[inline(always)] |
| 134 | + fn $get_fn<'a>(&'a self) -> &'a $T { |
| 135 | + match *self { |
| 136 | + $get_pattern => $ret |
| 137 | + } |
| 138 | + } |
| 139 | + )+ |
174 | 140 | }
|
175 |
| - } |
176 |
| - } |
177 |
| - } |
178 |
| - #[inline(always)] |
179 |
| - fn ne(&self, other: &(A, B)) -> bool { !(*self).eq(other) } |
180 |
| -} |
181 | 141 |
|
182 |
| -#[cfg(not(test))] |
183 |
| -impl<A:Ord,B:Ord> Ord for (A, B) { |
184 |
| - #[inline(always)] |
185 |
| - fn lt(&self, other: &(A, B)) -> bool { |
186 |
| - match (*self) { |
187 |
| - (ref self_a, ref self_b) => { |
188 |
| - match (*other) { |
189 |
| - (ref other_a, ref other_b) => { |
190 |
| - if (*self_a).lt(other_a) { return true; } |
191 |
| - if (*other_a).lt(self_a) { return false; } |
192 |
| - if (*self_b).lt(other_b) { return true; } |
193 |
| - return false; |
| 142 | + impl<$($T:Clone),+> Clone for ($($T),+) { |
| 143 | + fn clone(&self) -> ($($T),+) { |
| 144 | + ($(self.$get_fn().clone()),+) |
194 | 145 | }
|
195 | 146 | }
|
196 |
| - } |
197 |
| - } |
198 |
| - } |
199 |
| - #[inline(always)] |
200 |
| - fn le(&self, other: &(A, B)) -> bool { !(*other).lt(&(*self)) } |
201 |
| - #[inline(always)] |
202 |
| - fn ge(&self, other: &(A, B)) -> bool { !(*self).lt(other) } |
203 |
| - #[inline(always)] |
204 |
| - fn gt(&self, other: &(A, B)) -> bool { (*other).lt(&(*self)) } |
205 |
| -} |
206 | 147 |
|
207 |
| -#[cfg(not(test))] |
208 |
| -impl<A:Eq,B:Eq,C:Eq> Eq for (A, B, C) { |
209 |
| - #[inline(always)] |
210 |
| - fn eq(&self, other: &(A, B, C)) -> bool { |
211 |
| - match (*self) { |
212 |
| - (ref self_a, ref self_b, ref self_c) => match other { |
213 |
| - &(ref other_a, ref other_b, ref other_c) => { |
214 |
| - (*self_a).eq(other_a) && (*self_b).eq(other_b) |
215 |
| - && (*self_c).eq(other_c) |
216 |
| - } |
217 |
| - } |
218 |
| - } |
219 |
| - } |
220 |
| - #[inline(always)] |
221 |
| - fn ne(&self, other: &(A, B, C)) -> bool { !(*self).eq(other) } |
222 |
| -} |
| 148 | + #[cfg(not(test))] |
| 149 | + impl<$($T:Eq),+> Eq for ($($T),+) { |
| 150 | + #[inline(always)] |
| 151 | + fn eq(&self, other: &($($T),+)) -> bool { |
| 152 | + $(*self.$get_fn() == *other.$get_fn())&&+ |
| 153 | + } |
223 | 154 |
|
224 |
| -#[cfg(not(test))] |
225 |
| -impl<A:Ord,B:Ord,C:Ord> Ord for (A, B, C) { |
226 |
| - #[inline(always)] |
227 |
| - fn lt(&self, other: &(A, B, C)) -> bool { |
228 |
| - match (*self) { |
229 |
| - (ref self_a, ref self_b, ref self_c) => { |
230 |
| - match (*other) { |
231 |
| - (ref other_a, ref other_b, ref other_c) => { |
232 |
| - if (*self_a).lt(other_a) { return true; } |
233 |
| - if (*other_a).lt(self_a) { return false; } |
234 |
| - if (*self_b).lt(other_b) { return true; } |
235 |
| - if (*other_b).lt(self_b) { return false; } |
236 |
| - if (*self_c).lt(other_c) { return true; } |
237 |
| - return false; |
| 155 | + #[inline(always)] |
| 156 | + fn ne(&self, other: &($($T),+)) -> bool { |
| 157 | + !(*self == *other) |
238 | 158 | }
|
239 | 159 | }
|
240 |
| - } |
241 |
| - } |
242 |
| - } |
243 |
| - #[inline(always)] |
244 |
| - fn le(&self, other: &(A, B, C)) -> bool { !(*other).lt(&(*self)) } |
245 |
| - #[inline(always)] |
246 |
| - fn ge(&self, other: &(A, B, C)) -> bool { !(*self).lt(other) } |
247 |
| - #[inline(always)] |
248 |
| - fn gt(&self, other: &(A, B, C)) -> bool { (*other).lt(&(*self)) } |
249 |
| -} |
250 | 160 |
|
251 |
| -// Tuple element getters |
| 161 | + #[cfg(not(test))] |
| 162 | + impl<$($T:Ord),+> Ord for ($($T),+) { |
| 163 | + #[inline(always)] |
| 164 | + fn lt(&self, other: &($($T),+)) -> bool { |
| 165 | + $(*self.$get_fn() < *other.$get_fn())&&+ |
| 166 | + } |
252 | 167 |
|
253 |
| -macro_rules! tuple_getters( |
254 |
| - ($( |
255 |
| - $name:ident { |
256 |
| - $(fn $method:ident -> $T:ident { $accessor:pat => $t:expr })+ |
257 |
| - } |
258 |
| - )+) => ( |
259 |
| - pub mod getters { |
260 |
| - $(pub trait $name<$($T),+> { |
261 |
| - $(fn $method<'a>(&'a self) -> &'a $T;)+ |
262 |
| - })+ |
| 168 | + #[inline(always)] |
| 169 | + fn le(&self, other: &($($T),+)) -> bool { |
| 170 | + $(*self.$get_fn() <= *other.$get_fn())&&+ |
| 171 | + } |
263 | 172 |
|
264 |
| - $(impl<$($T),+> $name<$($T),+> for ($($T),+) { |
265 |
| - $( |
266 | 173 | #[inline(always)]
|
267 |
| - fn $method<'a>(&'a self) -> &'a $T { |
268 |
| - match *self { |
269 |
| - $accessor => $t |
270 |
| - } |
| 174 | + fn ge(&self, other: &($($T),+)) -> bool { |
| 175 | + $(*self.$get_fn() >= *other.$get_fn())&&+ |
271 | 176 | }
|
272 |
| - )+ |
273 |
| - })+ |
| 177 | + |
| 178 | + #[inline(always)] |
| 179 | + fn gt(&self, other: &($($T),+)) -> bool { |
| 180 | + $(*self.$get_fn() > *other.$get_fn())&&+ |
| 181 | + } |
| 182 | + } |
| 183 | + )+ |
274 | 184 | }
|
275 | 185 | )
|
276 | 186 | )
|
277 | 187 |
|
278 |
| -tuple_getters!( |
| 188 | +tuple_impls!( |
279 | 189 | Tuple2 {
|
280 | 190 | fn n0 -> A { (ref a,_) => a }
|
281 | 191 | fn n1 -> B { (_,ref b) => b }
|
|
0 commit comments