Skip to content

Commit 21be137

Browse files
committed
Rename some core::option functions
from_maybe => get_with_default maybe => with_option may => with_option_do I know these names are kind of ridiculous, but it's the best I could think of. Feel free to bikeshed. Closes #2081
1 parent 987bc23 commit 21be137

31 files changed

+67
-64
lines changed

Diff for: doc/tutorial.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ programs that just can't be typed.
15511551

15521552
~~~~
15531553
let n = option::none;
1554-
# option::may(n, fn&(&&x:int) {})
1554+
# option::with_option_do(n, fn&(&&x:int) {})
15551555
~~~~
15561556

15571557
If you never do anything else with `n`, the compiler will not be able

Diff for: src/compiletest/header.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn load_props(testfile: str) -> test_props {
4040
pp_exact = parse_pp_exact(ln, testfile);
4141
}
4242

43-
option::may(parse_aux_build(ln)) {|ab|
43+
option::with_option_do(parse_aux_build(ln)) {|ab|
4444
aux_builds += [ab];
4545
}
4646
};

Diff for: src/libcore/iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<A> of iterable<A> for [A] {
2929

3030
impl<A> of iterable<A> for option<A> {
3131
fn iter(blk: fn(A)) {
32-
option::may(self, blk)
32+
option::with_option_do(self, blk)
3333
}
3434
}
3535

Diff for: src/libcore/option.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ pure fn is_some<T>(opt: option<T>) -> bool {
5252
!is_none(opt)
5353
}
5454

55-
pure fn from_maybe<T: copy>(opt: option<T>, def: T) -> T {
55+
pure fn get_or_default<T: copy>(opt: option<T>, def: T) -> T {
5656
#[doc = "Returns the contained value or a default"];
5757

5858
alt opt { some(x) { x } none { def } }
5959
}
6060

61-
fn maybe<T, U: copy>(opt: option<T>, def: U, f: fn(T) -> U) -> U {
61+
fn with_option<T, U: copy>(opt: option<T>, def: U, f: fn(T) -> U) -> U {
6262
#[doc = "Applies a function to the contained value or returns a default"];
6363

6464
alt opt { none { def } some(t) { f(t) } }
6565
}
6666

67-
fn may<T>(opt: option<T>, f: fn(T)) {
67+
fn with_option_do<T>(opt: option<T>, f: fn(T)) {
6868
#[doc = "Performs an operation on the contained value or does nothing"];
6969

7070
alt opt { none { } some(t) { f(t); } }
@@ -94,11 +94,12 @@ impl extensions<T:copy> for option<T> {
9494
"]
9595
fn chain<U>(f: fn(T) -> option<U>) -> option<U> { chain(self, f) }
9696
#[doc = "Returns the contained value or a default"]
97-
fn from_maybe(def: T) -> T { from_maybe(self, def) }
97+
fn get_or_default(def: T) -> T { get_or_default(self, def) }
9898
#[doc = "Applies a function to the contained value or returns a default"]
99-
fn maybe<U: copy>(def: U, f: fn(T) -> U) -> U { maybe(self, def, f) }
99+
fn with_option<U: copy>(def: U, f: fn(T) -> U) -> U
100+
{ with_option(self, def, f) }
100101
#[doc = "Performs an operation on the contained value or does nothing"]
101-
fn may(f: fn(T)) { may(self, f) }
102+
fn with_option_do(f: fn(T)) { with_option_do(self, f) }
102103
#[doc = "
103104
Gets the value out of an option
104105

Diff for: src/libcore/os.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ mod tests {
702702
setenv("HOME", "");
703703
assert os::homedir() == none;
704704

705-
option::may(oldhome, {|s| setenv("HOME", s)});
705+
option::with_option_do(oldhome, {|s| setenv("HOME", s)});
706706
}
707707

708708
#[test]
@@ -732,8 +732,9 @@ mod tests {
732732
setenv("USERPROFILE", "/home/PaloAlto");
733733
assert os::homedir() == some("/home/MountainView");
734734

735-
option::may(oldhome, {|s| setenv("HOME", s)});
736-
option::may(olduserprofile, {|s| setenv("USERPROFILE", s)});
735+
option::with_option_do(oldhome, {|s| setenv("HOME", s)});
736+
option::with_option_do(olduserprofile,
737+
{|s| setenv("USERPROFILE", s)});
737738
}
738739

739740
// Issue #712

Diff for: src/libcore/result.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ If the result is an error
2020
pure fn get<T: copy, U>(res: result<T, U>) -> T {
2121
alt res {
2222
ok(t) { t }
23-
err(_) {
24-
// FIXME: Serialize the error value
25-
// and include it in the fail message (maybe just note it)
26-
fail "get called on error result";
23+
err(the_err) {
24+
// FIXME: have a run-fail test for this
25+
unchecked{ fail #fmt("get called on error result: %?", the_err); }
2726
}
2827
}
2928
}

Diff for: src/libcore/task.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ fn spawn_raw(opts: task_opts, +f: fn~()) unsafe {
498498
}
499499
};
500500

501-
option::may(opts.notify_chan) {|c|
501+
option::with_option_do(opts.notify_chan) {|c|
502502
// FIXME (1087): Would like to do notification in Rust
503503
rustrt::rust_task_config_notify(new_task, c);
504504
}

Diff for: src/librustsyntax/diagnostic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ fn highlight_lines(cm: codemap::codemap, sp: span,
243243
}
244244

245245
fn print_macro_backtrace(cm: codemap::codemap, sp: span) {
246-
option::may (sp.expn_info) {|ei|
247-
let ss = option::maybe(ei.callie.span, "",
246+
option::with_option_do (sp.expn_info) {|ei|
247+
let ss = option::with_option(ei.callie.span, "",
248248
bind codemap::span_to_str(_, cm));
249249
print_diagnostic(ss, note,
250250
#fmt("in expansion of #%s", ei.callie.name));

Diff for: src/librustsyntax/ext/qquote.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn expand_ast(ecx: ext_ctxt, _sp: span,
135135
-> @ast::expr
136136
{
137137
let mut what = "expr";
138-
option::may(arg) {|arg|
138+
option::with_option_do(arg) {|arg|
139139
let args: [@ast::expr] =
140140
alt arg.node {
141141
ast::expr_vec(elts, _) { elts }

Diff for: src/librustsyntax/parse/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn eval_crate_directives_to_mod(cx: ctx, cdirs: [@ast::crate_directive],
2323
-> (ast::_mod, [ast::attribute]) {
2424
#debug("eval crate prefix: %s", prefix);
2525
#debug("eval crate suffix: %s",
26-
option::from_maybe(suffix, "none"));
26+
option::get_or_default(suffix, "none"));
2727
let (cview_items, citems, cattrs)
2828
= parse_companion_mod(cx, prefix, suffix);
2929
let mut view_items: [@ast::view_item] = [];

Diff for: src/librustsyntax/print/pprust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ fn print_mac(s: ps, m: ast::mac) {
792792
some(@{node: ast::expr_vec(_, _), _}) { }
793793
_ { word(s.s, " "); }
794794
}
795-
option::may(arg, bind print_expr(s, _));
795+
option::with_option_do(arg, bind print_expr(s, _));
796796
// FIXME: extension 'body'
797797
}
798798
ast::mac_embed_type(ty) {

Diff for: src/librustsyntax/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn visit_pat<E>(p: @pat, e: E, v: vt<E>) {
225225
}
226226
pat_ident(path, inner) {
227227
visit_path(path, e, v);
228-
option::may(inner, {|subpat| v.visit_pat(subpat, e, v)});
228+
option::with_option_do(inner, {|subpat| v.visit_pat(subpat, e, v)});
229229
}
230230
pat_lit(ex) { v.visit_expr(ex, e, v); }
231231
pat_range(e1, e2) { v.visit_expr(e1, e, v); v.visit_expr(e2, e, v); }

Diff for: src/rustc/metadata/astencode.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -692,15 +692,15 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
692692

693693
#debug["Encoding side tables for id %d", id];
694694

695-
option::may(tcx.def_map.find(id)) {|def|
695+
option::with_option_do(tcx.def_map.find(id)) {|def|
696696
ebml_w.tag(c::tag_table_def) {||
697697
ebml_w.id(id);
698698
ebml_w.tag(c::tag_table_val) {||
699699
ast::serialize_def(ebml_w, def)
700700
}
701701
}
702702
}
703-
option::may((*tcx.node_types).find(id as uint)) {|ty|
703+
option::with_option_do((*tcx.node_types).find(id as uint)) {|ty|
704704
ebml_w.tag(c::tag_table_node_type) {||
705705
ebml_w.id(id);
706706
ebml_w.tag(c::tag_table_val) {||
@@ -709,7 +709,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
709709
}
710710
}
711711

712-
option::may(tcx.node_type_substs.find(id)) {|tys|
712+
option::with_option_do(tcx.node_type_substs.find(id)) {|tys|
713713
ebml_w.tag(c::tag_table_node_type_subst) {||
714714
ebml_w.id(id);
715715
ebml_w.tag(c::tag_table_val) {||
@@ -718,7 +718,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
718718
}
719719
}
720720

721-
option::may(tcx.freevars.find(id)) {|fv|
721+
option::with_option_do(tcx.freevars.find(id)) {|fv|
722722
ebml_w.tag(c::tag_table_freevars) {||
723723
ebml_w.id(id);
724724
ebml_w.tag(c::tag_table_val) {||
@@ -730,7 +730,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
730730
}
731731

732732
let lid = {crate: ast::local_crate, node: id};
733-
option::may(tcx.tcache.find(lid)) {|tpbt|
733+
option::with_option_do(tcx.tcache.find(lid)) {|tpbt|
734734
ebml_w.tag(c::tag_table_tcache) {||
735735
ebml_w.id(id);
736736
ebml_w.tag(c::tag_table_val) {||
@@ -739,7 +739,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
739739
}
740740
}
741741

742-
option::may(tcx.ty_param_bounds.find(id)) {|pbs|
742+
option::with_option_do(tcx.ty_param_bounds.find(id)) {|pbs|
743743
ebml_w.tag(c::tag_table_param_bounds) {||
744744
ebml_w.id(id);
745745
ebml_w.tag(c::tag_table_val) {||
@@ -753,7 +753,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
753753
// is what we actually use in trans, all modes will have been
754754
// resolved.
755755
//
756-
//option::may(tcx.inferred_modes.find(id)) {|m|
756+
//option::with_option_do(tcx.inferred_modes.find(id)) {|m|
757757
// ebml_w.tag(c::tag_table_inferred_modes) {||
758758
// ebml_w.id(id);
759759
// ebml_w.tag(c::tag_table_val) {||
@@ -762,25 +762,25 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
762762
// }
763763
//}
764764

765-
option::may(ccx.maps.mutbl_map.find(id)) {|_m|
765+
option::with_option_do(ccx.maps.mutbl_map.find(id)) {|_m|
766766
ebml_w.tag(c::tag_table_mutbl) {||
767767
ebml_w.id(id);
768768
}
769769
}
770770

771-
option::may(ccx.maps.copy_map.find(id)) {|_m|
771+
option::with_option_do(ccx.maps.copy_map.find(id)) {|_m|
772772
ebml_w.tag(c::tag_table_copy) {||
773773
ebml_w.id(id);
774774
}
775775
}
776776

777-
option::may(ccx.maps.spill_map.find(id)) {|_m|
777+
option::with_option_do(ccx.maps.spill_map.find(id)) {|_m|
778778
ebml_w.tag(c::tag_table_spill) {||
779779
ebml_w.id(id);
780780
}
781781
}
782782

783-
option::may(ccx.maps.last_uses.find(id)) {|m|
783+
option::with_option_do(ccx.maps.last_uses.find(id)) {|m|
784784
ebml_w.tag(c::tag_table_last_use) {||
785785
ebml_w.id(id);
786786
ebml_w.tag(c::tag_table_val) {||
@@ -792,7 +792,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
792792
// impl_map is not used except when emitting metadata,
793793
// don't need to keep it.
794794

795-
option::may(ccx.maps.method_map.find(id)) {|mo|
795+
option::with_option_do(ccx.maps.method_map.find(id)) {|mo|
796796
ebml_w.tag(c::tag_table_method_map) {||
797797
ebml_w.id(id);
798798
ebml_w.tag(c::tag_table_val) {||
@@ -801,7 +801,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
801801
}
802802
}
803803

804-
option::may(ccx.maps.vtable_map.find(id)) {|dr|
804+
option::with_option_do(ccx.maps.vtable_map.find(id)) {|dr|
805805
ebml_w.tag(c::tag_table_vtable_map) {||
806806
ebml_w.id(id);
807807
ebml_w.tag(c::tag_table_val) {||

Diff for: src/rustc/metadata/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn get_dep_hashes(cstore: cstore) -> [str] {
163163

164164
fn get_path(cstore: cstore, d: ast::def_id) -> [str] {
165165
// let f = bind str::split_str(_, "::");
166-
option::maybe(p(cstore).mod_path_map.find(d), [],
166+
option::with_option(p(cstore).mod_path_map.find(d), [],
167167
{|ds| str::split_str(ds, "::")})
168168
}
169169
// Local Variables:

Diff for: src/rustc/metadata/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn class_member_id(d: ebml::doc, cdata: cmd) -> ast::def_id {
120120

121121
fn field_mutability(d: ebml::doc) -> ast::class_mutability {
122122
// Use maybe_get_doc in case it's a method
123-
option::maybe(ebml::maybe_get_doc(d, tag_class_mut),
123+
option::with_option(ebml::maybe_get_doc(d, tag_class_mut),
124124
ast::class_immutable,
125125
{|d|
126126
alt ebml::doc_as_u8(d) as char {

Diff for: src/rustc/middle/check_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn check_item(it: @item, &&_is_const: bool, v: visit::vt<bool>) {
1919
item_const(_, ex) { v.visit_expr(ex, true, v); }
2020
item_enum(vs, _) {
2121
for var in vs {
22-
option::may(var.node.disr_expr) {|ex|
22+
option::with_option_do(var.node.disr_expr) {|ex|
2323
v.visit_expr(ex, true, v);
2424
}
2525
}

Diff for: src/rustc/middle/kind.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
183183
}
184184
}
185185
expr_path(_) | expr_field(_, _, _) {
186-
option::may(cx.tcx.node_type_substs.find(e.id)) {|ts|
186+
option::with_option_do(cx.tcx.node_type_substs.find(e.id)) {|ts|
187187
let bounds = alt check e.node {
188188
expr_path(_) {
189189
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(e.id));
@@ -235,7 +235,7 @@ fn check_stmt(stmt: @stmt, cx: ctx, v: visit::vt<ctx>) {
235235
fn check_ty(aty: @ty, cx: ctx, v: visit::vt<ctx>) {
236236
alt aty.node {
237237
ty_path(_, id) {
238-
option::may(cx.tcx.node_type_substs.find(id)) {|ts|
238+
option::with_option_do(cx.tcx.node_type_substs.find(id)) {|ts|
239239
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(id));
240240
let bounds = ty::lookup_item_type(cx.tcx, did).bounds;
241241
vec::iter2(ts, *bounds) {|ty, bound|

Diff for: src/rustc/middle/last_use.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn visit_expr(ex: @expr, cx: ctx, v: visit::vt<ctx>) {
137137
clear_in_current(cx, root_id, false);
138138
}
139139
_ {
140-
option::may(def_is_owned_local(cx, my_def)) {|nid|
140+
option::with_option_do(def_is_owned_local(cx, my_def)) {|nid|
141141
clear_in_current(cx, nid, false);
142142
cx.current += [{def: nid,
143143
uses: cons(var_use(ex.id), @nil)}];
@@ -192,7 +192,7 @@ fn visit_expr(ex: @expr, cx: ctx, v: visit::vt<ctx>) {
192192
alt ty::arg_mode(cx.tcx, arg_t) {
193193
by_ref | by_val | by_mutbl_ref {
194194
let def = cx.def_map.get(arg.id);
195-
option::may(def_is_owned_local(cx, def)) {|id|
195+
option::with_option_do(def_is_owned_local(cx, def)) {|id|
196196
clear_in_current(cx, id, false);
197197
cx.spill_map.insert(id, ());
198198
}
@@ -247,7 +247,7 @@ fn visit_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,
247247
alt cx.tcx.freevars.find(id) {
248248
some(vars) {
249249
for v in *vars {
250-
option::may(def_is_owned_local(cx, v.def)) {|nid|
250+
option::with_option_do(def_is_owned_local(cx, v.def)) {|nid|
251251
clear_in_current(cx, nid, false);
252252
cx.current += [{def: nid,
253253
uses: cons(close_over(id), @nil)}];

Diff for: src/rustc/middle/resolve.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@ fn check_exports(e: @env) {
19361936

19371937

19381938
fn maybe_add_reexport(e: @env, export_id: node_id, def: option<def>) {
1939-
option::may(def) {|def|
1939+
option::with_option_do(def) {|def|
19401940
add_export(e, export_id, def_id_of_def(def), true);
19411941
}
19421942
}
@@ -2118,7 +2118,7 @@ fn find_impls_in_view_item(e: env, vi: @ast::view_item,
21182118
ast::view_path_simple(name, pt, id) {
21192119
let mut found = [];
21202120
if vec::len(*pt) == 1u {
2121-
option::may(sc) {|sc|
2121+
option::with_option_do(sc) {|sc|
21222122
list::iter(sc) {|level|
21232123
if vec::len(found) == 0u {
21242124
for imp in *level {

Diff for: src/rustc/middle/trans/base.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ fn trans_if(cx: block, cond: @ast::expr, thn: ast::blk,
16571657
let then_cx = scope_block(bcx, "then");
16581658
then_cx.block_span = some(thn.span);
16591659
let else_cx = scope_block(bcx, "else");
1660-
option::may(els) {|e| else_cx.block_span = some(e.span); }
1660+
option::with_option_do(els) {|e| else_cx.block_span = some(e.span); }
16611661
CondBr(bcx, cond_val, then_cx.llbb, else_cx.llbb);
16621662
let then_bcx = trans_block(then_cx, thn, then_dest);
16631663
let then_bcx = trans_block_cleanups(then_bcx, then_cx);
@@ -2744,7 +2744,7 @@ fn trans_call_inner(in_cx: block, fn_expr_ty: ty::t, ret_ty: ty::t,
27442744
Unreachable(bcx);
27452745
} else if ret_in_loop {
27462746
bcx = with_cond(bcx, Load(bcx, option::get(ret_flag))) {|bcx|
2747-
option::may(bcx.fcx.loop_ret) {|lret|
2747+
option::with_option_do(bcx.fcx.loop_ret) {|lret|
27482748
Store(bcx, C_bool(true), lret.flagptr);
27492749
Store(bcx, C_bool(false), bcx.fcx.llretptr);
27502750
}
@@ -3816,7 +3816,7 @@ fn alloc_local(cx: block, local: @ast::local) -> block {
38163816
}
38173817
let val = alloc_ty(cx, t);
38183818
if cx.sess().opts.debuginfo {
3819-
option::may(simple_name) {|name|
3819+
option::with_option_do(simple_name) {|name|
38203820
str::as_c_str(name, {|buf|
38213821
llvm::LLVMSetValueName(val, buf)
38223822
});

Diff for: src/rustc/middle/trans/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fn build_closure(bcx0: block,
325325
}
326326
}
327327
}
328-
option::may(include_ret_handle) {|flagptr|
328+
option::with_option_do(include_ret_handle) {|flagptr|
329329
let our_ret = alt bcx.fcx.loop_ret {
330330
some({retptr, _}) { retptr }
331331
none { bcx.fcx.llretptr }

0 commit comments

Comments
 (0)