Merge pull request #3805 from erickt/incoming

Variety of small cleanups
This commit is contained in:
Tim Chevalier 2012-10-18 11:12:32 -07:00
commit 141ef23aa7
15 changed files with 102 additions and 103 deletions

View File

@ -56,7 +56,7 @@ pub enum DVec<A> {
}
/// Creates a new, empty dvec
pub fn DVec<A>() -> DVec<A> {
pub pure fn DVec<A>() -> DVec<A> {
DVec_({mut data: ~[]})
}

View File

@ -329,11 +329,11 @@ pub enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }
// 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) };

View File

@ -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: Reader> 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<T: Reader>(buf: &~[u8], chars: &mut ~[char])
fn chars_from_bytes<T: Reader>(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 @@ fn chars_from_bytes<T: Reader>(buf: &~[u8], chars: &mut ~[char])
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 @@ fn chars_from_bytes<T: Reader>(buf: &~[u8], chars: &mut ~[char])
}
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 @@ fn chars_from_bytes<T: Reader>(buf: &~[u8], chars: &mut ~[char])
// we're split in a unicode char?
break;
}
buf.push_all(data);
let (offset, nbreq) = chars_from_bytes::<T>(&buf, &mut chars);
bytes.push_all(data);
let (offset, nbreq) = chars_from_bytes::<T>(&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 @@ fn read_char() -> char {
}
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 @@ fn read_be_uint(size: uint) -> uint {
}
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 @@ fn seek(offset: int, whence: SeekStyle) {
// duration of its lifetime.
// FIXME there really should be a better way to do this // #2004
impl<T: Reader, C> {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<Reader, ~str> {
}
// Byte buffer readers
// Byte readers
pub struct BytesReader {
bytes: &[u8],
mut pos: uint
}
pub type ByteBuf = {buf: &[const u8], mut pos: uint};
impl BytesReader: Reader {
fn read(bytes: &[mut u8], len: uint) -> uint {
let count = uint::min(len, self.bytes.len() - self.pos);
impl ByteBuf: Reader {
fn read(buf: &[mut u8], len: uint) -> uint {
let count = uint::min(len, self.buf.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<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
f({buf: bytes, mut pos: 0u} as Reader)
pub pure fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
f(BytesReader { bytes: bytes, pos: 0u } as Reader)
}
pub fn with_str_reader<T>(s: &str, f: fn(Reader) -> T) -> T {
@ -602,10 +606,10 @@ fn write_line(s: &str) {
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 stderr() -> Writer { fd_writer(libc::STDERR_FILENO as c_int, false) }
pub fn println(s: &str) { stdout().write_line(s); }
pub struct BytesWriter {
buf: DVec<u8>,
bytes: DVec<u8>,
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 @@ fn flush() -> int { (*self).flush() }
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 @@ fn buffered_file_writer_bad_name() {
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]);
}
}

View File

@ -32,7 +32,7 @@ pub fn console_off() {
#[cfg(notest)]
#[lang="log_type"]
pub fn log_type<T>(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 {

View File

@ -225,7 +225,7 @@ pub fn swap_unwrap<T>(opt: &mut Option<T>) -> T {
pub pure fn unwrap_expect<T>(opt: Option<T>, 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)
}

View File

@ -559,7 +559,7 @@ fn log_simple<T:Repr>() -> bool {
unsafe {
self.align(sys::min_align_of::<T>());
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::<T>());
true
}

View File

@ -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) }
}

View File

@ -897,7 +897,7 @@ fn read_field<T>(&self, name: &str, idx: uint, f: fn() -> T) -> T {
// 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);

View File

@ -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 {

View File

@ -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);
}

View File

@ -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 */ }

View File

@ -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

View File

@ -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);
}

View File

@ -34,9 +34,7 @@ fn indenter() -> _indenter {
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

View File

@ -282,8 +282,7 @@ fn fn_to_str(cx: ctxt, purity: ast::purity, proto: ty::fn_proto,
_ => { }
}
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 field_to_str(cx: ctxt, f: field) -> ~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) => {