Skip to content

Commit 97452c0

Browse files
committed
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).
1 parent 476ce45 commit 97452c0

Some content is hidden

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

62 files changed

+579
-474
lines changed

doc/rust.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ An example of imports:
734734
~~~~
735735
import foo = core::info;
736736
import core::float::sin;
737-
import core::str::{slice, hash};
737+
import core::str::{slice, to_upper};
738738
import core::option::some;
739739
740740
fn main() {
@@ -745,8 +745,8 @@ fn main() {
745745
log(info, some(1.0));
746746
747747
// Equivalent to 'log(core::info,
748-
// core::str::hash(core::str::slice(~"foo", 0u, 1u)));'
749-
log(info, hash(slice(~"foo", 0u, 1u)));
748+
// core::str::to_upper(core::str::slice(~"foo", 0u, 1u)));'
749+
log(info, to_upper(slice(~"foo", 0u, 1u)));
750750
}
751751
~~~~
752752

src/cargo/cargo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ fn print_pkg(s: source, p: package) {
14721472
fn print_source(s: source) {
14731473
info(s.name + ~" (" + s.url + ~")");
14741474

1475-
let pks = sort::merge_sort(|a, b| a < b, copy s.packages);
1475+
let pks = sort::merge_sort(sys::shape_lt, copy s.packages);
14761476
let l = vec::len(pks);
14771477

14781478
print(io::with_str_writer(|writer| {

src/libcore/cmp.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@ trait eq {
88
pure fn eq(&&other: self) -> bool;
99
}
1010

11+
pure fn lt<T: ord>(v1: &T, v2: &T) -> bool {
12+
v1.lt(*v2)
13+
}
14+
15+
pure fn le<T: ord eq>(v1: &T, v2: &T) -> bool {
16+
v1.lt(*v2) || v1.eq(*v2)
17+
}
18+
19+
pure fn eq<T: eq>(v1: &T, v2: &T) -> bool {
20+
v1.eq(*v2)
21+
}

src/libcore/extfmt.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ mod ct {
9696
while i < lim {
9797
let size = str::utf8_char_width(s[i]);
9898
let curr = str::slice(s, i, i+size);
99-
if str::eq(curr, ~"%") {
99+
if curr == ~"%" {
100100
i += 1u;
101101
if i >= lim {
102102
error(~"unterminated conversion at end of string");
103103
}
104104
let curr2 = str::slice(s, i, i+1u);
105-
if str::eq(curr2, ~"%") {
105+
if curr2 == ~"%" {
106106
buf += curr2;
107107
i += 1u;
108108
} else {
@@ -232,27 +232,27 @@ mod ct {
232232
// FIXME (#2249): Do we really want two signed types here?
233233
// How important is it to be printf compatible?
234234
let t =
235-
if str::eq(tstr, ~"b") {
235+
if tstr == ~"b" {
236236
ty_bool
237-
} else if str::eq(tstr, ~"s") {
237+
} else if tstr == ~"s" {
238238
ty_str
239-
} else if str::eq(tstr, ~"c") {
239+
} else if tstr == ~"c" {
240240
ty_char
241-
} else if str::eq(tstr, ~"d") || str::eq(tstr, ~"i") {
241+
} else if tstr == ~"d" || tstr == ~"i" {
242242
ty_int(signed)
243-
} else if str::eq(tstr, ~"u") {
243+
} else if tstr == ~"u" {
244244
ty_int(unsigned)
245-
} else if str::eq(tstr, ~"x") {
245+
} else if tstr == ~"x" {
246246
ty_hex(case_lower)
247-
} else if str::eq(tstr, ~"X") {
247+
} else if tstr == ~"X" {
248248
ty_hex(case_upper)
249-
} else if str::eq(tstr, ~"t") {
249+
} else if tstr == ~"t" {
250250
ty_bits
251-
} else if str::eq(tstr, ~"o") {
251+
} else if tstr == ~"o" {
252252
ty_octal
253-
} else if str::eq(tstr, ~"f") {
253+
} else if tstr == ~"f" {
254254
ty_float
255-
} else if str::eq(tstr, ~"?") {
255+
} else if tstr == ~"?" {
256256
ty_poly
257257
} else { error(~"unknown type in conversion: " + tstr) };
258258
return {ty: t, next: i + 1u};

src/libcore/int-template.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ const max_value: T = min_value - 1 as T;
1919
pure fn min(&&x: T, &&y: T) -> T { if x < y { x } else { y } }
2020
pure fn max(&&x: T, &&y: T) -> T { if x > y { x } else { y } }
2121

22-
pure fn add(&&x: T, &&y: T) -> T { x + y }
23-
pure fn sub(&&x: T, &&y: T) -> T { x - y }
24-
pure fn mul(&&x: T, &&y: T) -> T { x * y }
25-
pure fn div(&&x: T, &&y: T) -> T { x / y }
26-
pure fn rem(&&x: T, &&y: T) -> T { x % y }
27-
28-
pure fn lt(&&x: T, &&y: T) -> bool { x < y }
29-
pure fn le(&&x: T, &&y: T) -> bool { x <= y }
30-
pure fn eq(&&x: T, &&y: T) -> bool { x == y }
31-
pure fn ne(&&x: T, &&y: T) -> bool { x != y }
32-
pure fn ge(&&x: T, &&y: T) -> bool { x >= y }
33-
pure fn gt(&&x: T, &&y: T) -> bool { x > y }
22+
pure fn add(x: &T, y: &T) -> T { *x + *y }
23+
pure fn sub(x: &T, y: &T) -> T { *x - *y }
24+
pure fn mul(x: &T, y: &T) -> T { *x * *y }
25+
pure fn div(x: &T, y: &T) -> T { *x / *y }
26+
pure fn rem(x: &T, y: &T) -> T { *x % *y }
27+
28+
pure fn lt(x: &T, y: &T) -> bool { *x < *y }
29+
pure fn le(x: &T, y: &T) -> bool { *x <= *y }
30+
pure fn eq(x: &T, y: &T) -> bool { *x == *y }
31+
pure fn ne(x: &T, y: &T) -> bool { *x != *y }
32+
pure fn ge(x: &T, y: &T) -> bool { *x >= *y }
33+
pure fn gt(x: &T, y: &T) -> bool { *x > *y }
3434

3535
pure fn is_positive(x: T) -> bool { x > 0 as T }
3636
pure fn is_negative(x: T) -> bool { x < 0 as T }
@@ -221,12 +221,11 @@ fn test_parse_buf() {
221221

222222
#[test]
223223
fn test_to_str() {
224-
import str::eq;
225-
assert (eq(to_str(0 as T, 10u), ~"0"));
226-
assert (eq(to_str(1 as T, 10u), ~"1"));
227-
assert (eq(to_str(-1 as T, 10u), ~"-1"));
228-
assert (eq(to_str(127 as T, 16u), ~"7f"));
229-
assert (eq(to_str(100 as T, 10u), ~"100"));
224+
assert (to_str(0 as T, 10u) == ~"0");
225+
assert (to_str(1 as T, 10u) == ~"1");
226+
assert (to_str(-1 as T, 10u) == ~"-1");
227+
assert (to_str(127 as T, 16u) == ~"7f");
228+
assert (to_str(100 as T, 10u) == ~"100");
230229
}
231230

232231
#[test]

src/libcore/int-template/int.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const bits: T = 32 as T;
77
const bits: T = 64 as T;
88

99
/// Produce a uint suitable for use in a hash table
10-
pure fn hash(&&x: int) -> uint { return x as uint; }
10+
pure fn hash(x: &int) -> uint { *x as uint }
1111

1212
/// Returns `base` raised to the power of `exponent`
1313
fn pow(base: int, exponent: uint) -> int {

src/libcore/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ mod tests {
811811
let inp: io::reader = result::get(io::file_reader(tmpfile));
812812
let frood2: ~str = inp.read_c_str();
813813
log(debug, frood2);
814-
assert (str::eq(frood, frood2));
814+
assert frood == frood2;
815815
}
816816

817817
#[test]

src/libcore/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ fn list_dir(p: path) -> ~[~str] {
579579
}
580580
581581
do rustrt::rust_list_files(star(p)).filter |filename| {
582-
!str::eq(filename, ~".") && !str::eq(filename, ~"..")
582+
filename != ~"." && filename != ~".."
583583
}
584584
}
585585

src/libcore/str.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,8 @@ pure fn replace(s: &str, from: &str, to: &str) -> ~str {
637637
Section: Comparing strings
638638
*/
639639

640-
/// Bytewise string equality
641-
pure fn eq(&&a: ~str, &&b: ~str) -> bool {
640+
/// Bytewise slice equality
641+
pure fn eq_slice(a: &str, b: &str) -> bool {
642642
// FIXME (#2627): This should just be "a == b" but that calls into the
643643
// shape code.
644644
let a_len = a.len();
@@ -655,12 +655,17 @@ pure fn eq(&&a: ~str, &&b: ~str) -> bool {
655655
return true;
656656
}
657657

658+
/// Bytewise string equality
659+
pure fn eq(a: &~str, b: &~str) -> bool {
660+
eq_slice(*a, *b)
661+
}
662+
658663
/// Bytewise less than or equal
659-
pure fn le(&&a: ~str, &&b: ~str) -> bool { a <= b }
664+
pure fn le(a: &~str, b: &~str) -> bool { *a <= *b }
660665

661666
/// String hash function
662-
pure fn hash(&&s: ~str) -> uint {
663-
let x = do as_bytes(s) |bytes| {
667+
pure fn hash(s: &~str) -> uint {
668+
let x = do as_bytes(*s) |bytes| {
664669
hash::hash_bytes(bytes)
665670
};
666671
return x as uint;
@@ -2070,17 +2075,17 @@ mod tests {
20702075

20712076
#[test]
20722077
fn test_eq() {
2073-
assert (eq(~"", ~""));
2074-
assert (eq(~"foo", ~"foo"));
2075-
assert (!eq(~"foo", ~"bar"));
2078+
assert (eq(&~"", &~""));
2079+
assert (eq(&~"foo", &~"foo"));
2080+
assert (!eq(&~"foo", &~"bar"));
20762081
}
20772082

20782083
#[test]
20792084
fn test_le() {
2080-
assert (le(~"", ~""));
2081-
assert (le(~"", ~"foo"));
2082-
assert (le(~"foo", ~"foo"));
2083-
assert (!eq(~"foo", ~"bar"));
2085+
assert (le(&~"", &~""));
2086+
assert (le(&~"", &~"foo"));
2087+
assert (le(&~"foo", &~"foo"));
2088+
assert (!eq(&~"foo", &~"bar"));
20842089
}
20852090

20862091
#[test]
@@ -2220,7 +2225,7 @@ mod tests {
22202225
fn test_split_str() {
22212226
fn t(s: ~str, sep: &a/str, i: int, k: ~str) {
22222227
let v = split_str(s, sep);
2223-
assert eq(v[i], k);
2228+
assert v[i] == k;
22242229
}
22252230

22262231
t(~"--1233345--", ~"12345", 0, ~"--1233345--");
@@ -2348,7 +2353,7 @@ mod tests {
23482353
#[test]
23492354
fn test_substr() {
23502355
fn t(a: ~str, b: ~str, start: int) {
2351-
assert (eq(substr(a, start as uint, len(b)), b));
2356+
assert substr(a, start as uint, len(b)) == b;
23522357
}
23532358
t(~"hello", ~"llo", 2);
23542359
t(~"hello", ~"el", 1);
@@ -2357,7 +2362,7 @@ mod tests {
23572362

23582363
#[test]
23592364
fn test_concat() {
2360-
fn t(v: ~[~str], s: ~str) { assert (eq(concat(v), s)); }
2365+
fn t(v: ~[~str], s: ~str) { assert concat(v) == s; }
23612366
t(~[~"you", ~"know", ~"I'm", ~"no", ~"good"], ~"youknowI'mnogood");
23622367
let v: ~[~str] = ~[];
23632368
t(v, ~"");
@@ -2367,7 +2372,7 @@ mod tests {
23672372
#[test]
23682373
fn test_connect() {
23692374
fn t(v: ~[~str], sep: ~str, s: ~str) {
2370-
assert (eq(connect(v, sep), s));
2375+
assert connect(v, sep) == s;
23712376
}
23722377
t(~[~"you", ~"know", ~"I'm", ~"no", ~"good"],
23732378
~" ", ~"you know I'm no good");
@@ -2385,7 +2390,7 @@ mod tests {
23852390
let input = ~"abcDEF" + unicode + ~"xyz:.;";
23862391
let expected = ~"ABCDEF" + unicode + ~"XYZ:.;";
23872392
let actual = to_upper(input);
2388-
assert (eq(expected, actual));
2393+
assert expected == actual;
23892394
}
23902395
23912396
#[test]
@@ -2398,9 +2403,9 @@ mod tests {
23982403
#[test]
23992404
fn test_unsafe_slice() {
24002405
unsafe {
2401-
assert (eq(~"ab", unsafe::slice_bytes(~"abc", 0u, 2u)));
2402-
assert (eq(~"bc", unsafe::slice_bytes(~"abc", 1u, 3u)));
2403-
assert (eq(~"", unsafe::slice_bytes(~"abc", 1u, 1u)));
2406+
assert ~"ab" == unsafe::slice_bytes(~"abc", 0u, 2u);
2407+
assert ~"bc" == unsafe::slice_bytes(~"abc", 1u, 3u);
2408+
assert ~"" == unsafe::slice_bytes(~"abc", 1u, 1u);
24042409
fn a_million_letter_a() -> ~str {
24052410
let mut i = 0;
24062411
let mut rs = ~"";
@@ -2413,9 +2418,8 @@ mod tests {
24132418
while i < 100000 { push_str(rs, ~"aaaaa"); i += 1; }
24142419
return rs;
24152420
}
2416-
assert eq(half_a_million_letter_a(),
2417-
unsafe::slice_bytes(a_million_letter_a(),
2418-
0u, 500000u));
2421+
assert half_a_million_letter_a() ==
2422+
unsafe::slice_bytes(a_million_letter_a(), 0u, 500000u);
24192423
}
24202424
}
24212425

@@ -2501,10 +2505,10 @@ mod tests {
25012505

25022506
#[test]
25032507
fn test_slice() {
2504-
assert (eq(~"ab", slice(~"abc", 0u, 2u)));
2505-
assert (eq(~"bc", slice(~"abc", 1u, 3u)));
2506-
assert (eq(~"", slice(~"abc", 1u, 1u)));
2507-
assert (eq(~"\u65e5", slice(~"\u65e5\u672c", 0u, 3u)));
2508+
assert ~"ab" == slice(~"abc", 0u, 2u);
2509+
assert ~"bc" == slice(~"abc", 1u, 3u);
2510+
assert ~"" == slice(~"abc", 1u, 1u);
2511+
assert ~"\u65e5" == slice(~"\u65e5\u672c", 0u, 3u);
25082512

25092513
let data = ~"ประเทศไทย中华";
25102514
assert ~"ป" == slice(data, 0u, 3u);
@@ -2524,8 +2528,8 @@ mod tests {
25242528
while i < 100000 { push_str(rs, ~"华华华华华"); i += 1; }
25252529
return rs;
25262530
}
2527-
assert eq(half_a_million_letter_X(),
2528-
slice(a_million_letter_X(), 0u, 3u * 500000u));
2531+
assert half_a_million_letter_X() ==
2532+
slice(a_million_letter_X(), 0u, 3u * 500000u);
25292533
}
25302534

25312535
#[test]
@@ -2709,7 +2713,7 @@ mod tests {
27092713
let s = ~"hello";
27102714
let sb = as_buf(s, |b, _l| b);
27112715
let s_cstr = unsafe::from_buf(sb);
2712-
assert (eq(s_cstr, s));
2716+
assert s_cstr == s;
27132717
}
27142718
}
27152719

src/libcore/sys.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export pref_align_of;
88
export refcount;
99
export log_str;
1010
export lock_and_signal, condition, methods;
11+
export shape_eq, shape_lt, shape_le;
1112

1213
import task::atomically;
1314

@@ -39,6 +40,20 @@ extern mod rusti {
3940
fn min_align_of<T>() -> uint;
4041
}
4142

43+
/// Compares contents of two pointers using the default method.
44+
/// Equivalent to `*x1 == *x2`. Useful for hashtables.
45+
pure fn shape_eq<T>(x1: &T, x2: &T) -> bool {
46+
*x1 == *x2
47+
}
48+
49+
pure fn shape_lt<T>(x1: &T, x2: &T) -> bool {
50+
*x1 < *x2
51+
}
52+
53+
pure fn shape_le<T>(x1: &T, x2: &T) -> bool {
54+
*x1 < *x2
55+
}
56+
4257
/**
4358
* Returns a pointer to a type descriptor.
4459
*

src/libcore/uint-template.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ const max_value: T = 0 as T - 1 as T;
1919
pure fn min(&&x: T, &&y: T) -> T { if x < y { x } else { y } }
2020
pure fn max(&&x: T, &&y: T) -> T { if x > y { x } else { y } }
2121

22-
pure fn add(&&x: T, &&y: T) -> T { x + y }
23-
pure fn sub(&&x: T, &&y: T) -> T { x - y }
24-
pure fn mul(&&x: T, &&y: T) -> T { x * y }
25-
pure fn div(&&x: T, &&y: T) -> T { x / y }
26-
pure fn rem(&&x: T, &&y: T) -> T { x % y }
27-
28-
pure fn lt(&&x: T, &&y: T) -> bool { x < y }
29-
pure fn le(&&x: T, &&y: T) -> bool { x <= y }
30-
pure fn eq(&&x: T, &&y: T) -> bool { x == y }
31-
pure fn ne(&&x: T, &&y: T) -> bool { x != y }
32-
pure fn ge(&&x: T, &&y: T) -> bool { x >= y }
33-
pure fn gt(&&x: T, &&y: T) -> bool { x > y }
22+
pure fn add(x: &T, y: &T) -> T { *x + *y }
23+
pure fn sub(x: &T, y: &T) -> T { *x - *y }
24+
pure fn mul(x: &T, y: &T) -> T { *x * *y }
25+
pure fn div(x: &T, y: &T) -> T { *x / *y }
26+
pure fn rem(x: &T, y: &T) -> T { *x % *y }
27+
28+
pure fn lt(x: &T, y: &T) -> bool { *x < *y }
29+
pure fn le(x: &T, y: &T) -> bool { *x <= *y }
30+
pure fn eq(x: &T, y: &T) -> bool { *x == *y }
31+
pure fn ne(x: &T, y: &T) -> bool { *x != *y }
32+
pure fn ge(x: &T, y: &T) -> bool { *x >= *y }
33+
pure fn gt(x: &T, y: &T) -> bool { *x > *y }
3434

3535
pure fn is_positive(x: T) -> bool { x > 0 as T }
3636
pure fn is_negative(x: T) -> bool { x < 0 as T }

0 commit comments

Comments
 (0)