Skip to content

Commit 438bdd6

Browse files
committed
core: More option demoding
1 parent 7b0ed94 commit 438bdd6

File tree

24 files changed

+60
-69
lines changed

24 files changed

+60
-69
lines changed

src/compiletest/header.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ fn load_props(testfile: &Path) -> test_props {
4141
}
4242

4343
do parse_aux_build(ln).iter |ab| {
44-
aux_builds.push(ab);
44+
aux_builds.push(*ab);
4545
}
4646

4747
do parse_exec_env(ln).iter |ee| {
48-
exec_env.push(ee);
48+
exec_env.push(*ee);
4949
}
5050
};
5151
return {

src/libcore/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ pure fn from_elem<T: Copy,BT: Buildable<T>>(n_elts: uint, t: T) -> BT {
289289
#[inline(always)]
290290
pure fn append<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
291291
lhs: IT, rhs: IT) -> BT {
292-
let size_opt = lhs.size_hint().chain(
293-
|sz1| rhs.size_hint().map(|sz2| sz1+*sz2));
292+
let size_opt = lhs.size_hint().chain_ref(
293+
|sz1| rhs.size_hint().map(|sz2| *sz1+*sz2));
294294
do build_sized_opt(size_opt) |push| {
295295
for lhs.each |x| { push(*x); }
296296
for rhs.each |x| { push(*x); }

src/libcore/option.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,18 @@ pure fn map_consume<T, U>(+opt: Option<T>, f: fn(+v: T) -> U) -> Option<U> {
7575
if opt.is_some() { Some(f(option::unwrap(move opt))) } else { None }
7676
}
7777

78-
pure fn chain<T, U>(opt: &Option<T>, f: fn(T) -> Option<U>) -> Option<U> {
78+
pure fn chain<T, U>(+opt: Option<T>, f: fn(+t: T) -> Option<U>) -> Option<U> {
7979
/*!
8080
* Update an optional value by optionally running its content through a
8181
* function that returns an option.
8282
*/
8383

84-
match *opt { Some(x) => f(x), None => None }
84+
// XXX write with move match
85+
if opt.is_some() {
86+
f(unwrap(opt))
87+
} else {
88+
None
89+
}
8590
}
8691

8792
pure fn chain_ref<T, U>(opt: &Option<T>,
@@ -139,14 +144,7 @@ pure fn map_default<T, U>(opt: &Option<T>, +def: U,
139144
match *opt { None => move def, Some(ref t) => f(t) }
140145
}
141146

142-
// This should change to by-copy mode; use iter_ref below for by reference
143-
pure fn iter<T>(opt: &Option<T>, f: fn(T)) {
144-
//! Performs an operation on the contained value or does nothing
145-
146-
match *opt { None => (), Some(t) => f(t) }
147-
}
148-
149-
pure fn iter_ref<T>(opt: &Option<T>, f: fn(x: &T)) {
147+
pure fn iter<T>(opt: &Option<T>, f: fn(x: &T)) {
150148
//! Performs an operation on the contained value by reference
151149
match *opt { None => (), Some(ref t) => f(t) }
152150
}
@@ -182,13 +180,6 @@ pure fn unwrap_expect<T>(+opt: Option<T>, reason: &str) -> T {
182180

183181
// Some of these should change to be &Option<T>, some should not. See below.
184182
impl<T> Option<T> {
185-
/**
186-
* Update an optional value by optionally running its content through a
187-
* function that returns an option.
188-
*/
189-
pure fn chain<U>(f: fn(T) -> Option<U>) -> Option<U> { chain(&self, f) }
190-
/// Performs an operation on the contained value or does nothing
191-
pure fn iter(f: fn(T)) { iter(&self, f) }
192183
/// Returns true if the option equals `none`
193184
pure fn is_none() -> bool { is_none(&self) }
194185
/// Returns true if the option contains some value
@@ -207,7 +198,7 @@ impl<T> &Option<T> {
207198
pure fn map_default<U>(+def: U, f: fn(x: &T) -> U) -> U
208199
{ map_default(self, move def, f) }
209200
/// Performs an operation on the contained value by reference
210-
pure fn iter_ref(f: fn(x: &T)) { iter_ref(self, f) }
201+
pure fn iter(f: fn(x: &T)) { iter(self, f) }
211202
/// Maps a `some` value from one type to another by reference
212203
pure fn map<U>(f: fn(x: &T) -> U) -> Option<U> { map(self, f) }
213204
/// Gets an immutable reference to the value inside a `some`.

src/libcore/os.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ mod tests {
884884
setenv(~"HOME", ~"");
885885
assert os::homedir().is_none();
886886

887-
oldhome.iter(|s| setenv(~"HOME", s));
887+
oldhome.iter(|s| setenv(~"HOME", *s));
888888
}
889889

890890
#[test]
@@ -911,9 +911,9 @@ mod tests {
911911
setenv(~"USERPROFILE", ~"/home/PaloAlto");
912912
assert os::homedir() == Some(Path("/home/MountainView"));
913913

914-
option::iter(&oldhome, |s| setenv(~"HOME", s));
914+
option::iter(&oldhome, |s| setenv(~"HOME", *s));
915915
option::iter(&olduserprofile,
916-
|s| setenv(~"USERPROFILE", s));
916+
|s| setenv(~"USERPROFILE", *s));
917917
}
918918

919919
#[test]

src/libcore/task/spawn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ fn each_ancestor(list: &mut AncestorList,
240240
if need_unwind && !nobe_is_dead {
241241
do bail_opt.iter |bail_blk| {
242242
do with_parent_tg(&mut nobe.parent_group) |tg_opt| {
243-
bail_blk(tg_opt)
243+
(*bail_blk)(tg_opt)
244244
}
245245
}
246246
}

src/libsyntax/print/pprust.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ fn print_mac(s: ps, m: ast::mac) {
979979
Some(@{node: ast::expr_vec(_, _), _}) => (),
980980
_ => word(s.s, ~" ")
981981
}
982-
arg.iter(|a| print_expr(s, a));
982+
arg.iter(|a| print_expr(s, *a));
983983
// FIXME: extension 'body' (#2339)
984984
}
985985
ast::mac_invoc_tt(pth, tts) => {
@@ -1177,7 +1177,7 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
11771177
ast::expr_loop(blk, opt_ident) => {
11781178
head(s, ~"loop");
11791179
space(s.s);
1180-
opt_ident.iter(|ident| {print_ident(s, ident); space(s.s)});
1180+
opt_ident.iter(|ident| {print_ident(s, *ident); space(s.s)});
11811181
print_block(s, blk);
11821182
}
11831183
ast::expr_match(expr, arms) => {
@@ -1360,12 +1360,12 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
13601360
ast::expr_break(opt_ident) => {
13611361
word(s.s, ~"break");
13621362
space(s.s);
1363-
opt_ident.iter(|ident| {print_ident(s, ident); space(s.s)});
1363+
opt_ident.iter(|ident| {print_ident(s, *ident); space(s.s)});
13641364
}
13651365
ast::expr_again(opt_ident) => {
13661366
word(s.s, ~"loop");
13671367
space(s.s);
1368-
opt_ident.iter(|ident| {print_ident(s, ident); space(s.s)});
1368+
opt_ident.iter(|ident| {print_ident(s, *ident); space(s.s)});
13691369
}
13701370
ast::expr_ret(result) => {
13711371
word(s.s, ~"return");

src/libsyntax/visit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn visit_pat<E>(p: @pat, e: E, v: vt<E>) {
241241
v.visit_pat(inner, e, v),
242242
pat_ident(_, path, inner) => {
243243
visit_path(path, e, v);
244-
do option::iter(&inner) |subpat| { v.visit_pat(subpat, e, v)};
244+
do option::iter(&inner) |subpat| { v.visit_pat(*subpat, e, v)};
245245
}
246246
pat_lit(ex) => v.visit_expr(ex, e, v),
247247
pat_range(e1, e2) => { v.visit_expr(e1, e, v); v.visit_expr(e2, e, v); }
@@ -342,10 +342,10 @@ fn visit_struct_def<E>(sd: @struct_def, nm: ast::ident, tps: ~[ty_param],
342342
visit_path(p.path, e, v);
343343
}
344344
do option::iter(&sd.ctor) |ctor| {
345-
visit_class_ctor_helper(ctor, nm, tps, ast_util::local_def(id), e, v);
345+
visit_class_ctor_helper(*ctor, nm, tps, ast_util::local_def(id), e, v);
346346
};
347347
do option::iter(&sd.dtor) |dtor| {
348-
visit_class_dtor_helper(dtor, tps, ast_util::local_def(id), e, v)
348+
visit_class_dtor_helper(*dtor, tps, ast_util::local_def(id), e, v)
349349
};
350350
}
351351

src/rustc/metadata/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn field_mutability(d: ebml::Doc) -> ast::class_mutability {
204204
}
205205

206206
fn variant_disr_val(d: ebml::Doc) -> Option<int> {
207-
do option::chain(&ebml::maybe_get_doc(d, tag_disr_val)) |val_doc| {
207+
do option::chain(ebml::maybe_get_doc(d, tag_disr_val)) |val_doc| {
208208
int::parse_bytes(ebml::doc_data(val_doc), 10u)
209209
}
210210
}

src/rustc/metadata/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::Writer, item: @item,
609609
ecx.tcx.sess.str_of(item.ident) +
610610
~"_dtor"),
611611
path, if tps.len() > 0u {
612-
Some(ii_dtor(dtor, item.ident, tps,
612+
Some(ii_dtor(*dtor, item.ident, tps,
613613
local_def(item.id))) }
614614
else { None }, tps);
615615
}
@@ -715,7 +715,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::Writer, item: @item,
715715
ebml_w.end_tag();
716716
}
717717
do opt_trait.iter() |associated_trait| {
718-
encode_trait_ref(ebml_w, ecx, associated_trait)
718+
encode_trait_ref(ebml_w, ecx, *associated_trait)
719719
}
720720
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
721721
ebml_w.end_tag();

src/rustc/middle/astencode.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -723,15 +723,15 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
723723
do ebml_w.tag(c::tag_table_def) {
724724
ebml_w.id(id);
725725
do ebml_w.tag(c::tag_table_val) {
726-
ast::serialize_def(ebml_w, def)
726+
ast::serialize_def(ebml_w, *def)
727727
}
728728
}
729729
}
730730
do option::iter(&(*tcx.node_types).find(id as uint)) |ty| {
731731
do ebml_w.tag(c::tag_table_node_type) {
732732
ebml_w.id(id);
733733
do ebml_w.tag(c::tag_table_val) {
734-
ebml_w.emit_ty(ecx, ty);
734+
ebml_w.emit_ty(ecx, *ty);
735735
}
736736
}
737737
}
@@ -740,7 +740,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
740740
do ebml_w.tag(c::tag_table_node_type_subst) {
741741
ebml_w.id(id);
742742
do ebml_w.tag(c::tag_table_val) {
743-
ebml_w.emit_tys(ecx, tys)
743+
ebml_w.emit_tys(ecx, *tys)
744744
}
745745
}
746746
}
@@ -749,7 +749,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
749749
do ebml_w.tag(c::tag_table_freevars) {
750750
ebml_w.id(id);
751751
do ebml_w.tag(c::tag_table_val) {
752-
do ebml_w.emit_from_vec(*fv) |fv_entry| {
752+
do ebml_w.emit_from_vec(**fv) |fv_entry| {
753753
encode_freevar_entry(ebml_w, *fv_entry)
754754
}
755755
}
@@ -761,7 +761,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
761761
do ebml_w.tag(c::tag_table_tcache) {
762762
ebml_w.id(id);
763763
do ebml_w.tag(c::tag_table_val) {
764-
ebml_w.emit_tpbt(ecx, tpbt);
764+
ebml_w.emit_tpbt(ecx, *tpbt);
765765
}
766766
}
767767
}
@@ -770,7 +770,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
770770
do ebml_w.tag(c::tag_table_param_bounds) {
771771
ebml_w.id(id);
772772
do ebml_w.tag(c::tag_table_val) {
773-
ebml_w.emit_bounds(ecx, pbs)
773+
ebml_w.emit_bounds(ecx, *pbs)
774774
}
775775
}
776776
}
@@ -810,7 +810,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
810810
do ebml_w.tag(c::tag_table_method_map) {
811811
ebml_w.id(id);
812812
do ebml_w.tag(c::tag_table_val) {
813-
serialize_method_map_entry(ecx, ebml_w, mme)
813+
serialize_method_map_entry(ecx, ebml_w, *mme)
814814
}
815815
}
816816
}
@@ -819,7 +819,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
819819
do ebml_w.tag(c::tag_table_vtable_map) {
820820
ebml_w.id(id);
821821
do ebml_w.tag(c::tag_table_val) {
822-
encode_vtable_res(ecx, ebml_w, dr);
822+
encode_vtable_res(ecx, ebml_w, *dr);
823823
}
824824
}
825825
}
@@ -828,7 +828,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
828828
do ebml_w.tag(c::tag_table_adjustments) {
829829
ebml_w.id(id);
830830
do ebml_w.tag(c::tag_table_val) {
831-
ty::serialize_AutoAdjustment(ebml_w, *adj)
831+
ty::serialize_AutoAdjustment(ebml_w, **adj)
832832
}
833833
}
834834
}

src/rustc/middle/check_alt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ fn missing_ctor(tcx: ty::ctxt, m: matrix, left_ty: ty::t) -> Option<ctor> {
269269
let mut found = ~[];
270270
for m.each |r| {
271271
do option::iter(&pat_ctor_id(tcx, r[0])) |id| {
272-
if !vec::contains(found, id) { found.push(id); }
272+
if !vec::contains(found, *id) { found.push(*id); }
273273
}
274274
}
275275
let variants = ty::enum_variants(tcx, eid);

src/rustc/middle/check_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn check_item(sess: session, ast_map: ast_map::map,
2828
item_enum(enum_definition, _) => {
2929
for enum_definition.variants.each |var| {
3030
do option::iter(&var.node.disr_expr) |ex| {
31-
v.visit_expr(ex, true, v);
31+
v.visit_expr(*ex, true, v);
3232
}
3333
}
3434
}

src/rustc/middle/kind.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
264264
~"non path/method call expr has type substs??")
265265
}
266266
};
267-
if vec::len(ts) != vec::len(*bounds) {
267+
if vec::len(*ts) != vec::len(*bounds) {
268268
// Fail earlier to make debugging easier
269269
fail fmt!("Internal error: in kind::check_expr, length \
270270
mismatch between actual and declared bounds: actual = \
271271
%s (%u tys), declared = %? (%u tys)",
272-
tys_to_str(cx.tcx, ts), ts.len(),
272+
tys_to_str(cx.tcx, *ts), ts.len(),
273273
*bounds, (*bounds).len());
274274
}
275-
do vec::iter2(ts, *bounds) |ty, bound| {
275+
do vec::iter2(*ts, *bounds) |ty, bound| {
276276
check_bounds(cx, id_to_use, e.span, ty, bound)
277277
}
278278
}
@@ -376,7 +376,7 @@ fn check_ty(aty: @ty, cx: ctx, v: visit::vt<ctx>) {
376376
do option::iter(&cx.tcx.node_type_substs.find(id)) |ts| {
377377
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(id));
378378
let bounds = ty::lookup_item_type(cx.tcx, did).bounds;
379-
do vec::iter2(ts, *bounds) |ty, bound| {
379+
do vec::iter2(*ts, *bounds) |ty, bound| {
380380
check_bounds(cx, aty.id, aty.span, ty, bound)
381381
}
382382
}

src/rustc/middle/mem_categorization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,14 +670,14 @@ impl &mem_categorization_ctxt {
670670

671671
match deref_kind(self.tcx, base_cmt.ty) {
672672
deref_ptr(ptr) => {
673-
let lp = do base_cmt.lp.chain |l| {
673+
let lp = do base_cmt.lp.chain_ref |l| {
674674
// Given that the ptr itself is loanable, we can
675675
// loan out deref'd uniq ptrs as the data they are
676676
// the only way to reach the data they point at.
677677
// Other ptr types admit aliases and are therefore
678678
// not loanable.
679679
match ptr {
680-
uniq_ptr => {Some(@lp_deref(l, ptr))}
680+
uniq_ptr => {Some(@lp_deref(*l, ptr))}
681681
gc_ptr | region_ptr(_) | unsafe_ptr => {None}
682682
}
683683
};

src/rustc/middle/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ impl Resolver {
923923
namespace_to_str(ns),
924924
self.session.str_of(name)));
925925
do child.span_for_namespace(ns).iter() |sp| {
926-
self.session.span_note(sp,
926+
self.session.span_note(*sp,
927927
#fmt("First definition of %s %s here:",
928928
namespace_to_str(ns),
929929
self.session.str_of(name)));

src/rustc/middle/trans/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ fn alloc_local(cx: block, local: @ast::local) -> block {
12521252
let val = alloc_ty(cx, t);
12531253
if cx.sess().opts.debuginfo {
12541254
do option::iter(&simple_name) |name| {
1255-
str::as_c_str(cx.ccx().sess.str_of(name), |buf| {
1255+
str::as_c_str(cx.ccx().sess.str_of(*name), |buf| {
12561256
llvm::LLVMSetValueName(val, buf)
12571257
});
12581258
}
@@ -1778,7 +1778,7 @@ fn trans_class_dtor(ccx: @crate_ctxt, path: path,
17781778
/* If we're monomorphizing, register the monomorphized decl
17791779
for the dtor */
17801780
do option::iter(&hash_id) |h_id| {
1781-
ccx.monomorphized.insert(h_id, lldecl);
1781+
ccx.monomorphized.insert(*h_id, lldecl);
17821782
}
17831783
/* Translate the dtor body */
17841784
trans_fn(ccx, path, ast_util::dtor_dec(),

src/rustc/middle/trans/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ fn build_closure(bcx0: block,
281281
// variables:
282282
do option::iter(&include_ret_handle) |flagptr| {
283283
// Flag indicating we have returned (a by-ref bool):
284-
let flag_datum = Datum {val: flagptr, ty: ty::mk_bool(tcx),
284+
let flag_datum = Datum {val: *flagptr, ty: ty::mk_bool(tcx),
285285
mode: ByRef, source: FromLvalue};
286286
env_vals.push(EnvValue {action: EnvRef,
287287
datum: flag_datum});

src/rustc/middle/trans/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ type optional_boxed_ast_expr = Option<@ast::expr>;
472472

473473
impl optional_boxed_ast_expr: get_node_info {
474474
fn info() -> Option<node_info> {
475-
self.chain(|s| s.info())
475+
self.chain_ref(|s| s.info())
476476
}
477477
}
478478

0 commit comments

Comments
 (0)