Skip to content

Commit b262685

Browse files
committed
---
yaml --- r: 30003 b: refs/heads/incoming c: 20773f3 h: refs/heads/master i: 30001: 8cbe821 29999: 5766600 v: v3
1 parent ec530cb commit b262685

File tree

6 files changed

+93
-35
lines changed

6 files changed

+93
-35
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
9-
refs/heads/incoming: d8988fe220865034f50b936dc5e219a1b6347314
9+
refs/heads/incoming: 20773f33f522ac7686e0ac7271332646e9944938
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/src/libcore/vec.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,9 @@ fn consume_mut<T>(+v: ~[mut T], f: fn(uint, +T)) unsafe {
510510
/// Remove the last element from a vector and return it
511511
fn pop<T>(&v: ~[const T]) -> T {
512512
let ln = len(v);
513-
assert ln > 0u;
513+
if ln == 0 {
514+
fail ~"sorry, cannot vec::pop an empty vector"
515+
}
514516
let valptr = ptr::mut_addr_of(v[ln - 1u]);
515517
unsafe {
516518
let val <- *valptr;
@@ -519,6 +521,29 @@ fn pop<T>(&v: ~[const T]) -> T {
519521
}
520522
}
521523

524+
/**
525+
* Remove an element from anywhere in the vector and return it, replacing it
526+
* with the last element. This does not preserve ordering, but is O(1).
527+
*
528+
* Fails if index >= length.
529+
*/
530+
fn swap_remove<T>(&v: ~[const T], index: uint) -> T {
531+
let ln = len(v);
532+
if index >= ln {
533+
fail #fmt("vec::swap_remove - index %u >= length %u", index, ln);
534+
}
535+
let lastptr = ptr::mut_addr_of(v[ln - 1]);
536+
unsafe {
537+
let mut val <- *lastptr;
538+
if index < ln - 1 {
539+
let valptr = ptr::mut_addr_of(v[index]);
540+
*valptr <-> val;
541+
}
542+
unsafe::set_len(v, ln - 1);
543+
val
544+
}
545+
}
546+
522547
/// Append an element to a vector
523548
#[inline(always)]
524549
fn push<T>(&v: ~[const T], +initval: T) {
@@ -1935,6 +1960,34 @@ mod tests {
19351960
assert (e == 5);
19361961
}
19371962

1963+
#[test]
1964+
fn test_swap_remove() {
1965+
let mut v = ~[1, 2, 3, 4, 5];
1966+
let mut e = swap_remove(v, 0);
1967+
assert (len(v) == 4);
1968+
assert e == 1;
1969+
assert (v[0] == 5);
1970+
e = swap_remove(v, 3);
1971+
assert (len(v) == 3);
1972+
assert e == 4;
1973+
assert (v[0] == 5);
1974+
assert (v[1] == 2);
1975+
assert (v[2] == 3);
1976+
}
1977+
1978+
#[test]
1979+
fn test_swap_remove_noncopyable() {
1980+
// Tests that we don't accidentally run destructors twice.
1981+
let mut v = ~[::unsafe::exclusive(()), ::unsafe::exclusive(()),
1982+
::unsafe::exclusive(())];
1983+
let mut _e = swap_remove(v, 0);
1984+
assert (len(v) == 2);
1985+
_e = swap_remove(v, 1);
1986+
assert (len(v) == 1);
1987+
_e = swap_remove(v, 0);
1988+
assert (len(v) == 0);
1989+
}
1990+
19381991
#[test]
19391992
fn test_push() {
19401993
// Test on-stack push().

branches/incoming/src/libstd/ebml.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,14 @@ impl writer {
284284
self.wr_tagged_bytes(tag_id, &[v as u8]);
285285
}
286286

287-
fn wr_tagged_str(tag_id: uint, v: &str) {
288-
str::byte_slice(v, |b| self.wr_tagged_bytes(tag_id, b));
287+
fn wr_tagged_str(tag_id: uint, v: ~str) {
288+
// Lame: can't use str::as_bytes() here because the resulting
289+
// vector is NULL-terminated. Annoyingly, the underlying
290+
// writer interface doesn't permit us to write a slice of a
291+
// vector. We need first-class slices, I think.
292+
293+
// str::as_bytes(v) {|b| self.wr_tagged_bytes(tag_id, b); }
294+
self.wr_tagged_bytes(tag_id, str::bytes(v));
289295
}
290296

291297
fn wr_bytes(b: &[u8]) {
@@ -363,7 +369,7 @@ impl ebml::writer: serialization::serializer {
363369
fn emit_f32(_v: f32) { fail ~"Unimplemented: serializing an f32"; }
364370
fn emit_float(_v: float) { fail ~"Unimplemented: serializing a float"; }
365371

366-
fn emit_str(v: &str) { self.wr_tagged_str(es_str as uint, v) }
372+
fn emit_str(v: ~str) { self.wr_tagged_str(es_str as uint, v) }
367373

368374
fn emit_enum(name: ~str, f: fn()) {
369375
self._emit_label(name);
@@ -445,7 +451,7 @@ priv impl ebml_deserializer {
445451
return r_doc;
446452
}
447453

448-
fn push_doc<T>(d: ebml::doc, f: fn() -> T) -> T{
454+
fn push_doc<T: copy>(d: ebml::doc, f: fn() -> T) -> T{
449455
let old_parent = self.parent;
450456
let old_pos = self.pos;
451457
self.parent = d;
@@ -499,13 +505,13 @@ impl ebml_deserializer: serialization::deserializer {
499505
fn read_str() -> ~str { ebml::doc_as_str(self.next_doc(es_str)) }
500506

501507
// Compound types:
502-
fn read_enum<T>(name: ~str, f: fn() -> T) -> T {
508+
fn read_enum<T:copy>(name: ~str, f: fn() -> T) -> T {
503509
debug!{"read_enum(%s)", name};
504510
self._check_label(name);
505511
self.push_doc(self.next_doc(es_enum), f)
506512
}
507513

508-
fn read_enum_variant<T>(f: fn(uint) -> T) -> T {
514+
fn read_enum_variant<T:copy>(f: fn(uint) -> T) -> T {
509515
debug!{"read_enum_variant()"};
510516
let idx = self._next_uint(es_enum_vid);
511517
debug!{" idx=%u", idx};
@@ -514,12 +520,12 @@ impl ebml_deserializer: serialization::deserializer {
514520
}
515521
}
516522

517-
fn read_enum_variant_arg<T>(idx: uint, f: fn() -> T) -> T {
523+
fn read_enum_variant_arg<T:copy>(idx: uint, f: fn() -> T) -> T {
518524
debug!{"read_enum_variant_arg(idx=%u)", idx};
519525
f()
520526
}
521527

522-
fn read_vec<T>(f: fn(uint) -> T) -> T {
528+
fn read_vec<T:copy>(f: fn(uint) -> T) -> T {
523529
debug!{"read_vec()"};
524530
do self.push_doc(self.next_doc(es_vec)) {
525531
let len = self._next_uint(es_vec_len);
@@ -528,38 +534,38 @@ impl ebml_deserializer: serialization::deserializer {
528534
}
529535
}
530536

531-
fn read_vec_elt<T>(idx: uint, f: fn() -> T) -> T {
537+
fn read_vec_elt<T:copy>(idx: uint, f: fn() -> T) -> T {
532538
debug!{"read_vec_elt(idx=%u)", idx};
533539
self.push_doc(self.next_doc(es_vec_elt), f)
534540
}
535541

536-
fn read_box<T>(f: fn() -> T) -> T {
542+
fn read_box<T:copy>(f: fn() -> T) -> T {
537543
debug!{"read_box()"};
538544
f()
539545
}
540546

541-
fn read_uniq<T>(f: fn() -> T) -> T {
547+
fn read_uniq<T:copy>(f: fn() -> T) -> T {
542548
debug!{"read_uniq()"};
543549
f()
544550
}
545551

546-
fn read_rec<T>(f: fn() -> T) -> T {
552+
fn read_rec<T:copy>(f: fn() -> T) -> T {
547553
debug!{"read_rec()"};
548554
f()
549555
}
550556

551-
fn read_rec_field<T>(f_name: ~str, f_idx: uint, f: fn() -> T) -> T {
557+
fn read_rec_field<T:copy>(f_name: ~str, f_idx: uint, f: fn() -> T) -> T {
552558
debug!{"read_rec_field(%s, idx=%u)", f_name, f_idx};
553559
self._check_label(f_name);
554560
f()
555561
}
556562

557-
fn read_tup<T>(sz: uint, f: fn() -> T) -> T {
563+
fn read_tup<T:copy>(sz: uint, f: fn() -> T) -> T {
558564
debug!{"read_tup(sz=%u)", sz};
559565
f()
560566
}
561567

562-
fn read_tup_elt<T>(idx: uint, f: fn() -> T) -> T {
568+
fn read_tup_elt<T:copy>(idx: uint, f: fn() -> T) -> T {
563569
debug!{"read_tup_elt(idx=%u)", idx};
564570
f()
565571
}

branches/incoming/src/libstd/prettyprint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Writer: serializer {
6363
self.write_str(fmt!{"%?_f32", v});
6464
}
6565

66-
fn emit_str(v: &str) {
66+
fn emit_str(v: ~str) {
6767
self.write_str(fmt!{"%?", v});
6868
}
6969

@@ -127,4 +127,4 @@ impl Writer: serializer {
127127
if idx > 0u { self.write_str(~", "); }
128128
f();
129129
}
130-
}
130+
}

branches/incoming/src/libstd/serialization.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ trait serializer {
2323
fn emit_float(v: float);
2424
fn emit_f64(v: f64);
2525
fn emit_f32(v: f32);
26-
fn emit_str(v: &str);
26+
fn emit_str(v: ~str);
2727

2828
// Compound types:
2929
fn emit_enum(name: ~str, f: fn());
@@ -65,17 +65,17 @@ trait deserializer {
6565
fn read_float() -> float;
6666

6767
// Compound types:
68-
fn read_enum<T>(name: ~str, f: fn() -> T) -> T;
69-
fn read_enum_variant<T>(f: fn(uint) -> T) -> T;
70-
fn read_enum_variant_arg<T>(idx: uint, f: fn() -> T) -> T;
71-
fn read_vec<T>(f: fn(uint) -> T) -> T;
72-
fn read_vec_elt<T>(idx: uint, f: fn() -> T) -> T;
73-
fn read_box<T>(f: fn() -> T) -> T;
74-
fn read_uniq<T>(f: fn() -> T) -> T;
75-
fn read_rec<T>(f: fn() -> T) -> T;
76-
fn read_rec_field<T>(f_name: ~str, f_idx: uint, f: fn() -> T) -> T;
77-
fn read_tup<T>(sz: uint, f: fn() -> T) -> T;
78-
fn read_tup_elt<T>(idx: uint, f: fn() -> T) -> T;
68+
fn read_enum<T:copy>(name: ~str, f: fn() -> T) -> T;
69+
fn read_enum_variant<T:copy>(f: fn(uint) -> T) -> T;
70+
fn read_enum_variant_arg<T:copy>(idx: uint, f: fn() -> T) -> T;
71+
fn read_vec<T:copy>(f: fn(uint) -> T) -> T;
72+
fn read_vec_elt<T:copy>(idx: uint, f: fn() -> T) -> T;
73+
fn read_box<T:copy>(f: fn() -> T) -> T;
74+
fn read_uniq<T:copy>(f: fn() -> T) -> T;
75+
fn read_rec<T:copy>(f: fn() -> T) -> T;
76+
fn read_rec_field<T:copy>(f_name: ~str, f_idx: uint, f: fn() -> T) -> T;
77+
fn read_tup<T:copy>(sz: uint, f: fn() -> T) -> T;
78+
fn read_tup_elt<T:copy>(idx: uint, f: fn() -> T) -> T;
7979
}
8080

8181
// ___________________________________________________________________________
@@ -201,7 +201,7 @@ fn deserialize_i64<D: deserializer>(d: D) -> i64 {
201201
d.read_i64()
202202
}
203203

204-
fn serialize_str<S: serializer>(s: S, v: &str) {
204+
fn serialize_str<S: serializer>(s: S, v: ~str) {
205205
s.emit_str(v);
206206
}
207207

branches/incoming/src/rustc/driver/driver.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,8 @@ fn parse_pretty(sess: session, &&name: ~str) -> pp_mode {
583583
~"expanded,identified" => ppm_expanded_identified,
584584
~"identified" => ppm_identified,
585585
_ => {
586-
sess.fatal(~"argument to `pretty` must be one of `normal`, \
587-
`expanded`, `typed`, `identified`, \
588-
or `expanded,identified`");
586+
sess.fatal(~"argument to `pretty` must be one of `normal`, `typed`, \
587+
or `identified`");
589588
}
590589
}
591590
}

0 commit comments

Comments
 (0)