Skip to content

Commit 9653436

Browse files
committed
rustc: Make < and = into traits
1 parent 94720fc commit 9653436

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+2984
-327
lines changed

src/cargo/cargo.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ type package = {
2525
versions: ~[(~str, ~str)]
2626
};
2727

28+
impl package : cmp::Ord {
29+
pure fn lt(&&other: package) -> bool {
30+
if self.name.lt(other.name) { return true; }
31+
if other.name.lt(self.name) { return false; }
32+
if self.uuid.lt(other.uuid) { return true; }
33+
if other.uuid.lt(self.uuid) { return false; }
34+
if self.url.lt(other.url) { return true; }
35+
if other.url.lt(self.url) { return false; }
36+
if self.method.lt(other.method) { return true; }
37+
if other.method.lt(self.method) { return false; }
38+
if self.description.lt(other.description) { return true; }
39+
if other.description.lt(self.description) { return false; }
40+
if self.tags.lt(other.tags) { return true; }
41+
if other.tags.lt(self.tags) { return false; }
42+
if self.versions.lt(other.versions) { return true; }
43+
return false;
44+
}
45+
}
46+
2847
type local_package = {
2948
name: ~str,
3049
metaname: ~str,
@@ -74,6 +93,12 @@ type options = {
7493

7594
enum mode { system_mode, user_mode, local_mode }
7695

96+
impl mode : cmp::Eq {
97+
pure fn eq(&&other: mode) -> bool {
98+
(self as uint) == (other as uint)
99+
}
100+
}
101+
77102
fn opts() -> ~[getopts::Opt] {
78103
~[optflag(~"g"), optflag(~"G"), optflag(~"test"),
79104
optflag(~"h"), optflag(~"help")]

src/fuzzer/fuzzer.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import syntax::diagnostic;
88
enum test_mode { tm_converge, tm_run, }
99
type context = { mode: test_mode }; // + rng
1010

11+
impl test_mode : cmp::Eq {
12+
pure fn eq(&&other: test_mode) -> bool {
13+
(self as uint) == (other as uint)
14+
}
15+
}
16+
1117
fn write_file(filename: &Path, content: ~str) {
1218
result::get(
1319
io::file_writer(filename, ~[io::Create, io::Truncate]))

src/libcore/bool.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
//! Boolean logic
88
9+
import cmp::Eq;
10+
911
export not, and, or, xor, implies;
1012
export eq, ne, is_true, is_false;
1113
export from_str, to_str, all_values, to_bit;
@@ -67,6 +69,12 @@ fn all_values(blk: fn(v: bool)) {
6769
/// converts truth value to an 8 bit byte
6870
pure fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }
6971

72+
impl bool : cmp::Eq {
73+
pure fn eq(&&other: bool) -> bool {
74+
self == other
75+
}
76+
}
77+
7078
#[test]
7179
fn test_bool_from_str() {
7280
do all_values |v| {

src/libcore/box.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
#[forbid(deprecated_mode)];
55
#[forbid(deprecated_pattern)];
66

7+
import cmp::{Eq, Ord};
8+
79
export ptr_eq;
810

911
pure fn ptr_eq<T>(a: @T, b: @T) -> bool {
1012
//! Determine if two shared boxes point to the same object
1113
unsafe { ptr::addr_of(*a) == ptr::addr_of(*b) }
1214
}
1315

16+
impl<T:Eq> @const T : Eq {
17+
pure fn eq(&&other: @const T) -> bool { *self == *other }
18+
}
19+
20+
impl<T:Ord> @const T : Ord {
21+
pure fn lt(&&other: @const T) -> bool { *self < *other }
22+
}
23+
1424
#[test]
1525
fn test() {
1626
let x = @3;

src/libcore/char.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#[forbid(deprecated_mode)];
55
#[forbid(deprecated_pattern)];
66

7+
import cmp::Eq;
8+
79
/*
810
Lu Uppercase_Letter an uppercase letter
911
Ll Lowercase_Letter a lowercase letter
@@ -187,6 +189,10 @@ pure fn cmp(a: char, b: char) -> int {
187189
else { 0 }
188190
}
189191

192+
impl char: Eq {
193+
pure fn eq(&&other: char) -> bool { self == other }
194+
}
195+
190196
#[test]
191197
fn test_is_lowercase() {
192198
assert is_lowercase('a');

src/libcore/cmp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ trait Eq {
2828
}
2929

3030
pure fn lt<T: Ord>(v1: &T, v2: &T) -> bool {
31-
v1.lt(*v2)
31+
v1.lt(v2)
3232
}
3333

3434
pure fn le<T: Ord Eq>(v1: &T, v2: &T) -> bool {
35-
v1.lt(*v2) || v1.eq(*v2)
35+
v1.lt(v2) || v1.eq(v2)
3636
}
3737

3838
pure fn eq<T: Eq>(v1: &T, v2: &T) -> bool {
39-
v1.eq(*v2)
39+
v1.eq(v2)
4040
}

src/libcore/core.rc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export num;
5757
export path;
5858
export managed;
5959
export flate;
60+
export unit;
61+
export uniq;
6062

6163
// NDM seems to be necessary for resolve to work
6264
export option_iter;
@@ -183,6 +185,10 @@ mod at_vec;
183185
mod bool;
184186
#[warn(non_camel_case_types)]
185187
mod tuple;
188+
#[warn(non_camel_case_types)]
189+
mod unit;
190+
#[warn(non_camel_case_types)]
191+
mod uniq;
186192

187193
// Ubiquitous-utility-type modules
188194

src/libcore/core.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import PosixPath = path::PosixPath;
1818
import tuple::{TupleOps, ExtendedTupleOps};
1919
import str::{StrSlice, UniqueStr};
2020
import vec::{ConstVector, CopyableVector, ImmutableVector};
21-
import vec::{ImmutableCopyableVector};
22-
import iter::{BaseIter, ExtendedIter, CopyableIter, Times, TimesIx};
21+
import vec::{ImmutableEqVector, ImmutableCopyableVector};
22+
import iter::{BaseIter, ExtendedIter, EqIter, CopyableIter};
23+
import iter::{CopyableOrderedIter, Times, TimesIx};
2324
import num::Num;
2425
import ptr::Ptr;
2526
import to_str::ToStr;
@@ -33,8 +34,8 @@ export Num, Times, TimesIx;
3334
// The following exports are the common traits
3435
export StrSlice, UniqueStr;
3536
export ConstVector, CopyableVector, ImmutableVector;
36-
export ImmutableCopyableVector, IterTraitExtensions;
37-
export BaseIter, CopyableIter, ExtendedIter;
37+
export ImmutableEqVector, ImmutableCopyableVector, IterTraitExtensions;
38+
export BaseIter, CopyableIter, CopyableOrderedIter, ExtendedIter, EqIter;
3839
export TupleOps, ExtendedTupleOps;
3940
export Ptr;
4041
export ToStr;

src/libcore/either.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
//! A type that represents one of two alternatives
66
7+
import cmp::Eq;
78
import result::Result;
89

910
/// The either type
@@ -125,6 +126,25 @@ pure fn unwrap_right<T,U>(+eith: Either<T,U>) -> U {
125126
}
126127
}
127128

129+
impl<T:Eq,U:Eq> Either<T,U> : Eq {
130+
pure fn eq(&&other: Either<T,U>) -> bool {
131+
match self {
132+
Left(a) => {
133+
match other {
134+
Left(b) => a.eq(b),
135+
Right(_) => false
136+
}
137+
}
138+
Right(a) => {
139+
match other {
140+
Left(_) => false,
141+
Right(b) => a.eq(b)
142+
}
143+
}
144+
}
145+
}
146+
}
147+
128148
#[test]
129149
fn test_either_left() {
130150
let val = Left(10);

src/libcore/extfmt.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ debug!("hello, %s!", "world");
2525
2626
*/
2727

28+
import cmp::Eq;
2829
import option::{Some, None};
2930

3031

@@ -383,7 +384,24 @@ mod rt {
383384
count_implied => 1u
384385
};
385386
}
387+
386388
enum pad_mode { pad_signed, pad_unsigned, pad_nozero, pad_float }
389+
390+
impl pad_mode: Eq {
391+
pure fn eq(&&other: pad_mode) -> bool {
392+
match (self, other) {
393+
(pad_signed, pad_signed) => true,
394+
(pad_unsigned, pad_unsigned) => true,
395+
(pad_nozero, pad_nozero) => true,
396+
(pad_float, pad_float) => true,
397+
(pad_signed, _) => false,
398+
(pad_unsigned, _) => false,
399+
(pad_nozero, _) => false,
400+
(pad_float, _) => false
401+
}
402+
}
403+
}
404+
387405
fn pad(cv: conv, &s: ~str, mode: pad_mode) -> ~str {
388406
let uwidth : uint = match cv.width {
389407
count_implied => return s,

src/libcore/float.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import f64::{lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix};
4141
import f64::{modf, pow, round, sinh, tanh, tgamma, trunc};
4242
import f64::signbit;
4343
import f64::{j0, j1, jn, y0, y1, yn};
44+
import cmp::{Eq, Ord};
4445
import num::from_int;
4546

4647
const NaN: float = 0.0/0.0;
@@ -311,7 +312,7 @@ fn from_str(num: &str) -> Option<float> {
311312
}
312313
}
313314

314-
if (c == 'e') | (c == 'E') {//Examine exponent
315+
if (c == 'e') || (c == 'E') { //Examine exponent
315316
let mut exponent = 0u;
316317
let mut neg_exponent = false;
317318
if(pos < len) {
@@ -414,6 +415,14 @@ pure fn sin(x: float) -> float { f64::sin(x as f64) as float }
414415
pure fn cos(x: float) -> float { f64::cos(x as f64) as float }
415416
pure fn tan(x: float) -> float { f64::tan(x as f64) as float }
416417

418+
impl float: Eq {
419+
pure fn eq(&&other: float) -> bool { self == other }
420+
}
421+
422+
impl float: Ord {
423+
pure fn lt(&&other: float) -> bool { self < other }
424+
}
425+
417426
impl float: num::Num {
418427
pure fn add(&&other: float) -> float { return self + other; }
419428
pure fn sub(&&other: float) -> float { return self - other; }
@@ -521,7 +530,7 @@ fn test_to_str_inf() {
521530

522531
#[test]
523532
fn test_traits() {
524-
fn test<U:num::Num>(ten: &U) {
533+
fn test<U:num::Num cmp::Eq>(ten: &U) {
525534
assert (ten.to_int() == 10);
526535

527536
let two: U = from_int(2);

src/libcore/int-template.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn test_to_str() {
239239

240240
#[test]
241241
fn test_interfaces() {
242-
fn test<U:num::Num>(+ten: U) {
242+
fn test<U:num::Num cmp::Eq>(+ten: U) {
243243
assert (ten.to_int() == 10);
244244

245245
let two: U = from_int(2);

src/libcore/io.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Basic input/output
66

77
import result::Result;
88

9+
import cmp::Eq;
910
import dvec::DVec;
1011
import libc::{c_int, c_long, c_uint, c_void, size_t, ssize_t};
1112
import libc::consts::os::posix88::*;
@@ -324,6 +325,15 @@ enum FileFlag { Append, Create, Truncate, NoFlag, }
324325
// What type of writer are we?
325326
enum WriterType { Screen, File }
326327

328+
impl WriterType: Eq {
329+
pure fn eq(&&other: WriterType) -> bool {
330+
match (self, other) {
331+
(Screen, Screen) | (File, File) => true,
332+
(Screen, _) | (File, _) => false
333+
}
334+
}
335+
}
336+
327337
// FIXME (#2004): Seekable really should be orthogonal.
328338
// FIXME (#2004): eventually u64
329339
trait Writer {

src/libcore/iter-trait.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// workaround our lack of traits and lack of macros. See core.{rc,rs} for
33
// how this file is used.
44

5+
import cmp::{Eq, Ord};
56
import inst::{IMPL_T, EACH, SIZE_HINT};
67
export extensions;
78

@@ -17,6 +18,9 @@ impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
1718
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
1819
iter::foldl(self, b0, blk)
1920
}
21+
}
22+
23+
impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
2024
pure fn contains(x: A) -> bool { iter::contains(self, x) }
2125
pure fn count(x: A) -> uint { iter::count(self, x) }
2226
pure fn position(f: fn(A) -> bool) -> Option<uint> {
@@ -38,7 +42,11 @@ impl<A: copy> IMPL_T<A>: iter::CopyableIter<A> {
3842
// iter::flat_map_to_vec(self, op)
3943
// }
4044

45+
pure fn find(p: fn(A) -> bool) -> Option<A> { iter::find(self, p) }
46+
}
47+
48+
impl<A: copy Ord> IMPL_T<A>: iter::CopyableOrderedIter<A> {
4149
pure fn min() -> A { iter::min(self) }
4250
pure fn max() -> A { iter::max(self) }
43-
pure fn find(p: fn(A) -> bool) -> Option<A> { iter::find(self, p) }
4451
}
52+

0 commit comments

Comments
 (0)