about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2012-08-02 15:42:56 -0700
committerNiko Matsakis <niko@alum.mit.edu>2012-08-02 15:53:28 -0700
commit97452c0ca16238a2de5503aca07db26ff9e8ba63 (patch)
tree47ef430d1671ab297bc192009aa74a23723a42fc /src/libcore
parent476ce459bd3b687658e566c75d0fb73281450d67 (diff)
Remove modes from map API and replace with regions.
API is (for now) mostly by value, there are options to use it by
reference if you like.  Hash and equality functions must be pure
and by reference (forward looking to the day when something
like send_map becomes the standard map).
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/cmp.rs11
-rw-r--r--src/libcore/extfmt.rs26
-rw-r--r--src/libcore/int-template.rs35
-rw-r--r--src/libcore/int-template/int.rs2
-rw-r--r--src/libcore/io.rs2
-rw-r--r--src/libcore/os.rs2
-rw-r--r--src/libcore/str.rs64
-rw-r--r--src/libcore/sys.rs15
-rw-r--r--src/libcore/uint-template.rs24
-rw-r--r--src/libcore/uint-template/uint.rs14
-rw-r--r--src/libcore/vec.rs26
11 files changed, 125 insertions, 96 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs
index d10b9603af0..a336e18a63d 100644
--- a/src/libcore/cmp.rs
+++ b/src/libcore/cmp.rs
@@ -8,3 +8,14 @@ trait eq {
     pure fn eq(&&other: self) -> bool;
 }
 
+pure fn lt<T: ord>(v1: &T, v2: &T) -> bool {
+    v1.lt(*v2)
+}
+
+pure fn le<T: ord eq>(v1: &T, v2: &T) -> bool {
+    v1.lt(*v2) || v1.eq(*v2)
+}
+
+pure fn eq<T: eq>(v1: &T, v2: &T) -> bool {
+    v1.eq(*v2)
+}
diff --git a/src/libcore/extfmt.rs b/src/libcore/extfmt.rs
index a4e3b2144c9..49313295edc 100644
--- a/src/libcore/extfmt.rs
+++ b/src/libcore/extfmt.rs
@@ -96,13 +96,13 @@ mod ct {
         while i < lim {
             let size = str::utf8_char_width(s[i]);
             let curr = str::slice(s, i, i+size);
-            if str::eq(curr, ~"%") {
+            if curr == ~"%" {
                 i += 1u;
                 if i >= lim {
                     error(~"unterminated conversion at end of string");
                 }
                 let curr2 = str::slice(s, i, i+1u);
-                if str::eq(curr2, ~"%") {
+                if curr2 == ~"%" {
                     buf += curr2;
                     i += 1u;
                 } else {
@@ -232,27 +232,27 @@ mod ct {
         // FIXME (#2249): Do we really want two signed types here?
         // How important is it to be printf compatible?
         let t =
-            if str::eq(tstr, ~"b") {
+            if tstr == ~"b" {
                 ty_bool
-            } else if str::eq(tstr, ~"s") {
+            } else if tstr == ~"s" {
                 ty_str
-            } else if str::eq(tstr, ~"c") {
+            } else if tstr == ~"c" {
                 ty_char
-            } else if str::eq(tstr, ~"d") || str::eq(tstr, ~"i") {
+            } else if tstr == ~"d" || tstr == ~"i" {
                 ty_int(signed)
-            } else if str::eq(tstr, ~"u") {
+            } else if tstr == ~"u" {
                 ty_int(unsigned)
-            } else if str::eq(tstr, ~"x") {
+            } else if tstr == ~"x" {
                 ty_hex(case_lower)
-            } else if str::eq(tstr, ~"X") {
+            } else if tstr == ~"X" {
                 ty_hex(case_upper)
-            } else if str::eq(tstr, ~"t") {
+            } else if tstr == ~"t" {
                 ty_bits
-            } else if str::eq(tstr, ~"o") {
+            } else if tstr == ~"o" {
                 ty_octal
-            } else if str::eq(tstr, ~"f") {
+            } else if tstr == ~"f" {
                 ty_float
-            } else if str::eq(tstr, ~"?") {
+            } else if tstr == ~"?" {
                 ty_poly
             } else { error(~"unknown type in conversion: " + tstr) };
         return {ty: t, next: i + 1u};
diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs
index 46413517a5b..82b6d00d909 100644
--- a/src/libcore/int-template.rs
+++ b/src/libcore/int-template.rs
@@ -19,18 +19,18 @@ const max_value: T = min_value - 1 as T;
 pure fn min(&&x: T, &&y: T) -> T { if x < y { x } else { y } }
 pure fn max(&&x: T, &&y: T) -> T { if x > y { x } else { y } }
 
-pure fn add(&&x: T, &&y: T) -> T { x + y }
-pure fn sub(&&x: T, &&y: T) -> T { x - y }
-pure fn mul(&&x: T, &&y: T) -> T { x * y }
-pure fn div(&&x: T, &&y: T) -> T { x / y }
-pure fn rem(&&x: T, &&y: T) -> T { x % y }
-
-pure fn lt(&&x: T, &&y: T) -> bool { x < y }
-pure fn le(&&x: T, &&y: T) -> bool { x <= y }
-pure fn eq(&&x: T, &&y: T) -> bool { x == y }
-pure fn ne(&&x: T, &&y: T) -> bool { x != y }
-pure fn ge(&&x: T, &&y: T) -> bool { x >= y }
-pure fn gt(&&x: T, &&y: T) -> bool { x > y }
+pure fn add(x: &T, y: &T) -> T { *x + *y }
+pure fn sub(x: &T, y: &T) -> T { *x - *y }
+pure fn mul(x: &T, y: &T) -> T { *x * *y }
+pure fn div(x: &T, y: &T) -> T { *x / *y }
+pure fn rem(x: &T, y: &T) -> T { *x % *y }
+
+pure fn lt(x: &T, y: &T) -> bool { *x < *y }
+pure fn le(x: &T, y: &T) -> bool { *x <= *y }
+pure fn eq(x: &T, y: &T) -> bool { *x == *y }
+pure fn ne(x: &T, y: &T) -> bool { *x != *y }
+pure fn ge(x: &T, y: &T) -> bool { *x >= *y }
+pure fn gt(x: &T, y: &T) -> bool { *x > *y }
 
 pure fn is_positive(x: T) -> bool { x > 0 as T }
 pure fn is_negative(x: T) -> bool { x < 0 as T }
@@ -221,12 +221,11 @@ fn test_parse_buf() {
 
 #[test]
 fn test_to_str() {
-    import str::eq;
-    assert (eq(to_str(0 as T, 10u), ~"0"));
-    assert (eq(to_str(1 as T, 10u), ~"1"));
-    assert (eq(to_str(-1 as T, 10u), ~"-1"));
-    assert (eq(to_str(127 as T, 16u), ~"7f"));
-    assert (eq(to_str(100 as T, 10u), ~"100"));
+    assert (to_str(0 as T, 10u) == ~"0");
+    assert (to_str(1 as T, 10u) == ~"1");
+    assert (to_str(-1 as T, 10u) == ~"-1");
+    assert (to_str(127 as T, 16u) == ~"7f");
+    assert (to_str(100 as T, 10u) == ~"100");
 }
 
 #[test]
diff --git a/src/libcore/int-template/int.rs b/src/libcore/int-template/int.rs
index 500316de2f7..2ed6155edc0 100644
--- a/src/libcore/int-template/int.rs
+++ b/src/libcore/int-template/int.rs
@@ -7,7 +7,7 @@ const bits: T = 32 as T;
 const bits: T = 64 as T;
 
 /// Produce a uint suitable for use in a hash table
-pure fn hash(&&x: int) -> uint { return x as uint; }
+pure fn hash(x: &int) -> uint { *x as uint }
 
 /// Returns `base` raised to the power of `exponent`
 fn pow(base: int, exponent: uint) -> int {
diff --git a/src/libcore/io.rs b/src/libcore/io.rs
index 1831938496a..4b5898279dd 100644
--- a/src/libcore/io.rs
+++ b/src/libcore/io.rs
@@ -811,7 +811,7 @@ mod tests {
         let inp: io::reader = result::get(io::file_reader(tmpfile));
         let frood2: ~str = inp.read_c_str();
         log(debug, frood2);
-        assert (str::eq(frood, frood2));
+        assert frood == frood2;
     }
 
     #[test]
diff --git a/src/libcore/os.rs b/src/libcore/os.rs
index f758c1b62cb..a2e3ca02bc3 100644
--- a/src/libcore/os.rs
+++ b/src/libcore/os.rs
@@ -579,7 +579,7 @@ fn list_dir(p: path) -> ~[~str] {
     }
 
     do rustrt::rust_list_files(star(p)).filter |filename| {
-        !str::eq(filename, ~".") && !str::eq(filename, ~"..")
+        filename != ~"." && filename != ~".."
     }
 }
 
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index cdc3a826b67..9b76f64bcc5 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -637,8 +637,8 @@ pure fn replace(s: &str, from: &str, to: &str) -> ~str {
 Section: Comparing strings
 */
 
-/// Bytewise string equality
-pure fn eq(&&a: ~str, &&b: ~str) -> bool {
+/// Bytewise slice equality
+pure fn eq_slice(a: &str, b: &str) -> bool {
     // FIXME (#2627): This should just be "a == b" but that calls into the
     // shape code.
     let a_len = a.len();
@@ -655,12 +655,17 @@ pure fn eq(&&a: ~str, &&b: ~str) -> bool {
     return true;
 }
 
+/// Bytewise string equality
+pure fn eq(a: &~str, b: &~str) -> bool {
+    eq_slice(*a, *b)
+}
+
 /// Bytewise less than or equal
-pure fn le(&&a: ~str, &&b: ~str) -> bool { a <= b }
+pure fn le(a: &~str, b: &~str) -> bool { *a <= *b }
 
 /// String hash function
-pure fn hash(&&s: ~str) -> uint {
-    let x = do as_bytes(s) |bytes| {
+pure fn hash(s: &~str) -> uint {
+    let x = do as_bytes(*s) |bytes| {
         hash::hash_bytes(bytes)
     };
     return x as uint;
@@ -2070,17 +2075,17 @@ mod tests {
 
     #[test]
     fn test_eq() {
-        assert (eq(~"", ~""));
-        assert (eq(~"foo", ~"foo"));
-        assert (!eq(~"foo", ~"bar"));
+        assert (eq(&~"", &~""));
+        assert (eq(&~"foo", &~"foo"));
+        assert (!eq(&~"foo", &~"bar"));
     }
 
     #[test]
     fn test_le() {
-        assert (le(~"", ~""));
-        assert (le(~"", ~"foo"));
-        assert (le(~"foo", ~"foo"));
-        assert (!eq(~"foo", ~"bar"));
+        assert (le(&~"", &~""));
+        assert (le(&~"", &~"foo"));
+        assert (le(&~"foo", &~"foo"));
+        assert (!eq(&~"foo", &~"bar"));
     }
 
     #[test]
@@ -2220,7 +2225,7 @@ mod tests {
     fn test_split_str() {
         fn t(s: ~str, sep: &a/str, i: int, k: ~str) {
             let v = split_str(s, sep);
-            assert eq(v[i], k);
+            assert v[i] == k;
         }
 
         t(~"--1233345--", ~"12345", 0, ~"--1233345--");
@@ -2348,7 +2353,7 @@ mod tests {
     #[test]
     fn test_substr() {
         fn t(a: ~str, b: ~str, start: int) {
-            assert (eq(substr(a, start as uint, len(b)), b));
+            assert substr(a, start as uint, len(b)) == b;
         }
         t(~"hello", ~"llo", 2);
         t(~"hello", ~"el", 1);
@@ -2357,7 +2362,7 @@ mod tests {
 
     #[test]
     fn test_concat() {
-        fn t(v: ~[~str], s: ~str) { assert (eq(concat(v), s)); }
+        fn t(v: ~[~str], s: ~str) { assert concat(v) == s; }
         t(~[~"you", ~"know", ~"I'm", ~"no", ~"good"], ~"youknowI'mnogood");
         let v: ~[~str] = ~[];
         t(v, ~"");
@@ -2367,7 +2372,7 @@ mod tests {
     #[test]
     fn test_connect() {
         fn t(v: ~[~str], sep: ~str, s: ~str) {
-            assert (eq(connect(v, sep), s));
+            assert connect(v, sep) == s;
         }
         t(~[~"you", ~"know", ~"I'm", ~"no", ~"good"],
           ~" ", ~"you know I'm no good");
@@ -2385,7 +2390,7 @@ mod tests {
         let input = ~"abcDEF" + unicode + ~"xyz:.;";
         let expected = ~"ABCDEF" + unicode + ~"XYZ:.;";
         let actual = to_upper(input);
-        assert (eq(expected, actual));
+        assert expected == actual;
     }
 
     #[test]
@@ -2398,9 +2403,9 @@ mod tests {
     #[test]
     fn test_unsafe_slice() {
         unsafe {
-            assert (eq(~"ab", unsafe::slice_bytes(~"abc", 0u, 2u)));
-            assert (eq(~"bc", unsafe::slice_bytes(~"abc", 1u, 3u)));
-            assert (eq(~"", unsafe::slice_bytes(~"abc", 1u, 1u)));
+            assert ~"ab" == unsafe::slice_bytes(~"abc", 0u, 2u);
+            assert ~"bc" == unsafe::slice_bytes(~"abc", 1u, 3u);
+            assert ~"" == unsafe::slice_bytes(~"abc", 1u, 1u);
             fn a_million_letter_a() -> ~str {
                 let mut i = 0;
                 let mut rs = ~"";
@@ -2413,9 +2418,8 @@ mod tests {
                 while i < 100000 { push_str(rs, ~"aaaaa"); i += 1; }
                 return rs;
             }
-            assert eq(half_a_million_letter_a(),
-                      unsafe::slice_bytes(a_million_letter_a(),
-                                          0u, 500000u));
+            assert half_a_million_letter_a() ==
+                unsafe::slice_bytes(a_million_letter_a(), 0u, 500000u);
         }
     }
 
@@ -2501,10 +2505,10 @@ mod tests {
 
     #[test]
     fn test_slice() {
-        assert (eq(~"ab", slice(~"abc", 0u, 2u)));
-        assert (eq(~"bc", slice(~"abc", 1u, 3u)));
-        assert (eq(~"", slice(~"abc", 1u, 1u)));
-        assert (eq(~"\u65e5", slice(~"\u65e5\u672c", 0u, 3u)));
+        assert ~"ab" == slice(~"abc", 0u, 2u);
+        assert ~"bc" == slice(~"abc", 1u, 3u);
+        assert ~"" == slice(~"abc", 1u, 1u);
+        assert ~"\u65e5" == slice(~"\u65e5\u672c", 0u, 3u);
 
         let data = ~"ประเทศไทย中华";
         assert ~"ป" == slice(data, 0u, 3u);
@@ -2524,8 +2528,8 @@ mod tests {
             while i < 100000 { push_str(rs, ~"华华华华华"); i += 1; }
             return rs;
         }
-        assert eq(half_a_million_letter_X(),
-                  slice(a_million_letter_X(), 0u, 3u * 500000u));
+        assert half_a_million_letter_X() ==
+            slice(a_million_letter_X(), 0u, 3u * 500000u);
     }
 
     #[test]
@@ -2709,7 +2713,7 @@ mod tests {
             let s = ~"hello";
             let sb = as_buf(s, |b, _l| b);
             let s_cstr = unsafe::from_buf(sb);
-            assert (eq(s_cstr, s));
+            assert s_cstr == s;
         }
     }
 
diff --git a/src/libcore/sys.rs b/src/libcore/sys.rs
index 76c0e9f0753..f0075d84189 100644
--- a/src/libcore/sys.rs
+++ b/src/libcore/sys.rs
@@ -8,6 +8,7 @@ export pref_align_of;
 export refcount;
 export log_str;
 export lock_and_signal, condition, methods;
+export shape_eq, shape_lt, shape_le;
 
 import task::atomically;
 
@@ -39,6 +40,20 @@ extern mod rusti {
     fn min_align_of<T>() -> uint;
 }
 
+/// Compares contents of two pointers using the default method.
+/// Equivalent to `*x1 == *x2`.  Useful for hashtables.
+pure fn shape_eq<T>(x1: &T, x2: &T) -> bool {
+    *x1 == *x2
+}
+
+pure fn shape_lt<T>(x1: &T, x2: &T) -> bool {
+    *x1 < *x2
+}
+
+pure fn shape_le<T>(x1: &T, x2: &T) -> bool {
+    *x1 < *x2
+}
+
 /**
  * Returns a pointer to a type descriptor.
  *
diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs
index 13746df2621..49ea87785ec 100644
--- a/src/libcore/uint-template.rs
+++ b/src/libcore/uint-template.rs
@@ -19,18 +19,18 @@ const max_value: T = 0 as T - 1 as T;
 pure fn min(&&x: T, &&y: T) -> T { if x < y { x } else { y } }
 pure fn max(&&x: T, &&y: T) -> T { if x > y { x } else { y } }
 
-pure fn add(&&x: T, &&y: T) -> T { x + y }
-pure fn sub(&&x: T, &&y: T) -> T { x - y }
-pure fn mul(&&x: T, &&y: T) -> T { x * y }
-pure fn div(&&x: T, &&y: T) -> T { x / y }
-pure fn rem(&&x: T, &&y: T) -> T { x % y }
-
-pure fn lt(&&x: T, &&y: T) -> bool { x < y }
-pure fn le(&&x: T, &&y: T) -> bool { x <= y }
-pure fn eq(&&x: T, &&y: T) -> bool { x == y }
-pure fn ne(&&x: T, &&y: T) -> bool { x != y }
-pure fn ge(&&x: T, &&y: T) -> bool { x >= y }
-pure fn gt(&&x: T, &&y: T) -> bool { x > y }
+pure fn add(x: &T, y: &T) -> T { *x + *y }
+pure fn sub(x: &T, y: &T) -> T { *x - *y }
+pure fn mul(x: &T, y: &T) -> T { *x * *y }
+pure fn div(x: &T, y: &T) -> T { *x / *y }
+pure fn rem(x: &T, y: &T) -> T { *x % *y }
+
+pure fn lt(x: &T, y: &T) -> bool { *x < *y }
+pure fn le(x: &T, y: &T) -> bool { *x <= *y }
+pure fn eq(x: &T, y: &T) -> bool { *x == *y }
+pure fn ne(x: &T, y: &T) -> bool { *x != *y }
+pure fn ge(x: &T, y: &T) -> bool { *x >= *y }
+pure fn gt(x: &T, y: &T) -> bool { *x > *y }
 
 pure fn is_positive(x: T) -> bool { x > 0 as T }
 pure fn is_negative(x: T) -> bool { x < 0 as T }
diff --git a/src/libcore/uint-template/uint.rs b/src/libcore/uint-template/uint.rs
index 401cb8c04c4..eb8b08a4fe5 100644
--- a/src/libcore/uint-template/uint.rs
+++ b/src/libcore/uint-template/uint.rs
@@ -13,9 +13,9 @@ type T = uint;
  * The smallest integer `q` such that `x/y <= q`.
  */
 pure fn div_ceil(x: uint, y: uint) -> uint {
-    let div = div(x, y);
-    if x % y == 0u { return div;}
-    else { return div + 1u; }
+    let div = x / y;
+    if x % y == 0u { div }
+    else { div + 1u }
 }
 
 /**
@@ -31,9 +31,9 @@ pure fn div_ceil(x: uint, y: uint) -> uint {
  * The integer `q` closest to `x/y`.
  */
 pure fn div_round(x: uint, y: uint) -> uint {
-    let div = div(x, y);
-    if x % y * 2u  < y { return div;}
-    else { return div + 1u; }
+    let div = x / y;
+    if x % y * 2u  < y { div }
+    else { div + 1u }
 }
 
 /**
@@ -54,7 +54,7 @@ pure fn div_round(x: uint, y: uint) -> uint {
 pure fn div_floor(x: uint, y: uint) -> uint { return x / y; }
 
 /// Produce a uint suitable for use in a hash table
-pure fn hash(&&x: uint) -> uint { return x; }
+pure fn hash(x: &uint) -> uint { *x }
 
 /**
  * Iterate over the range [`lo`..`hi`), or stop when requested
diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs
index 670df1bd00b..c02560bf6c3 100644
--- a/src/libcore/vec.rs
+++ b/src/libcore/vec.rs
@@ -1654,13 +1654,13 @@ mod u8 {
     export memcpy, memmove;
 
     /// Bytewise string comparison
-    pure fn cmp(&&a: ~[u8], &&b: ~[u8]) -> int {
-        let a_len = len(a);
-        let b_len = len(b);
+    pure fn cmp(a: &~[u8], b: &~[u8]) -> int {
+        let a_len = len(*a);
+        let b_len = len(*b);
         let n = uint::min(a_len, b_len) as libc::size_t;
         let r = unsafe {
-            libc::memcmp(unsafe::to_ptr(a) as *libc::c_void,
-                         unsafe::to_ptr(b) as *libc::c_void, n) as int
+            libc::memcmp(unsafe::to_ptr(*a) as *libc::c_void,
+                         unsafe::to_ptr(*b) as *libc::c_void, n) as int
         };
 
         if r != 0 { r } else {
@@ -1675,26 +1675,26 @@ mod u8 {
     }
 
     /// Bytewise less than or equal
-    pure fn lt(&&a: ~[u8], &&b: ~[u8]) -> bool { cmp(a, b) < 0 }
+    pure fn lt(a: &~[u8], b: &~[u8]) -> bool { cmp(a, b) < 0 }
 
     /// Bytewise less than or equal
-    pure fn le(&&a: ~[u8], &&b: ~[u8]) -> bool { cmp(a, b) <= 0 }
+    pure fn le(a: &~[u8], b: &~[u8]) -> bool { cmp(a, b) <= 0 }
 
     /// Bytewise equality
-    pure fn eq(&&a: ~[u8], &&b: ~[u8]) -> bool { unsafe { cmp(a, b) == 0 } }
+    pure fn eq(a: &~[u8], b: &~[u8]) -> bool { cmp(a, b) == 0 }
 
     /// Bytewise inequality
-    pure fn ne(&&a: ~[u8], &&b: ~[u8]) -> bool { unsafe { cmp(a, b) != 0 } }
+    pure fn ne(a: &~[u8], b: &~[u8]) -> bool { cmp(a, b) != 0 }
 
     /// Bytewise greater than or equal
-    pure fn ge(&&a: ~[u8], &&b: ~[u8]) -> bool { cmp(a, b) >= 0 }
+    pure fn ge(a: &~[u8], b: &~[u8]) -> bool { cmp(a, b) >= 0 }
 
     /// Bytewise greater than
-    pure fn gt(&&a: ~[u8], &&b: ~[u8]) -> bool { cmp(a, b) > 0 }
+    pure fn gt(a: &~[u8], b: &~[u8]) -> bool { cmp(a, b) > 0 }
 
     /// Byte-vec hash function
-    fn hash(&&s: ~[u8]) -> uint {
-        hash::hash_bytes(s) as uint
+    fn hash(s: &~[u8]) -> uint {
+        hash::hash_bytes(*s) as uint
     }
 
     /**