Skip to content

Commit 98ec79c

Browse files
committed
auto merge of #8294 : erickt/rust/map-move, r=bblum
According to #7887, we've decided to use the syntax of `fn map<U>(f: &fn(&T) -> U) -> U`, which passes a reference to the closure, and to `fn map_move<U>(f: &fn(T) -> U) -> U` which moves the value into the closure. This PR adds these `.map_move()` functions to `Option` and `Result`. In addition, it has these other minor features: * Replaces a couple uses of `option.get()`, `result.get()`, and `result.get_err()` with `option.unwrap()`, `result.unwrap()`, and `result.unwrap_err()`. (See #8268 and #8288 for a more thorough adaptation of this functionality. * Removes `option.take_map()` and `option.take_map_default()`. These two functions can be easily written as `.take().map_move(...)`. * Adds a better error message to `result.unwrap()` and `result.unwrap_err()`.
2 parents cdba212 + 19e17f5 commit 98ec79c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+243
-211
lines changed

src/compiletest/compiletest.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ pub fn parse_config(args: ~[~str]) -> config {
109109
compile_lib_path: getopts::opt_str(matches, "compile-lib-path"),
110110
run_lib_path: getopts::opt_str(matches, "run-lib-path"),
111111
rustc_path: opt_path(matches, "rustc-path"),
112-
clang_path: getopts::opt_maybe_str(matches, "clang-path").map(|s| Path(*s)),
113-
llvm_bin_path: getopts::opt_maybe_str(matches, "llvm-bin-path").map(|s| Path(*s)),
112+
clang_path: getopts::opt_maybe_str(matches, "clang-path").map_move(|s| Path(s)),
113+
llvm_bin_path: getopts::opt_maybe_str(matches, "llvm-bin-path").map_move(|s| Path(s)),
114114
src_base: opt_path(matches, "src-base"),
115115
build_base: opt_path(matches, "build-base"),
116116
aux_base: opt_path(matches, "aux-base"),
@@ -123,14 +123,14 @@ pub fn parse_config(args: ~[~str]) -> config {
123123
} else {
124124
None
125125
},
126-
logfile: getopts::opt_maybe_str(matches, "logfile").map(|s| Path(*s)),
127-
save_metrics: getopts::opt_maybe_str(matches, "save-metrics").map(|s| Path(*s)),
126+
logfile: getopts::opt_maybe_str(matches, "logfile").map_move(|s| Path(s)),
127+
save_metrics: getopts::opt_maybe_str(matches, "save-metrics").map_move(|s| Path(s)),
128128
ratchet_metrics:
129-
getopts::opt_maybe_str(matches, "ratchet-metrics").map(|s| Path(*s)),
129+
getopts::opt_maybe_str(matches, "ratchet-metrics").map_move(|s| Path(s)),
130130
ratchet_noise_percent:
131131
getopts::opt_maybe_str(matches,
132-
"ratchet-noise-percent").map(|s|
133-
f64::from_str(*s).unwrap()),
132+
"ratchet-noise-percent").map_move(|s|
133+
f64::from_str(s).unwrap()),
134134
runtool: getopts::opt_maybe_str(matches, "runtool"),
135135
rustcflags: getopts::opt_maybe_str(matches, "rustcflags"),
136136
jit: getopts::opt_present(matches, "jit"),

src/compiletest/runtest.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,8 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
162162
round += 1;
163163
}
164164

165-
let mut expected =
166-
match props.pp_exact {
167-
Some(ref file) => {
165+
let mut expected = match props.pp_exact {
166+
Some(ref file) => {
168167
let filepath = testfile.dir_path().push_rel(file);
169168
io::read_whole_file_str(&filepath).unwrap()
170169
}

src/libextra/dlist.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<T> DList<T> {
164164
/// Remove the first Node and return it, or None if the list is empty
165165
#[inline]
166166
fn pop_front_node(&mut self) -> Option<~Node<T>> {
167-
do self.list_head.take().map_consume |mut front_node| {
167+
do self.list_head.take().map_move |mut front_node| {
168168
self.length -= 1;
169169
match front_node.next.take() {
170170
Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
@@ -190,7 +190,7 @@ impl<T> DList<T> {
190190
/// Remove the last Node and return it, or None if the list is empty
191191
#[inline]
192192
fn pop_back_node(&mut self) -> Option<~Node<T>> {
193-
do self.list_tail.resolve().map_consume_default(None) |tail| {
193+
do self.list_tail.resolve().map_move_default(None) |tail| {
194194
self.length -= 1;
195195
self.list_tail = tail.prev;
196196
match tail.prev.resolve() {
@@ -237,7 +237,7 @@ impl<T> Deque<T> for DList<T> {
237237
///
238238
/// O(1)
239239
fn pop_front(&mut self) -> Option<T> {
240-
self.pop_front_node().map_consume(|~Node{value, _}| value)
240+
self.pop_front_node().map_move(|~Node{value, _}| value)
241241
}
242242

243243
/// Add an element last in the list
@@ -251,7 +251,7 @@ impl<T> Deque<T> for DList<T> {
251251
///
252252
/// O(1)
253253
fn pop_back(&mut self) -> Option<T> {
254-
self.pop_back_node().map_consume(|~Node{value, _}| value)
254+
self.pop_back_node().map_move(|~Node{value, _}| value)
255255
}
256256
}
257257

@@ -267,7 +267,7 @@ impl<T> DList<T> {
267267
/// If the list is empty, do nothing.
268268
#[inline]
269269
pub fn rotate_forward(&mut self) {
270-
do self.pop_back_node().map_consume |tail| {
270+
do self.pop_back_node().map_move |tail| {
271271
self.push_front_node(tail)
272272
};
273273
}
@@ -277,7 +277,7 @@ impl<T> DList<T> {
277277
/// If the list is empty, do nothing.
278278
#[inline]
279279
pub fn rotate_backward(&mut self) {
280-
do self.pop_front_node().map_consume |head| {
280+
do self.pop_front_node().map_move |head| {
281281
self.push_back_node(head)
282282
};
283283
}
@@ -463,7 +463,7 @@ impl<'self, A> DoubleEndedIterator<&'self A> for DListIterator<'self, A> {
463463
if self.nelem == 0 {
464464
return None;
465465
}
466-
do self.tail.resolve().map_consume |prev| {
466+
do self.tail.resolve().map_move |prev| {
467467
self.nelem -= 1;
468468
self.tail = prev.prev;
469469
&prev.value
@@ -477,7 +477,7 @@ impl<'self, A> Iterator<&'self mut A> for MutDListIterator<'self, A> {
477477
if self.nelem == 0 {
478478
return None;
479479
}
480-
do self.head.resolve().map_consume |next| {
480+
do self.head.resolve().map_move |next| {
481481
self.nelem -= 1;
482482
self.head = match next.next {
483483
Some(ref mut node) => Rawlink::some(&mut **node),
@@ -499,7 +499,7 @@ impl<'self, A> DoubleEndedIterator<&'self mut A> for MutDListIterator<'self, A>
499499
if self.nelem == 0 {
500500
return None;
501501
}
502-
do self.tail.resolve().map_consume |prev| {
502+
do self.tail.resolve().map_move |prev| {
503503
self.nelem -= 1;
504504
self.tail = prev.prev;
505505
&mut prev.value
@@ -553,7 +553,7 @@ impl<'self, A> ListInsertion<A> for MutDListIterator<'self, A> {
553553
if self.nelem == 0 {
554554
return None
555555
}
556-
self.head.resolve().map_consume(|head| &mut head.value)
556+
self.head.resolve().map_move(|head| &mut head.value)
557557
}
558558
}
559559

src/libextra/num/bigint.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ impl BigUint {
548548
549549
pub fn new(v: ~[BigDigit]) -> BigUint {
550550
// omit trailing zeros
551-
let new_len = v.rposition(|n| *n != 0).map_default(0, |p| *p + 1);
551+
let new_len = v.rposition(|n| *n != 0).map_move_default(0, |p| p + 1);
552552

553553
if new_len == v.len() { return BigUint { data: v }; }
554554
let mut v = v;
@@ -1145,7 +1145,7 @@ impl BigInt {
11451145
start = 1;
11461146
}
11471147
return BigUint::parse_bytes(buf.slice(start, buf.len()), radix)
1148-
.map_consume(|bu| BigInt::from_biguint(sign, bu));
1148+
.map_move(|bu| BigInt::from_biguint(sign, bu));
11491149
}
11501150
11511151
pub fn to_uint(&self) -> uint {
@@ -2028,7 +2028,7 @@ mod bigint_tests {
20282028
#[test]
20292029
fn test_from_str_radix() {
20302030
fn check(s: &str, ans: Option<int>) {
2031-
let ans = ans.map(|&n| IntConvertible::from_int::<BigInt>(n));
2031+
let ans = ans.map_move(|n| IntConvertible::from_int::<BigInt>(n));
20322032
assert_eq!(FromStrRadix::from_str_radix(s, 10), ans);
20332033
}
20342034
check("10", Some(10));

src/libextra/smallintmap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<V> SmallIntMap<V> {
203203
{
204204
let values = replace(&mut self.v, ~[]);
205205
values.consume_iter().enumerate().filter_map(|(i, v)| {
206-
v.map_consume(|v| (i, v))
206+
v.map_move(|v| (i, v))
207207
})
208208
}
209209
}

src/libextra/term.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl Terminal {
127127
let inf = ti.unwrap();
128128
let nc = if inf.strings.find_equiv(&("setaf")).is_some()
129129
&& inf.strings.find_equiv(&("setab")).is_some() {
130-
inf.numbers.find_equiv(&("colors")).map_consume_default(0, |&n| n)
130+
inf.numbers.find_equiv(&("colors")).map_move_default(0, |&n| n)
131131
} else { 0 };
132132

133133
return Ok(Terminal {out: out, ti: inf, num_colors: nc});
@@ -220,7 +220,7 @@ impl Terminal {
220220
cap = self.ti.strings.find_equiv(&("op"));
221221
}
222222
}
223-
let s = do cap.map_consume_default(Err(~"can't find terminfo capability `sgr0`")) |op| {
223+
let s = do cap.map_move_default(Err(~"can't find terminfo capability `sgr0`")) |op| {
224224
expand(*op, [], &mut Variables::new())
225225
};
226226
if s.is_ok() {

src/libextra/test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,20 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
238238
let run_ignored = getopts::opt_present(&matches, "ignored");
239239

240240
let logfile = getopts::opt_maybe_str(&matches, "logfile");
241-
let logfile = logfile.map(|s| Path(*s));
241+
let logfile = logfile.map_move(|s| Path(s));
242242

243243
let run_benchmarks = getopts::opt_present(&matches, "bench");
244244
let run_tests = ! run_benchmarks ||
245245
getopts::opt_present(&matches, "test");
246246

247247
let ratchet_metrics = getopts::opt_maybe_str(&matches, "ratchet-metrics");
248-
let ratchet_metrics = ratchet_metrics.map(|s| Path(*s));
248+
let ratchet_metrics = ratchet_metrics.map_move(|s| Path(s));
249249

250250
let ratchet_noise_percent = getopts::opt_maybe_str(&matches, "ratchet-noise-percent");
251-
let ratchet_noise_percent = ratchet_noise_percent.map(|s| f64::from_str(*s).unwrap());
251+
let ratchet_noise_percent = ratchet_noise_percent.map_move(|s| f64::from_str(s).unwrap());
252252

253253
let save_metrics = getopts::opt_maybe_str(&matches, "save-metrics");
254-
let save_metrics = save_metrics.map(|s| Path(*s));
254+
let save_metrics = save_metrics.map_move(|s| Path(s));
255255

256256
let test_opts = TestOpts {
257257
filter: filter,

src/libextra/treemap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ impl<'self, T> Iterator<&'self T> for TreeSetIterator<'self, T> {
394394
/// Advance the iterator to the next node (in order). If there are no more nodes, return `None`.
395395
#[inline]
396396
fn next(&mut self) -> Option<&'self T> {
397-
do self.iter.next().map |&(value, _)| { value }
397+
do self.iter.next().map_move |(value, _)| { value }
398398
}
399399
}
400400

src/libextra/workcache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn digest<T:Encodable<json::Encoder>>(t: &T) -> ~str {
221221
fn digest_file(path: &Path) -> ~str {
222222
let mut sha = ~Sha1::new();
223223
let s = io::read_whole_file_str(path);
224-
(*sha).input_str(*s.get_ref());
224+
(*sha).input_str(s.unwrap());
225225
(*sha).result_str()
226226
}
227227
@@ -378,7 +378,7 @@ fn test() {
378378
let pth = Path("foo.c");
379379
{
380380
let r = io::file_writer(&pth, [io::Create]);
381-
r.get_ref().write_str("int main() { return 0; }");
381+
r.unwrap().write_str("int main() { return 0; }");
382382
}
383383

384384
let cx = Context::new(RWArc::new(Database::new(Path("db.json"))),

src/librust/rust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn rustc_help() {
130130
fn find_cmd(command_string: &str) -> Option<Command> {
131131
do COMMANDS.iter().find_ |command| {
132132
command.cmd == command_string
133-
}.map_consume(|x| *x)
133+
}.map_move(|x| *x)
134134
}
135135

136136
fn cmd_help(args: &[~str]) -> ValidUsage {

src/librustc/driver/driver.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,7 @@ pub fn build_session_options(binary: @str,
669669
} else if opt_present(matches, "emit-llvm") {
670670
link::output_type_bitcode
671671
} else { link::output_type_exe };
672-
let sysroot_opt = getopts::opt_maybe_str(matches, "sysroot");
673-
let sysroot_opt = sysroot_opt.map(|m| @Path(*m));
672+
let sysroot_opt = getopts::opt_maybe_str(matches, "sysroot").map_move(|m| @Path(m));
674673
let target_opt = getopts::opt_maybe_str(matches, "target");
675674
let target_feature_opt = getopts::opt_maybe_str(matches, "target-feature");
676675
let save_temps = getopts::opt_present(matches, "save-temps");

src/librustc/front/config.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ fn fold_mod(cx: @Context, m: &ast::_mod, fld: @fold::ast_fold) -> ast::_mod {
6161
filter_item(cx, *a).chain(|x| fld.fold_item(x))
6262
}.collect();
6363
let filtered_view_items = do m.view_items.iter().filter_map |a| {
64-
filter_view_item(cx, a).map(|&x| fld.fold_view_item(x))
64+
do filter_view_item(cx, a).map_move |x| {
65+
fld.fold_view_item(x)
66+
}
6567
}.collect();
6668
ast::_mod {
6769
view_items: filtered_view_items,
@@ -83,7 +85,9 @@ fn fold_foreign_mod(
8385
) -> ast::foreign_mod {
8486
let filtered_items = nm.items.iter().filter_map(|a| filter_foreign_item(cx, *a)).collect();
8587
let filtered_view_items = do nm.view_items.iter().filter_map |a| {
86-
filter_view_item(cx, a).map(|&x| fld.fold_view_item(x))
88+
do filter_view_item(cx, a).map_move |x| {
89+
fld.fold_view_item(x)
90+
}
8791
}.collect();
8892
ast::foreign_mod {
8993
sort: nm.sort,
@@ -138,7 +142,7 @@ fn fold_block(
138142
filter_stmt(cx, *a).chain(|stmt| fld.fold_stmt(stmt))
139143
}.collect();
140144
let filtered_view_items = do b.view_items.iter().filter_map |a| {
141-
filter_view_item(cx, a).map(|&x| fld.fold_view_item(x))
145+
filter_view_item(cx, a).map(|x| fld.fold_view_item(*x))
142146
}.collect();
143147
ast::Block {
144148
view_items: filtered_view_items,

src/librustc/lib/llvm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2159,7 +2159,7 @@ impl TypeNames {
21592159
}
21602160

21612161
pub fn find_type(&self, s: &str) -> Option<Type> {
2162-
self.named_types.find_equiv(&s).map_consume(|x| Type::from_ref(*x))
2162+
self.named_types.find_equiv(&s).map_move(|x| Type::from_ref(*x))
21632163
}
21642164

21652165
// We have a depth count, because we seem to make infinite types.

src/librustc/metadata/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn add_extern_mod_stmt_cnum(cstore: &mut CStore,
133133
pub fn find_extern_mod_stmt_cnum(cstore: &CStore,
134134
emod_id: ast::NodeId)
135135
-> Option<ast::CrateNum> {
136-
cstore.extern_mod_crate_map.find(&emod_id).map_consume(|x| *x)
136+
cstore.extern_mod_crate_map.find(&emod_id).map_move(|x| *x)
137137
}
138138

139139
#[deriving(Clone)]

src/librustc/metadata/decoder.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ fn item_def_id(d: ebml::Doc, cdata: cmd) -> ast::def_id {
198198
}
199199

200200
fn get_provided_source(d: ebml::Doc, cdata: cmd) -> Option<ast::def_id> {
201-
do reader::maybe_get_doc(d, tag_item_method_provided_source).map |doc| {
202-
translate_def_id(cdata, reader::with_doc_data(*doc, parse_def_id))
201+
do reader::maybe_get_doc(d, tag_item_method_provided_source).map_move |doc| {
202+
translate_def_id(cdata, reader::with_doc_data(doc, parse_def_id))
203203
}
204204
}
205205

@@ -265,10 +265,10 @@ fn item_ty_param_defs(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd,
265265
}
266266

267267
fn item_ty_region_param(item: ebml::Doc) -> Option<ty::region_variance> {
268-
reader::maybe_get_doc(item, tag_region_param).map(|doc| {
269-
let mut decoder = reader::Decoder(*doc);
268+
do reader::maybe_get_doc(item, tag_region_param).map_move |doc| {
269+
let mut decoder = reader::Decoder(doc);
270270
Decodable::decode(&mut decoder)
271-
})
271+
}
272272
}
273273

274274
fn item_ty_param_count(item: ebml::Doc) -> uint {
@@ -415,7 +415,7 @@ pub fn get_impl_trait(cdata: cmd,
415415
tcx: ty::ctxt) -> Option<@ty::TraitRef>
416416
{
417417
let item_doc = lookup_item(id, cdata.data);
418-
do reader::maybe_get_doc(item_doc, tag_item_trait_ref).map |&tp| {
418+
do reader::maybe_get_doc(item_doc, tag_item_trait_ref).map_move |tp| {
419419
@doc_trait_ref(tp, tcx, cdata)
420420
}
421421
}

src/librustc/middle/borrowck/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,15 @@ pub fn opt_loan_path(cmt: mc::cmt) -> Option<@LoanPath> {
286286
}
287287

288288
mc::cat_deref(cmt_base, _, _) => {
289-
opt_loan_path(cmt_base).map(
290-
|&lp| @LpExtend(lp, cmt.mutbl, LpDeref))
289+
do opt_loan_path(cmt_base).map_move |lp| {
290+
@LpExtend(lp, cmt.mutbl, LpDeref)
291+
}
291292
}
292293

293294
mc::cat_interior(cmt_base, ik) => {
294-
opt_loan_path(cmt_base).map(
295-
|&lp| @LpExtend(lp, cmt.mutbl, LpInterior(ik)))
295+
do opt_loan_path(cmt_base).map_move |lp| {
296+
@LpExtend(lp, cmt.mutbl, LpInterior(ik))
297+
}
296298
}
297299

298300
mc::cat_downcast(cmt_base) |

src/librustc/middle/const_eval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,9 @@ pub fn compare_lit_exprs(tcx: middle::ty::ctxt, a: &expr, b: &expr) -> Option<in
493493
}
494494

495495
pub fn lit_expr_eq(tcx: middle::ty::ctxt, a: &expr, b: &expr) -> Option<bool> {
496-
compare_lit_exprs(tcx, a, b).map(|&val| val == 0)
496+
compare_lit_exprs(tcx, a, b).map_move(|val| val == 0)
497497
}
498498

499499
pub fn lit_eq(a: &lit, b: &lit) -> Option<bool> {
500-
compare_const_vals(&lit_to_const(a), &lit_to_const(b)).map(|&val| val == 0)
500+
compare_const_vals(&lit_to_const(a), &lit_to_const(b)).map_move(|val| val == 0)
501501
}

src/librustc/middle/lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl<'self> LanguageItemCollector<'self> {
393393
return; // Didn't match.
394394
}
395395

396-
let item_index = self.item_refs.find(&value).map(|x| **x);
396+
let item_index = self.item_refs.find(&value).map_move(|x| *x);
397397
// prevent borrow checker from considering ^~~~~~~~~~~
398398
// self to be borrowed (annoying)
399399

0 commit comments

Comments
 (0)