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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)