Skip to content

Commit af645e8

Browse files
committed
replace option::iter with a BaseIter impl
1 parent a7de81a commit af645e8

39 files changed

+120
-118
lines changed

src/compiletest/header.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use ptr;
4848
use str;
4949
use util;
5050
use num::Zero;
51+
use iter::BaseIter;
5152

5253
/// The option type
5354
#[deriving_eq]
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ mod tests {
12371237
setenv(~"HOME", ~"");
12381238
assert os::homedir().is_none();
12391239
1240-
oldhome.iter(|s| setenv(~"HOME", *s));
1240+
for oldhome.each |s| { setenv(~"HOME", *s) }
12411241
}
12421242
12431243
#[test]

src/libcore/task/spawn.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn each_ancestor(list: &mut AncestorList,
268268
* Step 3: Maybe unwind; compute return info for our caller.
269269
*##########################################################*/
270270
if need_unwind && !nobe_is_dead {
271-
do bail_opt.iter |bail_blk| {
271+
for bail_opt.each |bail_blk| {
272272
do with_parent_tg(&mut nobe.parent_group) |tg_opt| {
273273
(*bail_blk)(tg_opt)
274274
}
@@ -317,7 +317,7 @@ impl Drop for TCB {
317317
unsafe {
318318
// If we are failing, the whole taskgroup needs to die.
319319
if rt::rust_task_is_unwinding(self.me) {
320-
self.notifier.iter(|x| { x.failed = true; });
320+
for self.notifier.each |x| { x.failed = true; }
321321
// Take everybody down with us.
322322
do access_group(&self.tasks) |tg| {
323323
kill_taskgroup(tg, self.me, self.is_main);
@@ -341,9 +341,7 @@ impl Drop for TCB {
341341

342342
fn TCB(me: *rust_task, tasks: TaskGroupArc, ancestors: AncestorList,
343343
is_main: bool, notifier: Option<AutoNotify>) -> TCB {
344-
345-
let notifier = notifier;
346-
notifier.iter(|x| { x.failed = false; });
344+
for notifier.each |x| { x.failed = false; }
347345

348346
TCB {
349347
me: me,

src/librustc/metadata/encoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
715715
let idx = encode_info_for_struct(ecx, ebml_w, path,
716716
struct_def.fields, index);
717717
/* Encode the dtor */
718-
do struct_def.dtor.iter |dtor| {
718+
for struct_def.dtor.each |dtor| {
719719
index.push(entry {val: dtor.node.id, pos: ebml_w.writer.tell()});
720720
encode_info_for_ctor(ecx,
721721
ebml_w,
@@ -767,7 +767,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
767767
encode_region_param(ecx, ebml_w, item);
768768
/* Encode the dtor */
769769
/* Encode id for dtor */
770-
do struct_def.dtor.iter |dtor| {
770+
for struct_def.dtor.each |dtor| {
771771
do ebml_w.wr_tag(tag_item_dtor) {
772772
encode_def_id(ebml_w, local_def(dtor.node.id));
773773
}
@@ -821,7 +821,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
821821
ebml_w.writer.write(str::to_bytes(def_to_str(method_def_id)));
822822
ebml_w.end_tag();
823823
}
824-
do opt_trait.iter() |associated_trait| {
824+
for opt_trait.each |associated_trait| {
825825
encode_trait_ref(ebml_w, ecx, *associated_trait);
826826
}
827827
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));

src/librustc/middle/astencode.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -860,15 +860,16 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
860860
861861
debug!("Encoding side tables for id %d", id);
862862
863-
do option::iter(&tcx.def_map.find(&id)) |def| {
863+
for tcx.def_map.find(&id).each |def| {
864864
do ebml_w.tag(c::tag_table_def) {
865865
ebml_w.id(id);
866866
do ebml_w.tag(c::tag_table_val) {
867867
(*def).encode(&ebml_w)
868868
}
869869
}
870870
}
871-
do option::iter(&tcx.node_types.find(&(id as uint))) |&ty| {
871+
872+
for tcx.node_types.find(&(id as uint)).each |&ty| {
872873
do ebml_w.tag(c::tag_table_node_type) {
873874
ebml_w.id(id);
874875
do ebml_w.tag(c::tag_table_val) {
@@ -877,7 +878,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
877878
}
878879
}
879880
880-
do option::iter(&tcx.node_type_substs.find(&id)) |tys| {
881+
for tcx.node_type_substs.find(&id).each |tys| {
881882
do ebml_w.tag(c::tag_table_node_type_subst) {
882883
ebml_w.id(id);
883884
do ebml_w.tag(c::tag_table_val) {
@@ -886,7 +887,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
886887
}
887888
}
888889
889-
do option::iter(&tcx.freevars.find(&id)) |fv| {
890+
for tcx.freevars.find(&id).each |fv| {
890891
do ebml_w.tag(c::tag_table_freevars) {
891892
ebml_w.id(id);
892893
do ebml_w.tag(c::tag_table_val) {
@@ -898,7 +899,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
898899
}
899900
900901
let lid = ast::def_id { crate: ast::local_crate, node: id };
901-
do option::iter(&tcx.tcache.find(&lid)) |tpbt| {
902+
for tcx.tcache.find(&lid).each |tpbt| {
902903
do ebml_w.tag(c::tag_table_tcache) {
903904
ebml_w.id(id);
904905
do ebml_w.tag(c::tag_table_val) {
@@ -907,7 +908,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
907908
}
908909
}
909910
910-
do option::iter(&tcx.ty_param_bounds.find(&id)) |pbs| {
911+
for tcx.ty_param_bounds.find(&id).each |pbs| {
911912
do ebml_w.tag(c::tag_table_param_bounds) {
912913
ebml_w.id(id);
913914
do ebml_w.tag(c::tag_table_val) {
@@ -921,7 +922,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
921922
// is what we actually use in trans, all modes will have been
922923
// resolved.
923924
//
924-
//option::iter(tcx.inferred_modes.find(&id)) {|m|
925+
//for tcx.inferred_modes.find(&id).each |m| {
925926
// ebml_w.tag(c::tag_table_inferred_modes) {||
926927
// ebml_w.id(id);
927928
// ebml_w.tag(c::tag_table_val) {||
@@ -930,13 +931,13 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
930931
// }
931932
//}
932933
933-
do option::iter(&maps.mutbl_map.find(&id)) |_m| {
934+
if maps.mutbl_map.contains_key(&id) {
934935
do ebml_w.tag(c::tag_table_mutbl) {
935936
ebml_w.id(id);
936937
}
937938
}
938939
939-
do option::iter(&maps.last_use_map.find(&id)) |m| {
940+
for maps.last_use_map.find(&id).each |m| {
940941
do ebml_w.tag(c::tag_table_last_use) {
941942
ebml_w.id(id);
942943
do ebml_w.tag(c::tag_table_val) {
@@ -947,7 +948,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
947948
}
948949
}
949950
950-
do option::iter(&maps.method_map.find(&id)) |mme| {
951+
for maps.method_map.find(&id).each |mme| {
951952
do ebml_w.tag(c::tag_table_method_map) {
952953
ebml_w.id(id);
953954
do ebml_w.tag(c::tag_table_val) {
@@ -956,7 +957,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
956957
}
957958
}
958959
959-
do option::iter(&maps.vtable_map.find(&id)) |dr| {
960+
for maps.vtable_map.find(&id).each |dr| {
960961
do ebml_w.tag(c::tag_table_vtable_map) {
961962
ebml_w.id(id);
962963
do ebml_w.tag(c::tag_table_val) {
@@ -965,7 +966,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
965966
}
966967
}
967968
968-
do option::iter(&tcx.adjustments.find(&id)) |adj| {
969+
for tcx.adjustments.find(&id).each |adj| {
969970
do ebml_w.tag(c::tag_table_adjustments) {
970971
ebml_w.id(id);
971972
do ebml_w.tag(c::tag_table_val) {

src/librustc/middle/check_const.rs

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

src/librustc/middle/check_match.rs

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

src/librustc/middle/kind.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub fn check_expr(e: @expr, cx: Context, v: visit::vt<Context>) {
194194
expr_unary(*)|expr_binary(*)|expr_method_call(*) => e.callee_id,
195195
_ => e.id
196196
};
197-
do option::iter(&cx.tcx.node_type_substs.find(&type_parameter_id)) |ts| {
197+
for cx.tcx.node_type_substs.find(&type_parameter_id).each |ts| {
198198
let bounds = match e.node {
199199
expr_path(_) => {
200200
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(&e.id));
@@ -255,7 +255,7 @@ pub fn check_expr(e: @expr, cx: Context, v: visit::vt<Context>) {
255255
fn check_ty(aty: @Ty, cx: Context, v: visit::vt<Context>) {
256256
match aty.node {
257257
ty_path(_, id) => {
258-
do option::iter(&cx.tcx.node_type_substs.find(&id)) |ts| {
258+
for cx.tcx.node_type_substs.find(&id).each |ts| {
259259
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(&id));
260260
let bounds = ty::lookup_item_type(cx.tcx, did).bounds;
261261
for vec::each2(*ts, *bounds) |ty, bound| {

src/librustc/middle/liveness.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ pub impl Resolver {
10221022
fmt!("duplicate definition of %s %s",
10231023
namespace_to_str(ns),
10241024
*self.session.str_of(name)));
1025-
do child.span_for_namespace(ns).iter() |sp| {
1025+
for child.span_for_namespace(ns).each |sp| {
10261026
self.session.span_note(*sp,
10271027
fmt!("first definition of %s %s here:",
10281028
namespace_to_str(ns),
@@ -3463,7 +3463,7 @@ pub impl Resolver {
34633463
// then resolve the ty params
34643464
item_enum(ref enum_def, ref generics) => {
34653465
for (*enum_def).variants.each() |variant| {
3466-
do variant.node.disr_expr.iter() |dis_expr| {
3466+
for variant.node.disr_expr.each |dis_expr| {
34673467
// resolve the discriminator expr
34683468
// as a constant
34693469
self.with_constant_rib(|| {

src/librustc/middle/trans/base.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,10 +1221,10 @@ pub fn new_block(cx: fn_ctxt, parent: Option<block>, +kind: block_kind,
12211221
is_lpad,
12221222
opt_node_info,
12231223
cx);
1224-
do option::iter(&parent) |cx| {
1224+
for parent.each |cx| {
12251225
if cx.unreachable { Unreachable(bcx); }
12261226
};
1227-
return bcx;
1227+
bcx
12281228
}
12291229
}
12301230

@@ -1452,7 +1452,7 @@ pub fn alloc_local(cx: block, local: @ast::local) -> block {
14521452
};
14531453
let val = alloc_ty(cx, t);
14541454
if cx.sess().opts.debuginfo {
1455-
do option::iter(&simple_name) |name| {
1455+
for simple_name.each |name| {
14561456
str::as_c_str(*cx.ccx().sess.str_of(*name), |buf| {
14571457
unsafe {
14581458
llvm::LLVMSetValueName(val, buf)
@@ -1461,7 +1461,7 @@ pub fn alloc_local(cx: block, local: @ast::local) -> block {
14611461
}
14621462
}
14631463
cx.fcx.lllocals.insert(local.node.id, local_mem(val));
1464-
return cx;
1464+
cx
14651465
}
14661466

14671467

@@ -2017,7 +2017,7 @@ pub fn trans_struct_dtor(ccx: @CrateContext,
20172017
/* Look up the parent class's def_id */
20182018
let mut class_ty = ty::lookup_item_type(tcx, parent_id).ty;
20192019
/* Substitute in the class type if necessary */
2020-
do option::iter(&psubsts) |ss| {
2020+
for psubsts.each |ss| {
20212021
class_ty = ty::subst_tps(tcx, ss.tys, ss.self_ty, class_ty);
20222022
}
20232023
@@ -2034,7 +2034,7 @@ pub fn trans_struct_dtor(ccx: @CrateContext,
20342034
20352035
/* If we're monomorphizing, register the monomorphized decl
20362036
for the dtor */
2037-
do option::iter(&hash_id) |h_id| {
2037+
for hash_id.each |h_id| {
20382038
ccx.monomorphized.insert(*h_id, lldecl);
20392039
}
20402040
/* Translate the dtor body */
@@ -2148,7 +2148,7 @@ pub fn trans_struct_def(ccx: @CrateContext, struct_def: @ast::struct_def,
21482148
path: @ast_map::path,
21492149
id: ast::node_id) {
21502150
// Translate the destructor.
2151-
do option::iter(&struct_def.dtor) |dtor| {
2151+
for struct_def.dtor.each |dtor| {
21522152
trans_struct_dtor(ccx, /*bad*/copy *path, &dtor.node.body,
21532153
dtor.node.id, None, None, local_def(id));
21542154
};

src/librustc/middle/trans/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ pub fn trans_call_inner(
537537
} else if ret_in_loop {
538538
let ret_flag_result = bool_to_i1(bcx, Load(bcx, ret_flag.get()));
539539
bcx = do with_cond(bcx, ret_flag_result) |bcx| {
540-
do option::iter(&copy bcx.fcx.loop_ret) |&(flagptr, _)| {
540+
for (copy bcx.fcx.loop_ret).each |&(flagptr, _)| {
541541
Store(bcx, C_bool(true), flagptr);
542542
Store(bcx, C_bool(false), bcx.fcx.llretptr);
543543
}

src/librustc/middle/trans/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ pub fn build_closure(bcx0: block,
293293

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

0 commit comments

Comments
 (0)