Skip to content

Commit ce1e211

Browse files
committed
Adapt the getter macro to implement Clone, Eq and Ord for n-ary tuples
1 parent 3a323c1 commit ce1e211

File tree

1 file changed

+55
-145
lines changed

1 file changed

+55
-145
lines changed

src/libcore/tuple.rs

Lines changed: 55 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010

1111
//! Operations on tuples
1212
13-
use clone::Clone;
1413
use kinds::Copy;
1514
use vec;
1615

17-
#[cfg(not(test))] use cmp::{Eq, Ord};
18-
19-
pub use self::getters::*;
16+
pub use self::inner::*;
2017

2118
pub trait CopyableTuple<T, U> {
2219
fn first(&self) -> T;
@@ -46,16 +43,6 @@ impl<T:Copy,U:Copy> CopyableTuple<T, U> for (T, U) {
4643
let (t, u) = *self;
4744
return (u, t);
4845
}
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-
}
5946
}
6047

6148
pub trait ImmutableTuple<T, U> {
@@ -124,158 +111,81 @@ impl<A:Copy,B:Copy> ExtendedTupleOps<A,B> for (~[A], ~[B]) {
124111
}
125112
}
126113

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 })+
137120
}
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};
142125

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;)+
154129
}
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-
}
165130

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+
)+
174140
}
175-
}
176-
}
177-
}
178-
#[inline(always)]
179-
fn ne(&self, other: &(A, B)) -> bool { !(*self).eq(other) }
180-
}
181141

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()),+)
194145
}
195146
}
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-
}
206147

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+
}
223154

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)
238158
}
239159
}
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-
}
250160

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+
}
252167

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+
}
263172

264-
$(impl<$($T),+> $name<$($T),+> for ($($T),+) {
265-
$(
266173
#[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())&&+
271176
}
272-
)+
273-
})+
177+
178+
#[inline(always)]
179+
fn gt(&self, other: &($($T),+)) -> bool {
180+
$(*self.$get_fn() > *other.$get_fn())&&+
181+
}
182+
}
183+
)+
274184
}
275185
)
276186
)
277187

278-
tuple_getters!(
188+
tuple_impls!(
279189
Tuple2 {
280190
fn n0 -> A { (ref a,_) => a }
281191
fn n1 -> B { (_,ref b) => b }

0 commit comments

Comments
 (0)