diff --git a/src/libcore/dvec.rs b/src/libcore/dvec.rs
index 1540eb30fe5a4..1b6a7522864ef 100644
--- a/src/libcore/dvec.rs
+++ b/src/libcore/dvec.rs
@@ -56,7 +56,7 @@ pub enum DVec {
}
/// Creates a new, empty dvec
-pub fn DVec() -> DVec {
+pub pure fn DVec() -> DVec {
DVec_({mut data: ~[]})
}
diff --git a/src/libcore/extfmt.rs b/src/libcore/extfmt.rs
index 5acb45fdf1a55..d41393c23934c 100644
--- a/src/libcore/extfmt.rs
+++ b/src/libcore/extfmt.rs
@@ -329,11 +329,11 @@ pub mod rt {
// For strings, precision is the maximum characters
// displayed
let mut unpadded = match cv.precision {
- CountImplied => s.to_unique(),
+ CountImplied => s.to_owned(),
CountIs(max) => if max as uint < str::char_len(s) {
str::substr(s, 0u, max as uint)
} else {
- s.to_unique()
+ s.to_owned()
}
};
return unsafe { pad(cv, move unpadded, PadNozero) };
diff --git a/src/libcore/io.rs b/src/libcore/io.rs
index fd0fcbbe1c1f8..77f7b5023dfa9 100644
--- a/src/libcore/io.rs
+++ b/src/libcore/io.rs
@@ -37,7 +37,7 @@ pub trait Reader {
// FIXME (#2004): Seekable really should be orthogonal.
// FIXME (#2982): This should probably return an error.
- fn read(buf: &[mut u8], len: uint) -> uint;
+ fn read(bytes: &[mut u8], len: uint) -> uint;
fn read_byte() -> int;
fn unread_byte(int);
fn eof() -> bool;
@@ -65,32 +65,32 @@ pub trait ReaderUtil {
impl T : ReaderUtil {
fn read_bytes(len: uint) -> ~[u8] {
- let mut buf = vec::with_capacity(len);
- unsafe { vec::raw::set_len(&mut buf, len); }
+ let mut bytes = vec::with_capacity(len);
+ unsafe { vec::raw::set_len(&mut bytes, len); }
- let count = self.read(buf, len);
+ let count = self.read(bytes, len);
- unsafe { vec::raw::set_len(&mut buf, count); }
- move buf
+ unsafe { vec::raw::set_len(&mut bytes, count); }
+ move bytes
}
fn read_line() -> ~str {
- let mut buf = ~[];
+ let mut bytes = ~[];
loop {
let ch = self.read_byte();
if ch == -1 || ch == 10 { break; }
- buf.push(ch as u8);
+ bytes.push(ch as u8);
}
- str::from_bytes(buf)
+ str::from_bytes(bytes)
}
fn read_chars(n: uint) -> ~[char] {
// returns the (consumed offset, n_req), appends characters to &chars
- fn chars_from_bytes(buf: &~[u8], chars: &mut ~[char])
+ fn chars_from_bytes(bytes: &~[u8], chars: &mut ~[char])
-> (uint, uint) {
let mut i = 0;
- let buf_len = buf.len();
- while i < buf_len {
- let b0 = buf[i];
+ let bytes_len = bytes.len();
+ while i < bytes_len {
+ let b0 = bytes[i];
let w = str::utf8_char_width(b0);
let end = i + w;
i += 1;
@@ -100,12 +100,12 @@ impl T : ReaderUtil {
loop;
}
// can't satisfy this char with the existing data
- if end > buf_len {
- return (i - 1, end - buf_len);
+ if end > bytes_len {
+ return (i - 1, end - bytes_len);
}
let mut val = 0;
while i < end {
- let next = buf[i] as int;
+ let next = bytes[i] as int;
i += 1;
assert (next > -1);
assert (next & 192 == 128);
@@ -119,8 +119,8 @@ impl T : ReaderUtil {
}
return (i, 0);
}
- let mut buf: ~[u8] = ~[];
- let mut chars: ~[char] = ~[];
+ let mut bytes = ~[];
+ let mut chars = ~[];
// might need more bytes, but reading n will never over-read
let mut nbread = n;
while nbread > 0 {
@@ -130,15 +130,15 @@ impl T : ReaderUtil {
// we're split in a unicode char?
break;
}
- buf.push_all(data);
- let (offset, nbreq) = chars_from_bytes::(&buf, &mut chars);
+ bytes.push_all(data);
+ let (offset, nbreq) = chars_from_bytes::(&bytes, &mut chars);
let ncreq = n - chars.len();
// again we either know we need a certain number of bytes
// to complete a character, or we make sure we don't
// over-read by reading 1-byte per char needed
nbread = if ncreq > nbreq { ncreq } else { nbreq };
if nbread > 0 {
- buf = vec::slice(buf, offset, buf.len());
+ bytes = vec::slice(bytes, offset, bytes.len());
}
}
move chars
@@ -154,12 +154,12 @@ impl T : ReaderUtil {
}
fn read_c_str() -> ~str {
- let mut buf: ~[u8] = ~[];
+ let mut bytes: ~[u8] = ~[];
loop {
let ch = self.read_byte();
- if ch < 1 { break; } else { buf.push(ch as u8); }
+ if ch < 1 { break; } else { bytes.push(ch as u8); }
}
- str::from_bytes(buf)
+ str::from_bytes(bytes)
}
// FIXME deal with eof? // #2004
@@ -191,9 +191,9 @@ impl T : ReaderUtil {
}
fn read_whole_stream() -> ~[u8] {
- let mut buf: ~[u8] = ~[];
- while !self.eof() { buf.push_all(self.read_bytes(2048u)); }
- move buf
+ let mut bytes: ~[u8] = ~[];
+ while !self.eof() { bytes.push_all(self.read_bytes(2048u)); }
+ move bytes
}
fn each_byte(it: fn(int) -> bool) {
@@ -226,8 +226,8 @@ fn convert_whence(whence: SeekStyle) -> i32 {
}
impl *libc::FILE: Reader {
- fn read(buf: &[mut u8], len: uint) -> uint {
- do vec::as_mut_buf(buf) |buf_p, buf_len| {
+ fn read(bytes: &[mut u8], len: uint) -> uint {
+ do vec::as_mut_buf(bytes) |buf_p, buf_len| {
assert buf_len <= len;
let count = libc::fread(buf_p as *mut c_void, 1u as size_t,
@@ -250,7 +250,9 @@ impl *libc::FILE: Reader {
// duration of its lifetime.
// FIXME there really should be a better way to do this // #2004
impl {base: T, cleanup: C}: Reader {
- fn read(buf: &[mut u8], len: uint) -> uint { self.base.read(buf, len) }
+ fn read(bytes: &[mut u8], len: uint) -> uint {
+ self.base.read(bytes, len)
+ }
fn read_byte() -> int { self.base.read_byte() }
fn unread_byte(byte: int) { self.base.unread_byte(byte); }
fn eof() -> bool { self.base.eof() }
@@ -297,39 +299,41 @@ pub fn file_reader(path: &Path) -> Result {
}
-// Byte buffer readers
-
-pub type ByteBuf = {buf: &[const u8], mut pos: uint};
+// Byte readers
+pub struct BytesReader {
+ bytes: &[u8],
+ mut pos: uint
+}
-impl ByteBuf: Reader {
- fn read(buf: &[mut u8], len: uint) -> uint {
- let count = uint::min(len, self.buf.len() - self.pos);
+impl BytesReader: Reader {
+ fn read(bytes: &[mut u8], len: uint) -> uint {
+ let count = uint::min(len, self.bytes.len() - self.pos);
- let view = vec::const_view(self.buf, self.pos, self.buf.len());
- vec::bytes::memcpy(buf, view, count);
+ let view = vec::view(self.bytes, self.pos, self.bytes.len());
+ vec::bytes::memcpy(bytes, view, count);
self.pos += count;
count
}
fn read_byte() -> int {
- if self.pos == self.buf.len() { return -1; }
- let b = self.buf[self.pos];
+ if self.pos == self.bytes.len() { return -1; }
+ let b = self.bytes[self.pos];
self.pos += 1u;
return b as int;
}
// FIXME (#2738): implement this
fn unread_byte(_byte: int) { error!("Unimplemented: unread_byte"); fail; }
- fn eof() -> bool { self.pos == self.buf.len() }
+ fn eof() -> bool { self.pos == self.bytes.len() }
fn seek(offset: int, whence: SeekStyle) {
let pos = self.pos;
- self.pos = seek_in_buf(offset, pos, self.buf.len(), whence);
+ self.pos = seek_in_buf(offset, pos, self.bytes.len(), whence);
}
fn tell() -> uint { self.pos }
}
-pub fn with_bytes_reader(bytes: &[u8], f: fn(Reader) -> t) -> t {
- f({buf: bytes, mut pos: 0u} as Reader)
+pub pure fn with_bytes_reader(bytes: &[u8], f: fn(Reader) -> t) -> t {
+ f(BytesReader { bytes: bytes, pos: 0u } as Reader)
}
pub fn with_str_reader(s: &str, f: fn(Reader) -> T) -> T {
@@ -602,10 +606,10 @@ impl T : WriterUtil {
self.write_str(&"\n");
}
fn write_int(n: int) {
- int::to_str_bytes(n, 10u, |buf| self.write(buf))
+ int::to_str_bytes(n, 10u, |bytes| self.write(bytes))
}
fn write_uint(n: uint) {
- uint::to_str_bytes(false, n, 10u, |buf| self.write(buf))
+ uint::to_str_bytes(false, n, 10u, |bytes| self.write(bytes))
}
fn write_le_uint(n: uint) {
u64_to_le_bytes(n as u64, uint::bytes, |v| self.write(v))
@@ -687,34 +691,34 @@ pub fn print(s: &str) { stdout().write_str(s); }
pub fn println(s: &str) { stdout().write_line(s); }
pub struct BytesWriter {
- buf: DVec,
+ bytes: DVec,
mut pos: uint,
}
impl BytesWriter: Writer {
fn write(v: &[const u8]) {
- do self.buf.swap |buf| {
- let mut buf <- buf;
+ do self.bytes.swap |bytes| {
+ let mut bytes <- bytes;
let v_len = v.len();
- let buf_len = buf.len();
+ let bytes_len = bytes.len();
- let count = uint::max(buf_len, self.pos + v_len);
- vec::reserve(&mut buf, count);
- unsafe { vec::raw::set_len(&mut buf, count); }
+ let count = uint::max(bytes_len, self.pos + v_len);
+ vec::reserve(&mut bytes, count);
+ unsafe { vec::raw::set_len(&mut bytes, count); }
{
- let view = vec::mut_view(buf, self.pos, count);
+ let view = vec::mut_view(bytes, self.pos, count);
vec::bytes::memcpy(view, v, v_len);
}
self.pos += v_len;
- move buf
+ move bytes
}
}
fn seek(offset: int, whence: SeekStyle) {
let pos = self.pos;
- let len = self.buf.len();
+ let len = self.bytes.len();
self.pos = seek_in_buf(offset, pos, len, whence);
}
fn tell() -> uint { self.pos }
@@ -730,21 +734,25 @@ impl @BytesWriter : Writer {
fn get_type() -> WriterType { (*self).get_type() }
}
-pub fn BytesWriter() -> BytesWriter {
- BytesWriter { buf: DVec(), mut pos: 0u }
+pub pure fn BytesWriter() -> BytesWriter {
+ BytesWriter { bytes: DVec(), mut pos: 0u }
}
-pub fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
+pub pure fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
let wr = @BytesWriter();
f(wr as Writer);
- wr.buf.check_out(|buf| move buf)
+ // FIXME (#3758): This should not be needed.
+ unsafe { wr.bytes.check_out(|bytes| move bytes) }
}
-pub fn with_str_writer(f: fn(Writer)) -> ~str {
+pub pure fn with_str_writer(f: fn(Writer)) -> ~str {
let mut v = with_bytes_writer(f);
- // Make sure the vector has a trailing null and is proper utf8.
- v.push(0);
+ // FIXME (#3758): This should not be needed.
+ unsafe {
+ // Make sure the vector has a trailing null and is proper utf8.
+ v.push(0);
+ }
assert str::is_utf8(v);
unsafe { move ::cast::transmute(move v) }
@@ -975,15 +983,17 @@ mod tests {
fn bytes_buffer_overwrite() {
let wr = BytesWriter();
wr.write(~[0u8, 1u8, 2u8, 3u8]);
- assert wr.buf.borrow(|buf| buf == ~[0u8, 1u8, 2u8, 3u8]);
+ assert wr.bytes.borrow(|bytes| bytes == ~[0u8, 1u8, 2u8, 3u8]);
wr.seek(-2, SeekCur);
wr.write(~[4u8, 5u8, 6u8, 7u8]);
- assert wr.buf.borrow(|buf| buf == ~[0u8, 1u8, 4u8, 5u8, 6u8, 7u8]);
+ assert wr.bytes.borrow(|bytes| bytes ==
+ ~[0u8, 1u8, 4u8, 5u8, 6u8, 7u8]);
wr.seek(-2, SeekEnd);
wr.write(~[8u8]);
wr.seek(1, SeekSet);
wr.write(~[9u8]);
- assert wr.buf.borrow(|buf| buf == ~[0u8, 9u8, 4u8, 5u8, 8u8, 7u8]);
+ assert wr.bytes.borrow(|bytes| bytes ==
+ ~[0u8, 9u8, 4u8, 5u8, 8u8, 7u8]);
}
}
diff --git a/src/libcore/logging.rs b/src/libcore/logging.rs
index d4f3c0ea272ed..958d1ac56ea78 100644
--- a/src/libcore/logging.rs
+++ b/src/libcore/logging.rs
@@ -32,7 +32,7 @@ pub fn console_off() {
#[cfg(notest)]
#[lang="log_type"]
pub fn log_type(level: u32, object: &T) {
- let bytes = do io::with_bytes_writer() |writer| {
+ let bytes = do io::with_bytes_writer |writer| {
repr::write_repr(writer, object);
};
unsafe {
diff --git a/src/libcore/option.rs b/src/libcore/option.rs
index f8bafe29fdde2..50489a820291c 100644
--- a/src/libcore/option.rs
+++ b/src/libcore/option.rs
@@ -225,7 +225,7 @@ pub fn swap_unwrap(opt: &mut Option) -> T {
pub pure fn unwrap_expect(opt: Option, reason: &str) -> T {
//! As unwrap, but with a specified failure message.
- if opt.is_none() { fail reason.to_unique(); }
+ if opt.is_none() { fail reason.to_owned(); }
unwrap(move opt)
}
diff --git a/src/libcore/repr.rs b/src/libcore/repr.rs
index ff29953f09a49..b246adcb1d7cd 100644
--- a/src/libcore/repr.rs
+++ b/src/libcore/repr.rs
@@ -559,7 +559,7 @@ impl ReprPrinter {
unsafe {
self.align(sys::min_align_of::());
let value_addr: &T = transmute(copy self.ptr);
- (*value_addr).write_repr(self.writer);
+ value_addr.write_repr(self.writer);
self.bump(sys::size_of::());
true
}
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index d59f36e068137..3122f3d7f32e2 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -2135,7 +2135,7 @@ pub trait StrSlice {
pure fn trim() -> ~str;
pure fn trim_left() -> ~str;
pure fn trim_right() -> ~str;
- pure fn to_unique() -> ~str;
+ pure fn to_owned() -> ~str;
pure fn to_managed() -> @str;
pure fn char_at(i: uint) -> char;
}
@@ -2258,13 +2258,12 @@ impl &str: StrSlice {
pure fn trim_right() -> ~str { trim_right(self) }
#[inline]
- pure fn to_unique() -> ~str { self.slice(0, self.len()) }
+ pure fn to_owned() -> ~str { self.slice(0, self.len()) }
#[inline]
pure fn to_managed() -> @str {
- let v = at_vec::from_fn(self.len() + 1, |i| {
- if i == self.len() { 0 } else { self[i] }
- });
+ let bytes = as_bytes_slice(self);
+ let v = at_vec::from_fn(bytes.len(), |i| bytes[i]);
unsafe { ::cast::transmute(v) }
}
diff --git a/src/libstd/json.rs b/src/libstd/json.rs
index d170255b565d8..5f64389e58329 100644
--- a/src/libstd/json.rs
+++ b/src/libstd/json.rs
@@ -897,7 +897,7 @@ pub impl Deserializer: serialization::Deserializer {
// FIXME(#3148) This hint should not be necessary.
let obj: &self/~Object = obj;
- match obj.find_ref(&name.to_unique()) {
+ match obj.find_ref(&name.to_owned()) {
None => fail fmt!("no such field: %s", name),
Some(json) => {
self.stack.push(json);
diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs
index d08edd7af1d07..4bd1679600ffb 100644
--- a/src/libsyntax/attr.rs
+++ b/src/libsyntax/attr.rs
@@ -90,9 +90,7 @@ fn attr_meta(attr: ast::attribute) -> @ast::meta_item { @attr.node.value }
// Get the meta_items from inside a vector of attributes
fn attr_metas(attrs: ~[ast::attribute]) -> ~[@ast::meta_item] {
- let mut mitems = ~[];
- for attrs.each |a| { mitems.push(attr_meta(*a)); }
- return mitems;
+ do attrs.map |a| { attr_meta(*a) }
}
fn desugar_doc_attr(attr: &ast::attribute) -> ast::attribute {
diff --git a/src/rustc/back/upcall.rs b/src/rustc/back/upcall.rs
index a2c864f6f465f..45e7cd4e9d45a 100644
--- a/src/rustc/back/upcall.rs
+++ b/src/rustc/back/upcall.rs
@@ -27,8 +27,7 @@ fn declare_upcalls(targ_cfg: @session::config,
fn decl(llmod: ModuleRef, prefix: ~str, name: ~str,
tys: ~[TypeRef], rv: TypeRef) ->
ValueRef {
- let mut arg_tys: ~[TypeRef] = ~[];
- for tys.each |t| { arg_tys.push(*t); }
+ let arg_tys = tys.map(|t| *t);
let fn_ty = T_fn(arg_tys, rv);
return base::decl_cdecl_fn(llmod, prefix + name, fn_ty);
}
diff --git a/src/rustc/metadata/decoder.rs b/src/rustc/metadata/decoder.rs
index f0911bd1aa27a..1656efbd96684 100644
--- a/src/rustc/metadata/decoder.rs
+++ b/src/rustc/metadata/decoder.rs
@@ -598,13 +598,12 @@ fn get_enum_variants(intr: @ident_interner, cdata: cmd, id: ast::node_id,
let ctor_ty = item_type({crate: cdata.cnum, node: id}, item,
tcx, cdata);
let name = item_name(intr, item);
- let mut arg_tys: ~[ty::t] = ~[];
- match ty::get(ctor_ty).sty {
- ty::ty_fn(f) => {
- for f.sig.inputs.each |a| { arg_tys.push(a.ty); }
- }
- _ => { /* Nullary enum variant. */ }
- }
+ let arg_tys = match ty::get(ctor_ty).sty {
+ ty::ty_fn(f) => f.sig.inputs.map(|a| a.ty),
+
+ // Nullary enum variant.
+ _ => ~[],
+ };
match variant_disr_val(item) {
Some(val) => { disr_val = val; }
_ => { /* empty */ }
diff --git a/src/rustc/metadata/encoder.rs b/src/rustc/metadata/encoder.rs
index 0b25f0670b427..1579705dd0ecb 100644
--- a/src/rustc/metadata/encoder.rs
+++ b/src/rustc/metadata/encoder.rs
@@ -1162,7 +1162,7 @@ fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] {
if (parms.tcx.sess.meta_stats()) {
- do wr.buf.borrow |v| {
+ do wr.bytes.borrow |v| {
do v.each |e| {
if *e == 0 {
ecx.stats.zero_bytes += 1;
@@ -1195,7 +1195,7 @@ fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] {
(do str::as_bytes(&~"rust\x00\x00\x00\x01") |bytes| {
vec::slice(*bytes, 0, 8)
- }) + flate::deflate_bytes(wr.buf.check_out(|buf| buf))
+ }) + flate::deflate_bytes(wr.bytes.check_out(|buf| buf))
}
// Get the encoded string for a type
diff --git a/src/rustc/middle/trans/build.rs b/src/rustc/middle/trans/build.rs
index dfcc66adc3ac6..ea992600ae19b 100644
--- a/src/rustc/middle/trans/build.rs
+++ b/src/rustc/middle/trans/build.rs
@@ -433,8 +433,7 @@ fn GEP(cx: block, Pointer: ValueRef, Indices: ~[ValueRef]) -> ValueRef {
//
// XXX: Use a small-vector optimization to avoid allocations here.
fn GEPi(cx: block, base: ValueRef, ixs: &[uint]) -> ValueRef {
- let mut v: ~[ValueRef] = ~[];
- for vec::each(ixs) |i| { v.push(C_i32(*i as i32)); }
+ let v = do vec::map(ixs) |i| { C_i32(*i as i32) };
count_insn(cx, "gepi");
return InBoundsGEP(cx, base, v);
}
diff --git a/src/rustc/util/common.rs b/src/rustc/util/common.rs
index 0c6ec267da812..123905adba11b 100644
--- a/src/rustc/util/common.rs
+++ b/src/rustc/util/common.rs
@@ -34,9 +34,7 @@ type flag = HashMap<~str, ()>;
fn field_expr(f: ast::field) -> @ast::expr { return f.node.expr; }
fn field_exprs(fields: ~[ast::field]) -> ~[@ast::expr] {
- let mut es = ~[];
- for fields.each |f| { es.push(f.node.expr); }
- return es;
+ fields.map(|f| f.node.expr)
}
// Takes a predicate p, returns true iff p is true for any subexpressions
diff --git a/src/rustc/util/ppaux.rs b/src/rustc/util/ppaux.rs
index 27ace283fa0ce..8207082cf20e6 100644
--- a/src/rustc/util/ppaux.rs
+++ b/src/rustc/util/ppaux.rs
@@ -282,8 +282,7 @@ fn ty_to_str(cx: ctxt, typ: t) -> ~str {
_ => { }
}
s += ~"(";
- let mut strs = ~[];
- for inputs.each |a| { strs.push(fn_input_to_str(cx, *a)); }
+ let strs = inputs.map(|a| fn_input_to_str(cx, *a));
s += str::connect(strs, ~", ");
s += ~")";
if ty::get(output).sty != ty_nil {
@@ -338,13 +337,11 @@ fn ty_to_str(cx: ctxt, typ: t) -> ~str {
ty_unboxed_vec(tm) => { ~"unboxed_vec<" + mt_to_str(cx, tm) + ~">" }
ty_type => ~"type",
ty_rec(elems) => {
- let mut strs: ~[~str] = ~[];
- for elems.each |fld| { strs.push(field_to_str(cx, *fld)); }
+ let strs = elems.map(|fld| field_to_str(cx, *fld));
~"{" + str::connect(strs, ~",") + ~"}"
}
ty_tup(elems) => {
- let mut strs = ~[];
- for elems.each |elem| { strs.push(ty_to_str(cx, *elem)); }
+ let strs = elems.map(|elem| ty_to_str(cx, *elem));
~"(" + str::connect(strs, ~",") + ~")"
}
ty_fn(ref f) => {