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

0 commit comments

Comments
 (0)