Skip to content

Commit eddefbc

Browse files
committed
auto merge of #5212 : thestinger/rust/iter, r=graydon
A small step towards fixing #2827
2 parents 75c5bc9 + af645e8 commit eddefbc

39 files changed

+120
-118
lines changed

src/compiletest/header.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ pub fn load_props(testfile: &Path) -> TestProps {
5959
pp_exact = parse_pp_exact(ln, testfile);
6060
}
6161

62-
do parse_aux_build(ln).iter |ab| {
62+
for parse_aux_build(ln).each |ab| {
6363
aux_builds.push(*ab);
6464
}
6565

66-
do parse_exec_env(ln).iter |ee| {
66+
for parse_exec_env(ln).each |ee| {
6767
exec_env.push(*ee);
6868
}
6969

src/libcore/core.rc

-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ pub mod container;
132132
/* Common data structures */
133133

134134
pub mod option;
135-
#[path="iter-trait.rs"] #[merge = "iter-trait/option.rs"]
136-
pub mod option_iter;
137135
pub mod result;
138136
pub mod either;
139137
pub mod dvec;

src/libcore/option.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use cmp::{Eq,Ord};
4545
use kinds::Copy;
4646
use util;
4747
use num::Zero;
48+
use iter::BaseIter;
4849

4950
#[cfg(test)] use ptr;
5051
#[cfg(test)] use str;
@@ -228,12 +229,6 @@ pub pure fn map_default<T, U>(opt: &r/Option<T>, def: U,
228229
match *opt { None => def, Some(ref t) => f(t) }
229230
}
230231
231-
#[inline(always)]
232-
pub pure fn iter<T>(opt: &r/Option<T>, f: fn(x: &r/T)) {
233-
//! Performs an operation on the contained value by reference
234-
match *opt { None => (), Some(ref t) => f(t) }
235-
}
236-
237232
#[inline(always)]
238233
pub pure fn unwrap<T>(opt: Option<T>) -> T {
239234
/*!
@@ -281,6 +276,19 @@ pub pure fn expect<T>(opt: Option<T>, reason: &str) -> T {
281276
}
282277
}
283278
279+
impl<T> BaseIter<T> for Option<T> {
280+
/// Performs an operation on the contained value by reference
281+
#[inline(always)]
282+
pure fn each(&self, f: fn(x: &self/T) -> bool) {
283+
match *self { None => (), Some(ref t) => { f(t); } }
284+
}
285+
286+
#[inline(always)]
287+
pure fn size_hint(&self) -> Option<uint> {
288+
if self.is_some() { Some(1) } else { Some(0) }
289+
}
290+
}
291+
284292
pub impl<T> Option<T> {
285293
/// Returns true if the option equals `none`
286294
#[inline(always)]
@@ -339,10 +347,6 @@ pub impl<T> Option<T> {
339347
}
340348
}
341349
342-
/// Performs an operation on the contained value by reference
343-
#[inline(always)]
344-
pure fn iter(&self, f: fn(x: &self/T)) { iter(self, f) }
345-
346350
/**
347351
Gets an immutable reference to the value inside an option.
348352
@@ -476,7 +480,7 @@ fn test_option_dance() {
476480
let x = Some(());
477481
let mut y = Some(5);
478482
let mut y2 = 0;
479-
do x.iter |_x| {
483+
for x.each |_x| {
480484
y2 = swap_unwrap(&mut y);
481485
}
482486
assert y2 == 5;

src/libcore/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ mod tests {
12411241
setenv(~"HOME", ~"");
12421242
assert os::homedir().is_none();
12431243
1244-
oldhome.iter(|s| setenv(~"HOME", *s));
1244+
for oldhome.each |s| { setenv(~"HOME", *s) }
12451245
}
12461246
12471247
#[test]

src/libcore/task/spawn.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ fn each_ancestor(list: &mut AncestorList,
266266
* Step 3: Maybe unwind; compute return info for our caller.
267267
*##########################################################*/
268268
if need_unwind && !nobe_is_dead {
269-
do bail_opt.iter |bail_blk| {
269+
for bail_opt.each |bail_blk| {
270270
do with_parent_tg(&mut nobe.parent_group) |tg_opt| {
271271
(*bail_blk)(tg_opt)
272272
}
@@ -315,7 +315,7 @@ impl Drop for TCB {
315315
unsafe {
316316
// If we are failing, the whole taskgroup needs to die.
317317
if rt::rust_task_is_unwinding(self.me) {
318-
self.notifier.iter(|x| { x.failed = true; });
318+
for self.notifier.each |x| { x.failed = true; }
319319
// Take everybody down with us.
320320
do access_group(&self.tasks) |tg| {
321321
kill_taskgroup(tg, self.me, self.is_main);
@@ -339,9 +339,7 @@ impl Drop for TCB {
339339

340340
fn TCB(me: *rust_task, tasks: TaskGroupArc, ancestors: AncestorList,
341341
is_main: bool, notifier: Option<AutoNotify>) -> TCB {
342-
343-
let notifier = notifier;
344-
notifier.iter(|x| { x.failed = false; });
342+
for notifier.each |x| { x.failed = false; }
345343

346344
TCB {
347345
me: me,

src/librustc/metadata/encoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
710710
let idx = encode_info_for_struct(ecx, ebml_w, path,
711711
struct_def.fields, index);
712712
/* Encode the dtor */
713-
do struct_def.dtor.iter |dtor| {
713+
for struct_def.dtor.each |dtor| {
714714
index.push(entry {val: dtor.node.id, pos: ebml_w.writer.tell()});
715715
encode_info_for_ctor(ecx,
716716
ebml_w,
@@ -762,7 +762,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
762762
encode_region_param(ecx, ebml_w, item);
763763
/* Encode the dtor */
764764
/* Encode id for dtor */
765-
do struct_def.dtor.iter |dtor| {
765+
for struct_def.dtor.each |dtor| {
766766
do ebml_w.wr_tag(tag_item_dtor) {
767767
encode_def_id(ebml_w, local_def(dtor.node.id));
768768
}
@@ -816,7 +816,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
816816
ebml_w.writer.write(str::to_bytes(def_to_str(method_def_id)));
817817
ebml_w.end_tag();
818818
}
819-
do opt_trait.iter() |associated_trait| {
819+
for opt_trait.each |associated_trait| {
820820
encode_trait_ref(ebml_w, ecx, *associated_trait);
821821
}
822822
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));

src/librustc/middle/astencode.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -854,15 +854,16 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
854854
855855
debug!("Encoding side tables for id %d", id);
856856
857-
do option::iter(&tcx.def_map.find(&id)) |def| {
857+
for tcx.def_map.find(&id).each |def| {
858858
do ebml_w.tag(c::tag_table_def) {
859859
ebml_w.id(id);
860860
do ebml_w.tag(c::tag_table_val) {
861861
(*def).encode(&ebml_w)
862862
}
863863
}
864864
}
865-
do option::iter(&tcx.node_types.find(&(id as uint))) |&ty| {
865+
866+
for tcx.node_types.find(&(id as uint)).each |&ty| {
866867
do ebml_w.tag(c::tag_table_node_type) {
867868
ebml_w.id(id);
868869
do ebml_w.tag(c::tag_table_val) {
@@ -871,7 +872,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
871872
}
872873
}
873874
874-
do option::iter(&tcx.node_type_substs.find(&id)) |tys| {
875+
for tcx.node_type_substs.find(&id).each |tys| {
875876
do ebml_w.tag(c::tag_table_node_type_subst) {
876877
ebml_w.id(id);
877878
do ebml_w.tag(c::tag_table_val) {
@@ -880,7 +881,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
880881
}
881882
}
882883
883-
do option::iter(&tcx.freevars.find(&id)) |fv| {
884+
for tcx.freevars.find(&id).each |fv| {
884885
do ebml_w.tag(c::tag_table_freevars) {
885886
ebml_w.id(id);
886887
do ebml_w.tag(c::tag_table_val) {
@@ -892,7 +893,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
892893
}
893894
894895
let lid = ast::def_id { crate: ast::local_crate, node: id };
895-
do option::iter(&tcx.tcache.find(&lid)) |tpbt| {
896+
for tcx.tcache.find(&lid).each |tpbt| {
896897
do ebml_w.tag(c::tag_table_tcache) {
897898
ebml_w.id(id);
898899
do ebml_w.tag(c::tag_table_val) {
@@ -901,7 +902,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
901902
}
902903
}
903904
904-
do option::iter(&tcx.ty_param_bounds.find(&id)) |pbs| {
905+
for tcx.ty_param_bounds.find(&id).each |pbs| {
905906
do ebml_w.tag(c::tag_table_param_bounds) {
906907
ebml_w.id(id);
907908
do ebml_w.tag(c::tag_table_val) {
@@ -915,7 +916,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
915916
// is what we actually use in trans, all modes will have been
916917
// resolved.
917918
//
918-
//option::iter(tcx.inferred_modes.find(&id)) {|m|
919+
//for tcx.inferred_modes.find(&id).each |m| {
919920
// ebml_w.tag(c::tag_table_inferred_modes) {||
920921
// ebml_w.id(id);
921922
// ebml_w.tag(c::tag_table_val) {||
@@ -924,13 +925,13 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
924925
// }
925926
//}
926927
927-
do option::iter(&maps.mutbl_map.find(&id)) |_m| {
928+
if maps.mutbl_map.contains_key(&id) {
928929
do ebml_w.tag(c::tag_table_mutbl) {
929930
ebml_w.id(id);
930931
}
931932
}
932933
933-
do option::iter(&maps.last_use_map.find(&id)) |m| {
934+
for maps.last_use_map.find(&id).each |m| {
934935
do ebml_w.tag(c::tag_table_last_use) {
935936
ebml_w.id(id);
936937
do ebml_w.tag(c::tag_table_val) {
@@ -941,7 +942,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
941942
}
942943
}
943944
944-
do option::iter(&maps.method_map.find(&id)) |mme| {
945+
for maps.method_map.find(&id).each |mme| {
945946
do ebml_w.tag(c::tag_table_method_map) {
946947
ebml_w.id(id);
947948
do ebml_w.tag(c::tag_table_val) {
@@ -950,7 +951,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
950951
}
951952
}
952953
953-
do option::iter(&maps.vtable_map.find(&id)) |dr| {
954+
for maps.vtable_map.find(&id).each |dr| {
954955
do ebml_w.tag(c::tag_table_vtable_map) {
955956
ebml_w.id(id);
956957
do ebml_w.tag(c::tag_table_val) {
@@ -959,7 +960,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
959960
}
960961
}
961962
962-
do option::iter(&tcx.adjustments.find(&id)) |adj| {
963+
for tcx.adjustments.find(&id).each |adj| {
963964
do ebml_w.tag(c::tag_table_adjustments) {
964965
ebml_w.id(id);
965966
do ebml_w.tag(c::tag_table_val) {

src/librustc/middle/check_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn check_item(sess: Session,
5050
}
5151
item_enum(ref enum_definition, _) => {
5252
for (*enum_definition).variants.each |var| {
53-
do option::iter(&var.node.disr_expr) |ex| {
53+
for var.node.disr_expr.each |ex| {
5454
(v.visit_expr)(*ex, true, v);
5555
}
5656
}

src/librustc/middle/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ pub fn missing_ctor(cx: @MatchCheckCtxt,
362362
ty::ty_enum(eid, _) => {
363363
let mut found = ~[];
364364
for m.each |r| {
365-
do option::iter(&pat_ctor_id(cx, r[0])) |id| {
365+
for pat_ctor_id(cx, r[0]).each |id| {
366366
if !vec::contains(found, id) {
367367
found.push(/*bad*/copy *id);
368368
}

src/librustc/middle/kind.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub fn check_expr(e: @expr, cx: Context, v: visit::vt<Context>) {
192192
expr_unary(*)|expr_binary(*)|expr_method_call(*) => e.callee_id,
193193
_ => e.id
194194
};
195-
do option::iter(&cx.tcx.node_type_substs.find(&type_parameter_id)) |ts| {
195+
for cx.tcx.node_type_substs.find(&type_parameter_id).each |ts| {
196196
let bounds = match e.node {
197197
expr_path(_) => {
198198
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(&e.id));
@@ -253,7 +253,7 @@ pub fn check_expr(e: @expr, cx: Context, v: visit::vt<Context>) {
253253
fn check_ty(aty: @Ty, cx: Context, v: visit::vt<Context>) {
254254
match aty.node {
255255
ty_path(_, id) => {
256-
do option::iter(&cx.tcx.node_type_substs.find(&id)) |ts| {
256+
for cx.tcx.node_type_substs.find(&id).each |ts| {
257257
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(&id));
258258
let bounds = ty::lookup_item_type(cx.tcx, did).bounds;
259259
for vec::each2(*ts, *bounds) |ty, bound| {

src/librustc/middle/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ pub impl Liveness {
11011101

11021102
fn propagate_through_opt_expr(&self, opt_expr: Option<@expr>,
11031103
succ: LiveNode) -> LiveNode {
1104-
do opt_expr.foldl(succ) |succ, expr| {
1104+
do iter::foldl(&opt_expr, succ) |succ, expr| {
11051105
self.propagate_through_expr(*expr, *succ)
11061106
}
11071107
}

src/librustc/middle/resolve.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ pub impl Resolver {
10161016
fmt!("duplicate definition of %s %s",
10171017
namespace_to_str(ns),
10181018
*self.session.str_of(name)));
1019-
do child.span_for_namespace(ns).iter() |sp| {
1019+
for child.span_for_namespace(ns).each |sp| {
10201020
self.session.span_note(*sp,
10211021
fmt!("first definition of %s %s here:",
10221022
namespace_to_str(ns),
@@ -3457,7 +3457,7 @@ pub impl Resolver {
34573457
// then resolve the ty params
34583458
item_enum(ref enum_def, ref generics) => {
34593459
for (*enum_def).variants.each() |variant| {
3460-
do variant.node.disr_expr.iter() |dis_expr| {
3460+
for variant.node.disr_expr.each |dis_expr| {
34613461
// resolve the discriminator expr
34623462
// as a constant
34633463
self.with_constant_rib(|| {

src/librustc/middle/trans/base.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1211,10 +1211,10 @@ pub fn new_block(cx: fn_ctxt, parent: Option<block>, +kind: block_kind,
12111211
is_lpad,
12121212
opt_node_info,
12131213
cx);
1214-
do option::iter(&parent) |cx| {
1214+
for parent.each |cx| {
12151215
if cx.unreachable { Unreachable(bcx); }
12161216
};
1217-
return bcx;
1217+
bcx
12181218
}
12191219
}
12201220

@@ -1442,7 +1442,7 @@ pub fn alloc_local(cx: block, local: @ast::local) -> block {
14421442
};
14431443
let val = alloc_ty(cx, t);
14441444
if cx.sess().opts.debuginfo {
1445-
do option::iter(&simple_name) |name| {
1445+
for simple_name.each |name| {
14461446
str::as_c_str(*cx.ccx().sess.str_of(*name), |buf| {
14471447
unsafe {
14481448
llvm::LLVMSetValueName(val, buf)
@@ -1451,7 +1451,7 @@ pub fn alloc_local(cx: block, local: @ast::local) -> block {
14511451
}
14521452
}
14531453
cx.fcx.lllocals.insert(local.node.id, local_mem(val));
1454-
return cx;
1454+
cx
14551455
}
14561456

14571457

@@ -2011,7 +2011,7 @@ pub fn trans_struct_dtor(ccx: @CrateContext,
20112011
/* Look up the parent class's def_id */
20122012
let mut class_ty = ty::lookup_item_type(tcx, parent_id).ty;
20132013
/* Substitute in the class type if necessary */
2014-
do option::iter(&psubsts) |ss| {
2014+
for psubsts.each |ss| {
20152015
class_ty = ty::subst_tps(tcx, ss.tys, ss.self_ty, class_ty);
20162016
}
20172017
@@ -2028,7 +2028,7 @@ pub fn trans_struct_dtor(ccx: @CrateContext,
20282028
20292029
/* If we're monomorphizing, register the monomorphized decl
20302030
for the dtor */
2031-
do option::iter(&hash_id) |h_id| {
2031+
for hash_id.each |h_id| {
20322032
ccx.monomorphized.insert(*h_id, lldecl);
20332033
}
20342034
/* Translate the dtor body */
@@ -2142,7 +2142,7 @@ pub fn trans_struct_def(ccx: @CrateContext, struct_def: @ast::struct_def,
21422142
path: @ast_map::path,
21432143
id: ast::node_id) {
21442144
// Translate the destructor.
2145-
do option::iter(&struct_def.dtor) |dtor| {
2145+
for struct_def.dtor.each |dtor| {
21462146
trans_struct_dtor(ccx, /*bad*/copy *path, &dtor.node.body,
21472147
dtor.node.id, None, None, local_def(id));
21482148
};

src/librustc/middle/trans/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ pub fn trans_call_inner(
535535
} else if ret_in_loop {
536536
let ret_flag_result = bool_to_i1(bcx, Load(bcx, ret_flag.get()));
537537
bcx = do with_cond(bcx, ret_flag_result) |bcx| {
538-
do option::iter(&copy bcx.fcx.loop_ret) |&(flagptr, _)| {
538+
for (copy bcx.fcx.loop_ret).each |&(flagptr, _)| {
539539
Store(bcx, C_bool(true), flagptr);
540540
Store(bcx, C_bool(false), bcx.fcx.llretptr);
541541
}

src/librustc/middle/trans/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ pub fn build_closure(bcx0: block,
288288

289289
// If this is a `for` loop body, add two special environment
290290
// variables:
291-
do option::iter(&include_ret_handle) |flagptr| {
291+
for include_ret_handle.each |flagptr| {
292292
// Flag indicating we have returned (a by-ref bool):
293293
let flag_datum = Datum {val: *flagptr, ty: ty::mk_bool(tcx),
294294
mode: ByRef, source: ZeroMem};

0 commit comments

Comments
 (0)