6
6
7
7
use cmp:: { Eq , Ord } ;
8
8
9
- pub trait TupleOps < T , U > {
9
+ pub trait CopyableTuple < T , U > {
10
10
pure fn first ( ) -> T ;
11
11
pure fn second ( ) -> U ;
12
12
pure fn swap ( ) -> ( U , T ) ;
13
13
}
14
14
15
- impl < T : Copy , U : Copy > ( T , U ) : TupleOps < T , U > {
15
+ impl < T : Copy , U : Copy > ( T , U ) : CopyableTuple < T , U > {
16
16
17
17
/// Return the first element of self
18
18
pure fn first ( ) -> T {
@@ -34,6 +34,24 @@ impl<T: Copy, U: Copy> (T, U): TupleOps<T,U> {
34
34
35
35
}
36
36
37
+ pub trait ImmutableTuple < T , U > {
38
+ pure fn first_ref ( & self ) -> & self /T ;
39
+ pure fn second_ref ( & self ) -> & self /U ;
40
+ }
41
+
42
+ impl < T , U > ( T , U ) : ImmutableTuple < T , U > {
43
+ pure fn first_ref ( & self ) -> & self /T {
44
+ match * self {
45
+ ( ref t, _) => t,
46
+ }
47
+ }
48
+ pure fn second_ref ( & self ) -> & self /U {
49
+ match * self {
50
+ ( _, ref u) => u,
51
+ }
52
+ }
53
+ }
54
+
37
55
pub trait ExtendedTupleOps < A , B > {
38
56
fn zip ( & self ) -> ~[ ( A , B ) ] ;
39
57
fn map < C > ( & self , f : & fn ( a : & A , b : & B ) -> C ) -> ~[ C ] ;
@@ -145,6 +163,13 @@ impl<A: Ord, B: Ord, C: Ord> (A, B, C) : Ord {
145
163
pure fn gt ( other : & ( A , B , C ) ) -> bool { ( * other) . lt ( & self ) }
146
164
}
147
165
166
+ #[ test]
167
+ fn test_tuple_ref ( ) {
168
+ let ( ~"foo", ~"bar") ;
169
+ assert x. first_ref ( ) == & ~"foo";
170
+ assert x. second_ref ( ) == & ~"bar";
171
+ }
172
+
148
173
#[ test]
149
174
#[ allow( non_implicitly_copyable_typarams) ]
150
175
fn test_tuple ( ) {
0 commit comments