Skip to content

Commit

Permalink
auto merge of #8294 : erickt/rust/map-move, r=bblum
Browse files Browse the repository at this point in the history
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()`.
  • Loading branch information
bors committed Aug 7, 2013
2 parents cdba212 + 19e17f5 commit 98ec79c
Show file tree
Hide file tree
Showing 58 changed files with 243 additions and 211 deletions.
14 changes: 7 additions & 7 deletions src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ pub fn parse_config(args: ~[~str]) -> config {
compile_lib_path: getopts::opt_str(matches, "compile-lib-path"),
run_lib_path: getopts::opt_str(matches, "run-lib-path"),
rustc_path: opt_path(matches, "rustc-path"),
clang_path: getopts::opt_maybe_str(matches, "clang-path").map(|s| Path(*s)),
llvm_bin_path: getopts::opt_maybe_str(matches, "llvm-bin-path").map(|s| Path(*s)),
clang_path: getopts::opt_maybe_str(matches, "clang-path").map_move(|s| Path(s)),
llvm_bin_path: getopts::opt_maybe_str(matches, "llvm-bin-path").map_move(|s| Path(s)),
src_base: opt_path(matches, "src-base"),
build_base: opt_path(matches, "build-base"),
aux_base: opt_path(matches, "aux-base"),
Expand All @@ -123,14 +123,14 @@ pub fn parse_config(args: ~[~str]) -> config {
} else {
None
},
logfile: getopts::opt_maybe_str(matches, "logfile").map(|s| Path(*s)),
save_metrics: getopts::opt_maybe_str(matches, "save-metrics").map(|s| Path(*s)),
logfile: getopts::opt_maybe_str(matches, "logfile").map_move(|s| Path(s)),
save_metrics: getopts::opt_maybe_str(matches, "save-metrics").map_move(|s| Path(s)),
ratchet_metrics:
getopts::opt_maybe_str(matches, "ratchet-metrics").map(|s| Path(*s)),
getopts::opt_maybe_str(matches, "ratchet-metrics").map_move(|s| Path(s)),
ratchet_noise_percent:
getopts::opt_maybe_str(matches,
"ratchet-noise-percent").map(|s|
f64::from_str(*s).unwrap()),
"ratchet-noise-percent").map_move(|s|
f64::from_str(s).unwrap()),
runtool: getopts::opt_maybe_str(matches, "runtool"),
rustcflags: getopts::opt_maybe_str(matches, "rustcflags"),
jit: getopts::opt_present(matches, "jit"),
Expand Down
5 changes: 2 additions & 3 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
round += 1;
}

let mut expected =
match props.pp_exact {
Some(ref file) => {
let mut expected = match props.pp_exact {
Some(ref file) => {
let filepath = testfile.dir_path().push_rel(file);
io::read_whole_file_str(&filepath).unwrap()
}
Expand Down
20 changes: 10 additions & 10 deletions src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<T> DList<T> {
/// Remove the first Node and return it, or None if the list is empty
#[inline]
fn pop_front_node(&mut self) -> Option<~Node<T>> {
do self.list_head.take().map_consume |mut front_node| {
do self.list_head.take().map_move |mut front_node| {
self.length -= 1;
match front_node.next.take() {
Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
Expand All @@ -190,7 +190,7 @@ impl<T> DList<T> {
/// Remove the last Node and return it, or None if the list is empty
#[inline]
fn pop_back_node(&mut self) -> Option<~Node<T>> {
do self.list_tail.resolve().map_consume_default(None) |tail| {
do self.list_tail.resolve().map_move_default(None) |tail| {
self.length -= 1;
self.list_tail = tail.prev;
match tail.prev.resolve() {
Expand Down Expand Up @@ -237,7 +237,7 @@ impl<T> Deque<T> for DList<T> {
///
/// O(1)
fn pop_front(&mut self) -> Option<T> {
self.pop_front_node().map_consume(|~Node{value, _}| value)
self.pop_front_node().map_move(|~Node{value, _}| value)
}

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

Expand All @@ -267,7 +267,7 @@ impl<T> DList<T> {
/// If the list is empty, do nothing.
#[inline]
pub fn rotate_forward(&mut self) {
do self.pop_back_node().map_consume |tail| {
do self.pop_back_node().map_move |tail| {
self.push_front_node(tail)
};
}
Expand All @@ -277,7 +277,7 @@ impl<T> DList<T> {
/// If the list is empty, do nothing.
#[inline]
pub fn rotate_backward(&mut self) {
do self.pop_front_node().map_consume |head| {
do self.pop_front_node().map_move |head| {
self.push_back_node(head)
};
}
Expand Down Expand Up @@ -463,7 +463,7 @@ impl<'self, A> DoubleEndedIterator<&'self A> for DListIterator<'self, A> {
if self.nelem == 0 {
return None;
}
do self.tail.resolve().map_consume |prev| {
do self.tail.resolve().map_move |prev| {
self.nelem -= 1;
self.tail = prev.prev;
&prev.value
Expand All @@ -477,7 +477,7 @@ impl<'self, A> Iterator<&'self mut A> for MutDListIterator<'self, A> {
if self.nelem == 0 {
return None;
}
do self.head.resolve().map_consume |next| {
do self.head.resolve().map_move |next| {
self.nelem -= 1;
self.head = match next.next {
Some(ref mut node) => Rawlink::some(&mut **node),
Expand All @@ -499,7 +499,7 @@ impl<'self, A> DoubleEndedIterator<&'self mut A> for MutDListIterator<'self, A>
if self.nelem == 0 {
return None;
}
do self.tail.resolve().map_consume |prev| {
do self.tail.resolve().map_move |prev| {
self.nelem -= 1;
self.tail = prev.prev;
&mut prev.value
Expand Down Expand Up @@ -553,7 +553,7 @@ impl<'self, A> ListInsertion<A> for MutDListIterator<'self, A> {
if self.nelem == 0 {
return None
}
self.head.resolve().map_consume(|head| &mut head.value)
self.head.resolve().map_move(|head| &mut head.value)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/libextra/num/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ impl BigUint {

pub fn new(v: ~[BigDigit]) -> BigUint {
// omit trailing zeros
let new_len = v.rposition(|n| *n != 0).map_default(0, |p| *p + 1);
let new_len = v.rposition(|n| *n != 0).map_move_default(0, |p| p + 1);

if new_len == v.len() { return BigUint { data: v }; }
let mut v = v;
Expand Down Expand Up @@ -1145,7 +1145,7 @@ impl BigInt {
start = 1;
}
return BigUint::parse_bytes(buf.slice(start, buf.len()), radix)
.map_consume(|bu| BigInt::from_biguint(sign, bu));
.map_move(|bu| BigInt::from_biguint(sign, bu));
}
pub fn to_uint(&self) -> uint {
Expand Down Expand Up @@ -2028,7 +2028,7 @@ mod bigint_tests {
#[test]
fn test_from_str_radix() {
fn check(s: &str, ans: Option<int>) {
let ans = ans.map(|&n| IntConvertible::from_int::<BigInt>(n));
let ans = ans.map_move(|n| IntConvertible::from_int::<BigInt>(n));
assert_eq!(FromStrRadix::from_str_radix(s, 10), ans);
}
check("10", Some(10));
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl<V> SmallIntMap<V> {
{
let values = replace(&mut self.v, ~[]);
values.consume_iter().enumerate().filter_map(|(i, v)| {
v.map_consume(|v| (i, v))
v.map_move(|v| (i, v))
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Terminal {
let inf = ti.unwrap();
let nc = if inf.strings.find_equiv(&("setaf")).is_some()
&& inf.strings.find_equiv(&("setab")).is_some() {
inf.numbers.find_equiv(&("colors")).map_consume_default(0, |&n| n)
inf.numbers.find_equiv(&("colors")).map_move_default(0, |&n| n)
} else { 0 };

return Ok(Terminal {out: out, ti: inf, num_colors: nc});
Expand Down Expand Up @@ -220,7 +220,7 @@ impl Terminal {
cap = self.ti.strings.find_equiv(&("op"));
}
}
let s = do cap.map_consume_default(Err(~"can't find terminfo capability `sgr0`")) |op| {
let s = do cap.map_move_default(Err(~"can't find terminfo capability `sgr0`")) |op| {
expand(*op, [], &mut Variables::new())
};
if s.is_ok() {
Expand Down
8 changes: 4 additions & 4 deletions src/libextra/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,20 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
let run_ignored = getopts::opt_present(&matches, "ignored");

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

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

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

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

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

let test_opts = TestOpts {
filter: filter,
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl<'self, T> Iterator<&'self T> for TreeSetIterator<'self, T> {
/// Advance the iterator to the next node (in order). If there are no more nodes, return `None`.
#[inline]
fn next(&mut self) -> Option<&'self T> {
do self.iter.next().map |&(value, _)| { value }
do self.iter.next().map_move |(value, _)| { value }
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libextra/workcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn digest<T:Encodable<json::Encoder>>(t: &T) -> ~str {
fn digest_file(path: &Path) -> ~str {
let mut sha = ~Sha1::new();
let s = io::read_whole_file_str(path);
(*sha).input_str(*s.get_ref());
(*sha).input_str(s.unwrap());
(*sha).result_str()
}
Expand Down Expand Up @@ -378,7 +378,7 @@ fn test() {
let pth = Path("foo.c");
{
let r = io::file_writer(&pth, [io::Create]);
r.get_ref().write_str("int main() { return 0; }");
r.unwrap().write_str("int main() { return 0; }");
}

let cx = Context::new(RWArc::new(Database::new(Path("db.json"))),
Expand Down
2 changes: 1 addition & 1 deletion src/librust/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn rustc_help() {
fn find_cmd(command_string: &str) -> Option<Command> {
do COMMANDS.iter().find_ |command| {
command.cmd == command_string
}.map_consume(|x| *x)
}.map_move(|x| *x)
}

fn cmd_help(args: &[~str]) -> ValidUsage {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,7 @@ pub fn build_session_options(binary: @str,
} else if opt_present(matches, "emit-llvm") {
link::output_type_bitcode
} else { link::output_type_exe };
let sysroot_opt = getopts::opt_maybe_str(matches, "sysroot");
let sysroot_opt = sysroot_opt.map(|m| @Path(*m));
let sysroot_opt = getopts::opt_maybe_str(matches, "sysroot").map_move(|m| @Path(m));
let target_opt = getopts::opt_maybe_str(matches, "target");
let target_feature_opt = getopts::opt_maybe_str(matches, "target-feature");
let save_temps = getopts::opt_present(matches, "save-temps");
Expand Down
10 changes: 7 additions & 3 deletions src/librustc/front/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ fn fold_mod(cx: @Context, m: &ast::_mod, fld: @fold::ast_fold) -> ast::_mod {
filter_item(cx, *a).chain(|x| fld.fold_item(x))
}.collect();
let filtered_view_items = do m.view_items.iter().filter_map |a| {
filter_view_item(cx, a).map(|&x| fld.fold_view_item(x))
do filter_view_item(cx, a).map_move |x| {
fld.fold_view_item(x)
}
}.collect();
ast::_mod {
view_items: filtered_view_items,
Expand All @@ -83,7 +85,9 @@ fn fold_foreign_mod(
) -> ast::foreign_mod {
let filtered_items = nm.items.iter().filter_map(|a| filter_foreign_item(cx, *a)).collect();
let filtered_view_items = do nm.view_items.iter().filter_map |a| {
filter_view_item(cx, a).map(|&x| fld.fold_view_item(x))
do filter_view_item(cx, a).map_move |x| {
fld.fold_view_item(x)
}
}.collect();
ast::foreign_mod {
sort: nm.sort,
Expand Down Expand Up @@ -138,7 +142,7 @@ fn fold_block(
filter_stmt(cx, *a).chain(|stmt| fld.fold_stmt(stmt))
}.collect();
let filtered_view_items = do b.view_items.iter().filter_map |a| {
filter_view_item(cx, a).map(|&x| fld.fold_view_item(x))
filter_view_item(cx, a).map(|x| fld.fold_view_item(*x))
}.collect();
ast::Block {
view_items: filtered_view_items,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,7 @@ impl TypeNames {
}

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

// We have a depth count, because we seem to make infinite types.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn add_extern_mod_stmt_cnum(cstore: &mut CStore,
pub fn find_extern_mod_stmt_cnum(cstore: &CStore,
emod_id: ast::NodeId)
-> Option<ast::CrateNum> {
cstore.extern_mod_crate_map.find(&emod_id).map_consume(|x| *x)
cstore.extern_mod_crate_map.find(&emod_id).map_move(|x| *x)
}

#[deriving(Clone)]
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ fn item_def_id(d: ebml::Doc, cdata: cmd) -> ast::def_id {
}

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

Expand Down Expand Up @@ -265,10 +265,10 @@ fn item_ty_param_defs(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd,
}

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

fn item_ty_param_count(item: ebml::Doc) -> uint {
Expand Down Expand Up @@ -415,7 +415,7 @@ pub fn get_impl_trait(cdata: cmd,
tcx: ty::ctxt) -> Option<@ty::TraitRef>
{
let item_doc = lookup_item(id, cdata.data);
do reader::maybe_get_doc(item_doc, tag_item_trait_ref).map |&tp| {
do reader::maybe_get_doc(item_doc, tag_item_trait_ref).map_move |tp| {
@doc_trait_ref(tp, tcx, cdata)
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/librustc/middle/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,15 @@ pub fn opt_loan_path(cmt: mc::cmt) -> Option<@LoanPath> {
}

mc::cat_deref(cmt_base, _, _) => {
opt_loan_path(cmt_base).map(
|&lp| @LpExtend(lp, cmt.mutbl, LpDeref))
do opt_loan_path(cmt_base).map_move |lp| {
@LpExtend(lp, cmt.mutbl, LpDeref)
}
}

mc::cat_interior(cmt_base, ik) => {
opt_loan_path(cmt_base).map(
|&lp| @LpExtend(lp, cmt.mutbl, LpInterior(ik)))
do opt_loan_path(cmt_base).map_move |lp| {
@LpExtend(lp, cmt.mutbl, LpInterior(ik))
}
}

mc::cat_downcast(cmt_base) |
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,9 @@ pub fn compare_lit_exprs(tcx: middle::ty::ctxt, a: &expr, b: &expr) -> Option<in
}

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

pub fn lit_eq(a: &lit, b: &lit) -> Option<bool> {
compare_const_vals(&lit_to_const(a), &lit_to_const(b)).map(|&val| val == 0)
compare_const_vals(&lit_to_const(a), &lit_to_const(b)).map_move(|val| val == 0)
}
2 changes: 1 addition & 1 deletion src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl<'self> LanguageItemCollector<'self> {
return; // Didn't match.
}

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

Expand Down
Loading

0 comments on commit 98ec79c

Please sign in to comment.