Skip to content

Commit a7ecde3

Browse files
committed
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.
1 parent 95423d2 commit a7ecde3

File tree

8 files changed

+16
-25
lines changed

8 files changed

+16
-25
lines changed

src/libcore/io.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ impl<T: Reader> T : ReaderUtil {
119119
}
120120
return (i, 0);
121121
}
122-
let mut bytes: ~[u8] = ~[];
123-
let mut chars: ~[char] = ~[];
122+
let mut bytes = ~[];
123+
let mut chars = ~[];
124124
// might need more bytes, but reading n will never over-read
125125
let mut nbread = n;
126126
while nbread > 0 {

src/libcore/logging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn console_off() {
3232
#[cfg(notest)]
3333
#[lang="log_type"]
3434
pub fn log_type<T>(level: u32, object: &T) {
35-
let bytes = do io::with_bytes_writer() |writer| {
35+
let bytes = do io::with_bytes_writer |writer| {
3636
repr::write_repr(writer, object);
3737
};
3838
unsafe {

src/libcore/repr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ impl ReprPrinter {
559559
unsafe {
560560
self.align(sys::min_align_of::<T>());
561561
let value_addr: &T = transmute(copy self.ptr);
562-
(*value_addr).write_repr(self.writer);
562+
value_addr.write_repr(self.writer);
563563
self.bump(sys::size_of::<T>());
564564
true
565565
}

src/libsyntax/attr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ fn attr_meta(attr: ast::attribute) -> @ast::meta_item { @attr.node.value }
9090

9191
// Get the meta_items from inside a vector of attributes
9292
fn attr_metas(attrs: ~[ast::attribute]) -> ~[@ast::meta_item] {
93-
let mut mitems = ~[];
94-
for attrs.each |a| { mitems.push(attr_meta(*a)); }
95-
return mitems;
93+
do attrs.map |a| { attr_meta(*a) }
9694
}
9795

9896
fn desugar_doc_attr(attr: &ast::attribute) -> ast::attribute {

src/rustc/back/upcall.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ fn declare_upcalls(targ_cfg: @session::config,
2727
fn decl(llmod: ModuleRef, prefix: ~str, name: ~str,
2828
tys: ~[TypeRef], rv: TypeRef) ->
2929
ValueRef {
30-
let mut arg_tys: ~[TypeRef] = ~[];
31-
for tys.each |t| { arg_tys.push(*t); }
30+
let arg_tys = tys.map(|t| *t);
3231
let fn_ty = T_fn(arg_tys, rv);
3332
return base::decl_cdecl_fn(llmod, prefix + name, fn_ty);
3433
}

src/rustc/metadata/decoder.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -598,13 +598,12 @@ fn get_enum_variants(intr: @ident_interner, cdata: cmd, id: ast::node_id,
598598
let ctor_ty = item_type({crate: cdata.cnum, node: id}, item,
599599
tcx, cdata);
600600
let name = item_name(intr, item);
601-
let mut arg_tys: ~[ty::t] = ~[];
602-
match ty::get(ctor_ty).sty {
603-
ty::ty_fn(f) => {
604-
for f.sig.inputs.each |a| { arg_tys.push(a.ty); }
605-
}
606-
_ => { /* Nullary enum variant. */ }
607-
}
601+
let arg_tys = match ty::get(ctor_ty).sty {
602+
ty::ty_fn(f) => f.sig.inputs.map(|a| a.ty),
603+
604+
// Nullary enum variant.
605+
_ => ~[],
606+
};
608607
match variant_disr_val(item) {
609608
Some(val) => { disr_val = val; }
610609
_ => { /* empty */ }

src/rustc/util/common.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ type flag = HashMap<~str, ()>;
3434
fn field_expr(f: ast::field) -> @ast::expr { return f.node.expr; }
3535

3636
fn field_exprs(fields: ~[ast::field]) -> ~[@ast::expr] {
37-
let mut es = ~[];
38-
for fields.each |f| { es.push(f.node.expr); }
39-
return es;
37+
fields.map(|f| f.node.expr)
4038
}
4139

4240
// Takes a predicate p, returns true iff p is true for any subexpressions

src/rustc/util/ppaux.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,7 @@ fn ty_to_str(cx: ctxt, typ: t) -> ~str {
282282
_ => { }
283283
}
284284
s += ~"(";
285-
let mut strs = ~[];
286-
for inputs.each |a| { strs.push(fn_input_to_str(cx, *a)); }
285+
let strs = inputs.map(|a| fn_input_to_str(cx, *a));
287286
s += str::connect(strs, ~", ");
288287
s += ~")";
289288
if ty::get(output).sty != ty_nil {
@@ -338,13 +337,11 @@ fn ty_to_str(cx: ctxt, typ: t) -> ~str {
338337
ty_unboxed_vec(tm) => { ~"unboxed_vec<" + mt_to_str(cx, tm) + ~">" }
339338
ty_type => ~"type",
340339
ty_rec(elems) => {
341-
let mut strs: ~[~str] = ~[];
342-
for elems.each |fld| { strs.push(field_to_str(cx, *fld)); }
340+
let strs = elems.map(|fld| field_to_str(cx, *fld));
343341
~"{" + str::connect(strs, ~",") + ~"}"
344342
}
345343
ty_tup(elems) => {
346-
let mut strs = ~[];
347-
for elems.each |elem| { strs.push(ty_to_str(cx, *elem)); }
344+
let strs = elems.map(|elem| ty_to_str(cx, *elem));
348345
~"(" + str::connect(strs, ~",") + ~")"
349346
}
350347
ty_fn(ref f) => {

0 commit comments

Comments
 (0)