Skip to content

replace option::iter with a BaseIter impl #5212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ pub fn load_props(testfile: &Path) -> TestProps {
pp_exact = parse_pp_exact(ln, testfile);
}

do parse_aux_build(ln).iter |ab| {
for parse_aux_build(ln).each |ab| {
aux_builds.push(*ab);
}

do parse_exec_env(ln).iter |ee| {
for parse_exec_env(ln).each |ee| {
exec_env.push(*ee);
}

Expand Down
2 changes: 0 additions & 2 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ pub mod container;
/* Common data structures */

pub mod option;
#[path="iter-trait.rs"] #[merge = "iter-trait/option.rs"]
pub mod option_iter;
pub mod result;
pub mod either;
pub mod dvec;
Expand Down
26 changes: 15 additions & 11 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use ptr;
use str;
use util;
use num::Zero;
use iter::BaseIter;

/// The option type
#[deriving_eq]
Expand Down Expand Up @@ -228,12 +229,6 @@ pub pure fn map_default<T, U>(opt: &r/Option<T>, def: U,
match *opt { None => def, Some(ref t) => f(t) }
}

#[inline(always)]
pub pure fn iter<T>(opt: &r/Option<T>, f: fn(x: &r/T)) {
//! Performs an operation on the contained value by reference
match *opt { None => (), Some(ref t) => f(t) }
}

#[inline(always)]
pub pure fn unwrap<T>(opt: Option<T>) -> T {
/*!
Expand Down Expand Up @@ -281,6 +276,19 @@ pub pure fn expect<T>(opt: Option<T>, reason: &str) -> T {
}
}

impl<T> BaseIter<T> for Option<T> {
/// Performs an operation on the contained value by reference
#[inline(always)]
pure fn each(&self, f: fn(x: &self/T) -> bool) {
match *self { None => (), Some(ref t) => { f(t); } }
}

#[inline(always)]
pure fn size_hint(&self) -> Option<uint> {
if self.is_some() { Some(1) } else { Some(0) }
}
}

pub impl<T> Option<T> {
/// Returns true if the option equals `none`
#[inline(always)]
Expand Down Expand Up @@ -339,10 +347,6 @@ pub impl<T> Option<T> {
}
}

/// Performs an operation on the contained value by reference
#[inline(always)]
pure fn iter(&self, f: fn(x: &self/T)) { iter(self, f) }

/**
Gets an immutable reference to the value inside an option.

Expand Down Expand Up @@ -476,7 +480,7 @@ fn test_option_dance() {
let x = Some(());
let mut y = Some(5);
let mut y2 = 0;
do x.iter |_x| {
for x.each |_x| {
y2 = swap_unwrap(&mut y);
}
assert y2 == 5;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ mod tests {
setenv(~"HOME", ~"");
assert os::homedir().is_none();

oldhome.iter(|s| setenv(~"HOME", *s));
for oldhome.each |s| { setenv(~"HOME", *s) }
}

#[test]
Expand Down
8 changes: 3 additions & 5 deletions src/libcore/task/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn each_ancestor(list: &mut AncestorList,
* Step 3: Maybe unwind; compute return info for our caller.
*##########################################################*/
if need_unwind && !nobe_is_dead {
do bail_opt.iter |bail_blk| {
for bail_opt.each |bail_blk| {
do with_parent_tg(&mut nobe.parent_group) |tg_opt| {
(*bail_blk)(tg_opt)
}
Expand Down Expand Up @@ -317,7 +317,7 @@ impl Drop for TCB {
unsafe {
// If we are failing, the whole taskgroup needs to die.
if rt::rust_task_is_unwinding(self.me) {
self.notifier.iter(|x| { x.failed = true; });
for self.notifier.each |x| { x.failed = true; }
// Take everybody down with us.
do access_group(&self.tasks) |tg| {
kill_taskgroup(tg, self.me, self.is_main);
Expand All @@ -341,9 +341,7 @@ impl Drop for TCB {

fn TCB(me: *rust_task, tasks: TaskGroupArc, ancestors: AncestorList,
is_main: bool, notifier: Option<AutoNotify>) -> TCB {

let notifier = notifier;
notifier.iter(|x| { x.failed = false; });
for notifier.each |x| { x.failed = false; }

TCB {
me: me,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
let idx = encode_info_for_struct(ecx, ebml_w, path,
struct_def.fields, index);
/* Encode the dtor */
do struct_def.dtor.iter |dtor| {
for struct_def.dtor.each |dtor| {
index.push(entry {val: dtor.node.id, pos: ebml_w.writer.tell()});
encode_info_for_ctor(ecx,
ebml_w,
Expand Down Expand Up @@ -767,7 +767,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
encode_region_param(ecx, ebml_w, item);
/* Encode the dtor */
/* Encode id for dtor */
do struct_def.dtor.iter |dtor| {
for struct_def.dtor.each |dtor| {
do ebml_w.wr_tag(tag_item_dtor) {
encode_def_id(ebml_w, local_def(dtor.node.id));
}
Expand Down Expand Up @@ -821,7 +821,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
ebml_w.writer.write(str::to_bytes(def_to_str(method_def_id)));
ebml_w.end_tag();
}
do opt_trait.iter() |associated_trait| {
for opt_trait.each |associated_trait| {
encode_trait_ref(ebml_w, ecx, *associated_trait);
}
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
Expand Down
25 changes: 13 additions & 12 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,15 +860,16 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,

debug!("Encoding side tables for id %d", id);

do option::iter(&tcx.def_map.find(&id)) |def| {
for tcx.def_map.find(&id).each |def| {
do ebml_w.tag(c::tag_table_def) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
(*def).encode(&ebml_w)
}
}
}
do option::iter(&tcx.node_types.find(&(id as uint))) |&ty| {

for tcx.node_types.find(&(id as uint)).each |&ty| {
do ebml_w.tag(c::tag_table_node_type) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
Expand All @@ -877,7 +878,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
}
}

do option::iter(&tcx.node_type_substs.find(&id)) |tys| {
for tcx.node_type_substs.find(&id).each |tys| {
do ebml_w.tag(c::tag_table_node_type_subst) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
Expand All @@ -886,7 +887,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
}
}

do option::iter(&tcx.freevars.find(&id)) |fv| {
for tcx.freevars.find(&id).each |fv| {
do ebml_w.tag(c::tag_table_freevars) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
Expand All @@ -898,7 +899,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
}

let lid = ast::def_id { crate: ast::local_crate, node: id };
do option::iter(&tcx.tcache.find(&lid)) |tpbt| {
for tcx.tcache.find(&lid).each |tpbt| {
do ebml_w.tag(c::tag_table_tcache) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
Expand All @@ -907,7 +908,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
}
}

do option::iter(&tcx.ty_param_bounds.find(&id)) |pbs| {
for tcx.ty_param_bounds.find(&id).each |pbs| {
do ebml_w.tag(c::tag_table_param_bounds) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
Expand All @@ -921,7 +922,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
// is what we actually use in trans, all modes will have been
// resolved.
//
//option::iter(tcx.inferred_modes.find(&id)) {|m|
//for tcx.inferred_modes.find(&id).each |m| {
// ebml_w.tag(c::tag_table_inferred_modes) {||
// ebml_w.id(id);
// ebml_w.tag(c::tag_table_val) {||
Expand All @@ -930,13 +931,13 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
// }
//}

do option::iter(&maps.mutbl_map.find(&id)) |_m| {
if maps.mutbl_map.contains_key(&id) {
do ebml_w.tag(c::tag_table_mutbl) {
ebml_w.id(id);
}
}

do option::iter(&maps.last_use_map.find(&id)) |m| {
for maps.last_use_map.find(&id).each |m| {
do ebml_w.tag(c::tag_table_last_use) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
Expand All @@ -947,7 +948,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
}
}

do option::iter(&maps.method_map.find(&id)) |mme| {
for maps.method_map.find(&id).each |mme| {
do ebml_w.tag(c::tag_table_method_map) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
Expand All @@ -956,7 +957,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
}
}

do option::iter(&maps.vtable_map.find(&id)) |dr| {
for maps.vtable_map.find(&id).each |dr| {
do ebml_w.tag(c::tag_table_vtable_map) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
Expand All @@ -965,7 +966,7 @@ fn encode_side_tables_for_id(ecx: @e::EncodeContext,
}
}

do option::iter(&tcx.adjustments.find(&id)) |adj| {
for tcx.adjustments.find(&id).each |adj| {
do ebml_w.tag(c::tag_table_adjustments) {
ebml_w.id(id);
do ebml_w.tag(c::tag_table_val) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn check_item(sess: Session,
}
item_enum(ref enum_definition, _) => {
for (*enum_definition).variants.each |var| {
do option::iter(&var.node.disr_expr) |ex| {
for var.node.disr_expr.each |ex| {
(v.visit_expr)(*ex, true, v);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ pub fn missing_ctor(cx: @MatchCheckCtxt,
ty::ty_enum(eid, _) => {
let mut found = ~[];
for m.each |r| {
do option::iter(&pat_ctor_id(cx, r[0])) |id| {
for pat_ctor_id(cx, r[0]).each |id| {
if !vec::contains(found, id) {
found.push(/*bad*/copy *id);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn check_expr(e: @expr, cx: Context, v: visit::vt<Context>) {
expr_unary(*)|expr_binary(*)|expr_method_call(*) => e.callee_id,
_ => e.id
};
do option::iter(&cx.tcx.node_type_substs.find(&type_parameter_id)) |ts| {
for cx.tcx.node_type_substs.find(&type_parameter_id).each |ts| {
let bounds = match e.node {
expr_path(_) => {
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(&e.id));
Expand Down Expand Up @@ -255,7 +255,7 @@ pub fn check_expr(e: @expr, cx: Context, v: visit::vt<Context>) {
fn check_ty(aty: @Ty, cx: Context, v: visit::vt<Context>) {
match aty.node {
ty_path(_, id) => {
do option::iter(&cx.tcx.node_type_substs.find(&id)) |ts| {
for cx.tcx.node_type_substs.find(&id).each |ts| {
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(&id));
let bounds = ty::lookup_item_type(cx.tcx, did).bounds;
for vec::each2(*ts, *bounds) |ty, bound| {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ pub impl Liveness {

fn propagate_through_opt_expr(&self, opt_expr: Option<@expr>,
succ: LiveNode) -> LiveNode {
do opt_expr.foldl(succ) |succ, expr| {
do iter::foldl(&opt_expr, succ) |succ, expr| {
self.propagate_through_expr(*expr, *succ)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ pub impl Resolver {
fmt!("duplicate definition of %s %s",
namespace_to_str(ns),
*self.session.str_of(name)));
do child.span_for_namespace(ns).iter() |sp| {
for child.span_for_namespace(ns).each |sp| {
self.session.span_note(*sp,
fmt!("first definition of %s %s here:",
namespace_to_str(ns),
Expand Down Expand Up @@ -3463,7 +3463,7 @@ pub impl Resolver {
// then resolve the ty params
item_enum(ref enum_def, ref generics) => {
for (*enum_def).variants.each() |variant| {
do variant.node.disr_expr.iter() |dis_expr| {
for variant.node.disr_expr.each |dis_expr| {
// resolve the discriminator expr
// as a constant
self.with_constant_rib(|| {
Expand Down
14 changes: 7 additions & 7 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,10 +1221,10 @@ pub fn new_block(cx: fn_ctxt, parent: Option<block>, +kind: block_kind,
is_lpad,
opt_node_info,
cx);
do option::iter(&parent) |cx| {
for parent.each |cx| {
if cx.unreachable { Unreachable(bcx); }
};
return bcx;
bcx
}
}

Expand Down Expand Up @@ -1452,7 +1452,7 @@ pub fn alloc_local(cx: block, local: @ast::local) -> block {
};
let val = alloc_ty(cx, t);
if cx.sess().opts.debuginfo {
do option::iter(&simple_name) |name| {
for simple_name.each |name| {
str::as_c_str(*cx.ccx().sess.str_of(*name), |buf| {
unsafe {
llvm::LLVMSetValueName(val, buf)
Expand All @@ -1461,7 +1461,7 @@ pub fn alloc_local(cx: block, local: @ast::local) -> block {
}
}
cx.fcx.lllocals.insert(local.node.id, local_mem(val));
return cx;
cx
}


Expand Down Expand Up @@ -2017,7 +2017,7 @@ pub fn trans_struct_dtor(ccx: @CrateContext,
/* Look up the parent class's def_id */
let mut class_ty = ty::lookup_item_type(tcx, parent_id).ty;
/* Substitute in the class type if necessary */
do option::iter(&psubsts) |ss| {
for psubsts.each |ss| {
class_ty = ty::subst_tps(tcx, ss.tys, ss.self_ty, class_ty);
}

Expand All @@ -2034,7 +2034,7 @@ pub fn trans_struct_dtor(ccx: @CrateContext,

/* If we're monomorphizing, register the monomorphized decl
for the dtor */
do option::iter(&hash_id) |h_id| {
for hash_id.each |h_id| {
ccx.monomorphized.insert(*h_id, lldecl);
}
/* Translate the dtor body */
Expand Down Expand Up @@ -2148,7 +2148,7 @@ pub fn trans_struct_def(ccx: @CrateContext, struct_def: @ast::struct_def,
path: @ast_map::path,
id: ast::node_id) {
// Translate the destructor.
do option::iter(&struct_def.dtor) |dtor| {
for struct_def.dtor.each |dtor| {
trans_struct_dtor(ccx, /*bad*/copy *path, &dtor.node.body,
dtor.node.id, None, None, local_def(id));
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ pub fn trans_call_inner(
} else if ret_in_loop {
let ret_flag_result = bool_to_i1(bcx, Load(bcx, ret_flag.get()));
bcx = do with_cond(bcx, ret_flag_result) |bcx| {
do option::iter(&copy bcx.fcx.loop_ret) |&(flagptr, _)| {
for (copy bcx.fcx.loop_ret).each |&(flagptr, _)| {
Store(bcx, C_bool(true), flagptr);
Store(bcx, C_bool(false), bcx.fcx.llretptr);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub fn build_closure(bcx0: block,

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