From a7ecde33238f46ae473ceb63db95068f1ce6cffd Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 18 Oct 2012 09:14:11 -0700 Subject: [PATCH] libcore: minor code cleanup. This is minor and probably completely inconsequential to performance, but I find vec::map to be more clear than vec::each and a push. --- src/libcore/io.rs | 4 ++-- src/libcore/logging.rs | 2 +- src/libcore/repr.rs | 2 +- src/libsyntax/attr.rs | 4 +--- src/rustc/back/upcall.rs | 3 +-- src/rustc/metadata/decoder.rs | 13 ++++++------- src/rustc/util/common.rs | 4 +--- src/rustc/util/ppaux.rs | 9 +++------ 8 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/libcore/io.rs b/src/libcore/io.rs index 92315285d04..77f7b5023df 100644 --- a/src/libcore/io.rs +++ b/src/libcore/io.rs @@ -119,8 +119,8 @@ impl T : ReaderUtil { } return (i, 0); } - let mut bytes: ~[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 { diff --git a/src/libcore/logging.rs b/src/libcore/logging.rs index d4f3c0ea272..958d1ac56ea 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/repr.rs b/src/libcore/repr.rs index ff29953f09a..b246adcb1d7 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/libsyntax/attr.rs b/src/libsyntax/attr.rs index d08edd7af1d..4bd1679600f 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 a2c864f6f46..45e7cd4e9d4 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 f0911bd1aa2..1656efbd966 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/util/common.rs b/src/rustc/util/common.rs index 0c6ec267da8..123905adba1 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 27ace283fa0..8207082cf20 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) => {