Skip to content

Commit 6742f91

Browse files
committed
Create tuple element accessor traits
1 parent e91daaa commit 6742f91

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

src/libcore/prelude.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pub use from_str::{FromStr};
5252
pub use to_bytes::IterBytes;
5353
pub use to_str::{ToStr, ToStrConsume};
5454
pub use tuple::{CopyableTuple, ImmutableTuple, ExtendedTupleOps};
55+
pub use tuple::{Tuple2, Tuple3, Tuple4, Tuple5, Tuple6, Tuple7, Tuple8, Tuple9};
56+
pub use tuple::{Tuple10, Tuple11, Tuple12};
5557
pub use vec::{CopyableVector, ImmutableVector};
5658
pub use vec::{ImmutableEqVector, ImmutableCopyableVector};
5759
pub use vec::{OwnedVector, OwnedCopyableVector, MutableVector};

src/libcore/tuple.rs

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,154 @@ impl<A:Ord,B:Ord,C:Ord> Ord for (A, B, C) {
246246
fn gt(&self, other: &(A, B, C)) -> bool { (*other).lt(&(*self)) }
247247
}
248248

249+
// Tuple element accessor traits
250+
251+
macro_rules! n_tuple(
252+
($name:ident: $($method:ident : $T:ident),+) => (
253+
pub trait $name<$($T),+> {
254+
$(fn $method(&self) -> $T;)+
255+
}
256+
)
257+
)
258+
259+
n_tuple!(Tuple2: _0:A, _1:B)
260+
n_tuple!(Tuple3: _0:A, _1:B, _2:C)
261+
n_tuple!(Tuple4: _0:A, _1:B, _2:C, _3:D)
262+
n_tuple!(Tuple5: _0:A, _1:B, _2:C, _3:D, _4:E)
263+
n_tuple!(Tuple6: _0:A, _1:B, _2:C, _3:D, _4:E, _5:F)
264+
n_tuple!(Tuple7: _0:A, _1:B, _2:C, _3:D, _4:E, _5:F, _6:G)
265+
n_tuple!(Tuple8: _0:A, _1:B, _2:C, _3:D, _4:E, _5:F, _6:G, _7:H)
266+
n_tuple!(Tuple9: _0:A, _1:B, _2:C, _3:D, _4:E, _5:F, _6:G, _7:H, _8:I)
267+
n_tuple!(Tuple10: _0:A, _1:B, _2:C, _3:D, _4:E, _5:F, _6:G, _7:H, _8:I, _9:J)
268+
n_tuple!(Tuple11: _0:A, _1:B, _2:C, _3:D, _4:E, _5:F, _6:G, _7:H, _8:I, _9:J, _10:K)
269+
n_tuple!(Tuple12: _0:A, _1:B, _2:C, _3:D, _4:E, _5:F, _6:G, _7:H, _8:I, _9:J, _10:K, _11:L)
270+
271+
// Tuple element accessor trait implementations
272+
273+
macro_rules! impl_n_tuple(
274+
($name:ident: $($method:ident -> $T:ident { $accessor:pat => $t:expr })+) => (
275+
impl<$($T:Copy),+> $name<$($T),+> for ($($T),+) {
276+
$(
277+
fn $method(&self) -> $T {
278+
match *self {
279+
$accessor => $t
280+
}
281+
}
282+
)+
283+
}
284+
)
285+
)
286+
287+
impl_n_tuple!(Tuple2:
288+
_0 -> A { (a,_) => a }
289+
_1 -> B { (_,b) => b }
290+
)
291+
292+
impl_n_tuple!(Tuple3:
293+
_0 -> A { (a,_,_) => a }
294+
_1 -> B { (_,b,_) => b }
295+
_2 -> C { (_,_,c) => c }
296+
)
297+
298+
impl_n_tuple!(Tuple4:
299+
_0 -> A { (a,_,_,_) => a }
300+
_1 -> B { (_,b,_,_) => b }
301+
_2 -> C { (_,_,c,_) => c }
302+
_3 -> D { (_,_,_,d) => d }
303+
)
304+
305+
impl_n_tuple!(Tuple5:
306+
_0 -> A { (a,_,_,_,_) => a }
307+
_1 -> B { (_,b,_,_,_) => b }
308+
_2 -> C { (_,_,c,_,_) => c }
309+
_3 -> D { (_,_,_,d,_) => d }
310+
_4 -> E { (_,_,_,_,e) => e }
311+
)
312+
313+
impl_n_tuple!(Tuple6:
314+
_0 -> A { (a,_,_,_,_,_) => a }
315+
_1 -> B { (_,b,_,_,_,_) => b }
316+
_2 -> C { (_,_,c,_,_,_) => c }
317+
_3 -> D { (_,_,_,d,_,_) => d }
318+
_4 -> E { (_,_,_,_,e,_) => e }
319+
_5 -> F { (_,_,_,_,_,f) => f }
320+
)
321+
322+
impl_n_tuple!(Tuple7:
323+
_0 -> A { (a,_,_,_,_,_,_) => a }
324+
_1 -> B { (_,b,_,_,_,_,_) => b }
325+
_2 -> C { (_,_,c,_,_,_,_) => c }
326+
_3 -> D { (_,_,_,d,_,_,_) => d }
327+
_4 -> E { (_,_,_,_,e,_,_) => e }
328+
_5 -> F { (_,_,_,_,_,f,_) => f }
329+
_6 -> G { (_,_,_,_,_,_,g) => g }
330+
)
331+
332+
impl_n_tuple!(Tuple8:
333+
_0 -> A { (a,_,_,_,_,_,_,_) => a }
334+
_1 -> B { (_,b,_,_,_,_,_,_) => b }
335+
_2 -> C { (_,_,c,_,_,_,_,_) => c }
336+
_3 -> D { (_,_,_,d,_,_,_,_) => d }
337+
_4 -> E { (_,_,_,_,e,_,_,_) => e }
338+
_5 -> F { (_,_,_,_,_,f,_,_) => f }
339+
_6 -> G { (_,_,_,_,_,_,g,_) => g }
340+
_7 -> H { (_,_,_,_,_,_,_,h) => h }
341+
)
342+
343+
impl_n_tuple!(Tuple9:
344+
_0 -> A { (a,_,_,_,_,_,_,_,_) => a }
345+
_1 -> B { (_,b,_,_,_,_,_,_,_) => b }
346+
_2 -> C { (_,_,c,_,_,_,_,_,_) => c }
347+
_3 -> D { (_,_,_,d,_,_,_,_,_) => d }
348+
_4 -> E { (_,_,_,_,e,_,_,_,_) => e }
349+
_5 -> F { (_,_,_,_,_,f,_,_,_) => f }
350+
_6 -> G { (_,_,_,_,_,_,g,_,_) => g }
351+
_7 -> H { (_,_,_,_,_,_,_,h,_) => h }
352+
_8 -> I { (_,_,_,_,_,_,_,_,i) => i }
353+
)
354+
355+
impl_n_tuple!(Tuple10:
356+
_0 -> A { (a,_,_,_,_,_,_,_,_,_) => a }
357+
_1 -> B { (_,b,_,_,_,_,_,_,_,_) => b }
358+
_2 -> C { (_,_,c,_,_,_,_,_,_,_) => c }
359+
_3 -> D { (_,_,_,d,_,_,_,_,_,_) => d }
360+
_4 -> E { (_,_,_,_,e,_,_,_,_,_) => e }
361+
_5 -> F { (_,_,_,_,_,f,_,_,_,_) => f }
362+
_6 -> G { (_,_,_,_,_,_,g,_,_,_) => g }
363+
_7 -> H { (_,_,_,_,_,_,_,h,_,_) => h }
364+
_8 -> I { (_,_,_,_,_,_,_,_,i,_) => i }
365+
_9 -> J { (_,_,_,_,_,_,_,_,_,j) => j }
366+
)
367+
368+
impl_n_tuple!(Tuple11:
369+
_0 -> A { (a,_,_,_,_,_,_,_,_,_,_) => a }
370+
_1 -> B { (_,b,_,_,_,_,_,_,_,_,_) => b }
371+
_2 -> C { (_,_,c,_,_,_,_,_,_,_,_) => c }
372+
_3 -> D { (_,_,_,d,_,_,_,_,_,_,_) => d }
373+
_4 -> E { (_,_,_,_,e,_,_,_,_,_,_) => e }
374+
_5 -> F { (_,_,_,_,_,f,_,_,_,_,_) => f }
375+
_6 -> G { (_,_,_,_,_,_,g,_,_,_,_) => g }
376+
_7 -> H { (_,_,_,_,_,_,_,h,_,_,_) => h }
377+
_8 -> I { (_,_,_,_,_,_,_,_,i,_,_) => i }
378+
_9 -> J { (_,_,_,_,_,_,_,_,_,j,_) => j }
379+
_10 -> K { (_,_,_,_,_,_,_,_,_,_,k) => k }
380+
)
381+
382+
impl_n_tuple!(Tuple12:
383+
_0 -> A { (a,_,_,_,_,_,_,_,_,_,_,_) => a }
384+
_1 -> B { (_,b,_,_,_,_,_,_,_,_,_,_) => b }
385+
_2 -> C { (_,_,c,_,_,_,_,_,_,_,_,_) => c }
386+
_3 -> D { (_,_,_,d,_,_,_,_,_,_,_,_) => d }
387+
_4 -> E { (_,_,_,_,e,_,_,_,_,_,_,_) => e }
388+
_5 -> F { (_,_,_,_,_,f,_,_,_,_,_,_) => f }
389+
_6 -> G { (_,_,_,_,_,_,g,_,_,_,_,_) => g }
390+
_7 -> H { (_,_,_,_,_,_,_,h,_,_,_,_) => h }
391+
_8 -> I { (_,_,_,_,_,_,_,_,i,_,_,_) => i }
392+
_9 -> J { (_,_,_,_,_,_,_,_,_,j,_,_) => j }
393+
_10 -> K { (_,_,_,_,_,_,_,_,_,_,k,_) => k }
394+
_11 -> L { (_,_,_,_,_,_,_,_,_,_,_,l) => l }
395+
)
396+
249397
#[test]
250398
fn test_tuple_ref() {
251399
let x = (~"foo", ~"bar");
@@ -268,3 +416,20 @@ fn test_clone() {
268416
assert!(a.first() == b.first());
269417
assert!(a.second() == b.second());
270418
}
419+
420+
#[test]
421+
fn test_n_tuple() {
422+
let t = (0u8, 1u16, 2u32, 3u64, 4u, 5i8, 6i16, 7i32, 8i64, 9i, 10f32, 11f64);
423+
assert_eq!(t._0(), 0u8);
424+
assert_eq!(t._1(), 1u16);
425+
assert_eq!(t._2(), 2u32);
426+
assert_eq!(t._3(), 3u64);
427+
assert_eq!(t._4(), 4u);
428+
assert_eq!(t._5(), 5i8);
429+
assert_eq!(t._6(), 6i16);
430+
assert_eq!(t._7(), 7i32);
431+
assert_eq!(t._8(), 8i64);
432+
assert_eq!(t._9(), 9i);
433+
assert_eq!(t._10(), 10f32);
434+
assert_eq!(t._11(), 11f64);
435+
}

0 commit comments

Comments
 (0)