Skip to content

Commit 5a282ec

Browse files
committed
core: Convert some records to structs
1 parent 4a2a375 commit 5a282ec

File tree

9 files changed

+85
-79
lines changed

9 files changed

+85
-79
lines changed

src/compiletest/runtest.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -339,22 +339,22 @@ fn scan_char(haystack: ~str, needle: char, idx: &mut uint) -> bool {
339339
if *idx >= haystack.len() {
340340
return false;
341341
}
342-
let {ch, next} = str::char_range_at(haystack, *idx);
343-
if ch != needle {
342+
let range = str::char_range_at(haystack, *idx);
343+
if range.ch != needle {
344344
return false;
345345
}
346-
*idx = next;
346+
*idx = range.next;
347347
return true;
348348
}
349349

350350
fn scan_integer(haystack: ~str, idx: &mut uint) -> bool {
351351
let mut i = *idx;
352352
while i < haystack.len() {
353-
let {ch, next} = str::char_range_at(haystack, i);
354-
if ch < '0' || '9' < ch {
353+
let range = str::char_range_at(haystack, i);
354+
if range.ch < '0' || '9' < range.ch {
355355
break;
356356
}
357-
i = next;
357+
i = range.next;
358358
}
359359
if i == *idx {
360360
return false;
@@ -370,9 +370,9 @@ fn scan_string(haystack: ~str, needle: ~str, idx: &mut uint) -> bool {
370370
if haystack_i >= haystack.len() {
371371
return false;
372372
}
373-
let {ch, next} = str::char_range_at(haystack, haystack_i);
374-
haystack_i = next;
375-
if !scan_char(needle, ch, &mut needle_i) {
373+
let range = str::char_range_at(haystack, haystack_i);
374+
haystack_i = range.next;
375+
if !scan_char(needle, range.ch, &mut needle_i) {
376376
return false;
377377
}
378378
}

src/libcore/dvec.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,28 @@ use ptr::null;
4747
* pointers achieved about 103 million pushes/second. Using an option
4848
* type could only produce 47 million pushes/second.
4949
*/
50-
type DVec_<A> = {
50+
pub struct DVec<A> {
5151
mut data: ~[A]
52-
};
53-
54-
pub enum DVec<A> {
55-
DVec_(DVec_<A>)
5652
}
5753

5854
/// Creates a new, empty dvec
5955
pub pure fn DVec<A>() -> DVec<A> {
60-
DVec_({mut data: ~[]})
56+
DVec {mut data: ~[]}
6157
}
6258

6359
/// Creates a new dvec with a single element
6460
pub fn from_elem<A>(e: A) -> DVec<A> {
65-
DVec_({mut data: ~[move e]})
61+
DVec {mut data: ~[move e]}
6662
}
6763

6864
/// Creates a new dvec with the contents of a vector
6965
pub fn from_vec<A>(v: ~[A]) -> DVec<A> {
70-
DVec_({mut data: move v})
66+
DVec {mut data: move v}
7167
}
7268

7369
/// Consumes the vector and returns its contents
7470
pub fn unwrap<A>(d: DVec<A>) -> ~[A] {
75-
let DVec_({data: v}) = move d;
71+
let DVec {data: v} = move d;
7672
move v
7773
}
7874

src/libcore/either.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn rights<T, U: Copy>(eithers: &[Either<T, U>]) -> ~[U] {
5757

5858
// XXX bad copies. take arg by val
5959
pub fn partition<T: Copy, U: Copy>(eithers: &[Either<T, U>])
60-
-> {lefts: ~[T], rights: ~[U]} {
60+
-> (~[T], ~[U]) {
6161
/*!
6262
* Extracts from a vector of either all the left values and right values
6363
*
@@ -73,7 +73,7 @@ pub fn partition<T: Copy, U: Copy>(eithers: &[Either<T, U>])
7373
Right(copy r) => rights.push(r)
7474
}
7575
}
76-
return {lefts: move lefts, rights: move rights};
76+
return (move lefts, move rights);
7777
}
7878

7979
// XXX bad copies
@@ -212,36 +212,36 @@ fn test_rights_empty() {
212212
#[test]
213213
fn test_partition() {
214214
let input = ~[Left(10), Right(11), Left(12), Right(13), Left(14)];
215-
let result = partition(input);
216-
assert (result.lefts[0] == 10);
217-
assert (result.lefts[1] == 12);
218-
assert (result.lefts[2] == 14);
219-
assert (result.rights[0] == 11);
220-
assert (result.rights[1] == 13);
215+
let (lefts, rights) = partition(input);
216+
assert (lefts[0] == 10);
217+
assert (lefts[1] == 12);
218+
assert (lefts[2] == 14);
219+
assert (rights[0] == 11);
220+
assert (rights[1] == 13);
221221
}
222222

223223
#[test]
224224
fn test_partition_no_lefts() {
225225
let input: ~[Either<int, int>] = ~[Right(10), Right(11)];
226-
let result = partition(input);
227-
assert (vec::len(result.lefts) == 0u);
228-
assert (vec::len(result.rights) == 2u);
226+
let (lefts, rights) = partition(input);
227+
assert (vec::len(lefts) == 0u);
228+
assert (vec::len(rights) == 2u);
229229
}
230230

231231
#[test]
232232
fn test_partition_no_rights() {
233233
let input: ~[Either<int, int>] = ~[Left(10), Left(11)];
234-
let result = partition(input);
235-
assert (vec::len(result.lefts) == 2u);
236-
assert (vec::len(result.rights) == 0u);
234+
let (lefts, rights) = partition(input);
235+
assert (vec::len(lefts) == 2u);
236+
assert (vec::len(rights) == 0u);
237237
}
238238

239239
#[test]
240240
fn test_partition_empty() {
241241
let input: ~[Either<int, int>] = ~[];
242-
let result = partition(input);
243-
assert (vec::len(result.lefts) == 0u);
244-
assert (vec::len(result.rights) == 0u);
242+
let (lefts, rights) = partition(input);
243+
assert (vec::len(lefts) == 0u);
244+
assert (vec::len(rights) == 0u);
245245
}
246246

247247
//

src/libcore/ptr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,16 @@ impl<T:Ord> &const T : Ord {
269269
#[test]
270270
pub fn test() {
271271
unsafe {
272-
type Pair = {mut fst: int, mut snd: int};
273-
let p = {mut fst: 10, mut snd: 20};
272+
struct Pair {mut fst: int, mut snd: int};
273+
let p = Pair {mut fst: 10, mut snd: 20};
274274
let pptr: *mut Pair = mut_addr_of(&p);
275275
let iptr: *mut int = cast::reinterpret_cast(&pptr);
276276
assert (*iptr == 10);;
277277
*iptr = 30;
278278
assert (*iptr == 30);
279279
assert (p.fst == 30);;
280280

281-
*pptr = {mut fst: 50, mut snd: 60};
281+
*pptr = Pair {mut fst: 50, mut snd: 60};
282282
assert (*iptr == 50);
283283
assert (p.fst == 50);
284284
assert (p.snd == 60);

src/libcore/str.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ Section: Adding to and removing from a string
224224
pub fn pop_char(s: &mut ~str) -> char {
225225
let end = len(*s);
226226
assert end > 0u;
227-
let {ch, prev} = char_range_at_reverse(*s, end);
228-
unsafe { raw::set_len(s, prev); }
227+
let CharRange {ch, next} = char_range_at_reverse(*s, end);
228+
unsafe { raw::set_len(s, next); }
229229
return ch;
230230
}
231231

@@ -237,7 +237,7 @@ pub fn pop_char(s: &mut ~str) -> char {
237237
* If the string does not contain any characters
238238
*/
239239
pub fn shift_char(s: &mut ~str) -> char {
240-
let {ch, next} = char_range_at(*s, 0u);
240+
let CharRange {ch, next} = char_range_at(*s, 0u);
241241
*s = unsafe { raw::slice_bytes(*s, next, len(*s)) };
242242
return ch;
243243
}
@@ -253,7 +253,7 @@ pub fn shift_char(s: &mut ~str) -> char {
253253
*/
254254
#[inline]
255255
pub fn view_shift_char(s: &a/str) -> (char, &a/str) {
256-
let {ch, next} = char_range_at(s, 0u);
256+
let CharRange {ch, next} = char_range_at(s, 0u);
257257
let next_s = unsafe { raw::view_bytes(s, next, len(s)) };
258258
return (ch, next_s);
259259
}
@@ -296,7 +296,7 @@ pub pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
296296
match rfind(s, |c| !chars_to_trim.contains(&c)) {
297297
None => ~"",
298298
Some(last) => {
299-
let {next, _} = char_range_at(s, last);
299+
let next = char_range_at(s, last).next;
300300
unsafe { raw::slice_bytes(s, 0u, next) }
301301
}
302302
}
@@ -328,7 +328,7 @@ pub pure fn trim_right(s: &str) -> ~str {
328328
match rfind(s, |c| !char::is_whitespace(c)) {
329329
None => ~"",
330330
Some(last) => {
331-
let {next, _} = char_range_at(s, last);
331+
let next = char_range_at(s, last).next;
332332
unsafe { raw::slice_bytes(s, 0u, next) }
333333
}
334334
}
@@ -365,7 +365,7 @@ pub pure fn chars(s: &str) -> ~[char] {
365365
let mut buf = ~[], i = 0;
366366
let len = len(s);
367367
while i < len {
368-
let {ch, next} = char_range_at(s, i);
368+
let CharRange {ch, next} = char_range_at(s, i);
369369
unsafe { buf.push(ch); }
370370
i = next;
371371
}
@@ -475,7 +475,7 @@ pure fn split_inner(s: &str, sepfn: fn(cc: char) -> bool, count: uint,
475475
let l = len(s);
476476
let mut result = ~[], i = 0u, start = 0u, done = 0u;
477477
while i < l && done < count {
478-
let {ch, next} = char_range_at(s, i);
478+
let CharRange {ch, next} = char_range_at(s, i);
479479
if sepfn(ch) {
480480
if allow_empty || start < i unsafe {
481481
result.push(unsafe { raw::slice_bytes(s, start, i)});
@@ -866,7 +866,7 @@ pub pure fn each_chari(s: &str, it: fn(uint, char) -> bool) {
866866
let mut pos = 0u, ch_pos = 0u;
867867
let len = len(s);
868868
while pos < len {
869-
let {ch, next} = char_range_at(s, pos);
869+
let CharRange {ch, next} = char_range_at(s, pos);
870870
pos = next;
871871
if !it(ch_pos, ch) { break; }
872872
ch_pos += 1u;
@@ -878,7 +878,7 @@ pub pure fn chars_each(s: &str, it: fn(char) -> bool) {
878878
let mut pos = 0u;
879879
let len = len(s);
880880
while (pos < len) {
881-
let {ch, next} = char_range_at(s, pos);
881+
let CharRange {ch, next} = char_range_at(s, pos);
882882
pos = next;
883883
if !it(ch) { return; }
884884
}
@@ -1144,7 +1144,7 @@ pub pure fn find_between(s: &str, start: uint, end: uint, f: fn(char) -> bool)
11441144
assert is_char_boundary(s, start);
11451145
let mut i = start;
11461146
while i < end {
1147-
let {ch, next} = char_range_at(s, i);
1147+
let CharRange {ch, next} = char_range_at(s, i);
11481148
if f(ch) { return Some(i); }
11491149
i = next;
11501150
}
@@ -1224,7 +1224,7 @@ pub pure fn rfind_between(s: &str, start: uint, end: uint,
12241224
assert is_char_boundary(s, start);
12251225
let mut i = start;
12261226
while i > end {
1227-
let {ch, prev} = char_range_at_reverse(s, i);
1227+
let CharRange {ch, next: prev} = char_range_at_reverse(s, i);
12281228
if f(ch) { return Some(prev); }
12291229
i = prev;
12301230
}
@@ -1538,7 +1538,7 @@ pub pure fn count_chars(s: &str, start: uint, end: uint) -> uint {
15381538
assert is_char_boundary(s, end);
15391539
let mut i = start, len = 0u;
15401540
while i < end {
1541-
let {next, _} = char_range_at(s, i);
1541+
let next = char_range_at(s, i).next;
15421542
len += 1u;
15431543
i = next;
15441544
}
@@ -1552,7 +1552,7 @@ pub pure fn count_bytes(s: &b/str, start: uint, n: uint) -> uint {
15521552
let l = len(s);
15531553
while cnt > 0u {
15541554
assert end < l;
1555-
let {next, _} = char_range_at(s, end);
1555+
let next = char_range_at(s, end).next;
15561556
cnt -= 1u;
15571557
end = next;
15581558
}
@@ -1595,7 +1595,7 @@ pub pure fn is_char_boundary(s: &str, index: uint) -> bool {
15951595
* let s = "中华Việt Nam";
15961596
* let i = 0u;
15971597
* while i < str::len(s) {
1598-
* let {ch, next} = str::char_range_at(s, i);
1598+
* let CharRange {ch, next} = str::char_range_at(s, i);
15991599
* std::io::println(fmt!("%u: %c",i,ch));
16001600
* i = next;
16011601
* }
@@ -1631,11 +1631,11 @@ pub pure fn is_char_boundary(s: &str, index: uint) -> bool {
16311631
* If `i` is greater than or equal to the length of the string.
16321632
* If `i` is not the index of the beginning of a valid UTF-8 character.
16331633
*/
1634-
pub pure fn char_range_at(s: &str, i: uint) -> {ch: char, next: uint} {
1634+
pub pure fn char_range_at(s: &str, i: uint) -> CharRange {
16351635
let b0 = s[i];
16361636
let w = utf8_char_width(b0);
16371637
assert (w != 0u);
1638-
if w == 1u { return {ch: b0 as char, next: i + 1u}; }
1638+
if w == 1u { return CharRange {ch: b0 as char, next: i + 1u}; }
16391639
let mut val = 0u;
16401640
let end = i + w;
16411641
let mut i = i + 1u;
@@ -1650,21 +1650,26 @@ pub pure fn char_range_at(s: &str, i: uint) -> {ch: char, next: uint} {
16501650
// the first to clip off the marker bits at the left of the byte, and then
16511651
// a second (as uint) to get it to the right position.
16521652
val += ((b0 << ((w + 1u) as u8)) as uint) << ((w - 1u) * 6u - w - 1u);
1653-
return {ch: val as char, next: i};
1653+
return CharRange {ch: val as char, next: i};
16541654
}
16551655

16561656
/// Pluck a character out of a string
16571657
pub pure fn char_at(s: &str, i: uint) -> char {
16581658
return char_range_at(s, i).ch;
16591659
}
16601660

1661+
pub struct CharRange {
1662+
ch: char,
1663+
next: uint
1664+
}
1665+
16611666
/**
16621667
* Given a byte position and a str, return the previous char and its position
16631668
*
16641669
* This function can be used to iterate over a unicode string in reverse.
16651670
*/
16661671
pure fn char_range_at_reverse(ss: &str, start: uint)
1667-
-> {ch: char, prev: uint} {
1672+
-> CharRange {
16681673

16691674
let mut prev = start;
16701675

@@ -1677,7 +1682,7 @@ pure fn char_range_at_reverse(ss: &str, start: uint)
16771682
prev -= 1u;
16781683

16791684
let ch = char_at(ss, prev);
1680-
return {ch:ch, prev:prev};
1685+
return CharRange {ch:ch, next:prev};
16811686
}
16821687

16831688
/**
@@ -1707,7 +1712,7 @@ pub pure fn all_between(s: &str, start: uint, end: uint,
17071712
assert is_char_boundary(s, start);
17081713
let mut i = start;
17091714
while i < end {
1710-
let {ch, next} = char_range_at(s, i);
1715+
let CharRange {ch, next} = char_range_at(s, i);
17111716
if !it(ch) { return false; }
17121717
i = next;
17131718
}

src/librustc/metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn warn_if_multiple_versions(e: env, diag: span_handler,
5959

6060
if crate_cache.len() != 0u {
6161
let name = loader::crate_name_from_metas(*crate_cache.last().metas);
62-
let {lefts: matches, rights: non_matches} =
62+
let (matches, non_matches) =
6363
partition(crate_cache.map_to_vec(|entry| {
6464
let othername = loader::crate_name_from_metas(*entry.metas);
6565
if name == othername {

src/libstd/rope.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,9 +1206,11 @@ mod node {
12061206
(*it).leaf = option::None;
12071207
return option::None
12081208
} else {
1209-
let {ch, next} =
1209+
let range =
12101210
str::char_range_at(*aleaf.content,
12111211
(*it).leaf_byte_pos + aleaf.byte_offset);
1212+
let ch = range.ch;
1213+
let next = range.next;
12121214
(*it).leaf_byte_pos = next - aleaf.byte_offset;
12131215
return option::Some(ch)
12141216
}
@@ -1283,9 +1285,9 @@ mod tests {
12831285
equal = false;
12841286
} break; }
12851287
option::Some(c) => {
1286-
let {ch, next} = str::char_range_at(*sample, string_iter);
1287-
string_iter = next;
1288-
if ch != c { equal = false; break; }
1288+
let range = str::char_range_at(*sample, string_iter);
1289+
string_iter = range.next;
1290+
if range.ch != c { equal = false; break; }
12891291
}
12901292
}
12911293
}

0 commit comments

Comments
 (0)