Skip to content

Commit 9218aaa

Browse files
committed
std: add result.map_move, result.map_err_move
1 parent 5c08237 commit 9218aaa

File tree

6 files changed

+66
-20
lines changed

6 files changed

+66
-20
lines changed

src/libextra/num/bigint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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/librustc/middle/ty.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4443,15 +4443,15 @@ pub fn count_traits_and_supertraits(tcx: ctxt,
44434443
}
44444444

44454445
pub fn get_tydesc_ty(tcx: ctxt) -> Result<t, ~str> {
4446-
do tcx.lang_items.require(TyDescStructLangItem).map |tydesc_lang_item| {
4447-
tcx.intrinsic_defs.find_copy(tydesc_lang_item)
4446+
do tcx.lang_items.require(TyDescStructLangItem).map_move |tydesc_lang_item| {
4447+
tcx.intrinsic_defs.find_copy(&tydesc_lang_item)
44484448
.expect("Failed to resolve TyDesc")
44494449
}
44504450
}
44514451

44524452
pub fn get_opaque_ty(tcx: ctxt) -> Result<t, ~str> {
4453-
do tcx.lang_items.require(OpaqueStructLangItem).map |opaque_lang_item| {
4454-
tcx.intrinsic_defs.find_copy(opaque_lang_item)
4453+
do tcx.lang_items.require(OpaqueStructLangItem).map_move |opaque_lang_item| {
4454+
tcx.intrinsic_defs.find_copy(&opaque_lang_item)
44554455
.expect("Failed to resolve Opaque")
44564456
}
44574457
}

src/librustc/middle/typeck/check/vtable.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ fn lookup_vtables_for_param(vcx: &VtableContext,
131131
// ty is the value supplied for the type parameter A...
132132
let mut param_result = ~[];
133133

134-
do ty::each_bound_trait_and_supertraits(
135-
tcx, type_param_bounds.trait_bounds) |trait_ref|
136-
{
134+
do ty::each_bound_trait_and_supertraits(tcx, type_param_bounds.trait_bounds) |trait_ref| {
137135
// ...and here trait_ref is each bound that was declared on A,
138136
// expressed in terms of the type parameters.
139137

src/libstd/local_data.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,24 @@ fn test_tls_multitask() {
110110
set(my_key, @~"parent data");
111111
do task::spawn {
112112
// TLS shouldn't carry over.
113-
assert!(get(my_key, |k| k.map(|&k| *k)).is_none());
113+
assert!(get(my_key, |k| k.map_move(|k| *k)).is_none());
114114
set(my_key, @~"child data");
115-
assert!(*(get(my_key, |k| k.map(|&k| *k)).unwrap()) ==
115+
assert!(*(get(my_key, |k| k.map_move(|k| *k)).unwrap()) ==
116116
~"child data");
117117
// should be cleaned up for us
118118
}
119119
// Must work multiple times
120-
assert!(*(get(my_key, |k| k.map(|&k| *k)).unwrap()) == ~"parent data");
121-
assert!(*(get(my_key, |k| k.map(|&k| *k)).unwrap()) == ~"parent data");
122-
assert!(*(get(my_key, |k| k.map(|&k| *k)).unwrap()) == ~"parent data");
120+
assert!(*(get(my_key, |k| k.map_move(|k| *k)).unwrap()) == ~"parent data");
121+
assert!(*(get(my_key, |k| k.map_move(|k| *k)).unwrap()) == ~"parent data");
122+
assert!(*(get(my_key, |k| k.map_move(|k| *k)).unwrap()) == ~"parent data");
123123
}
124124
125125
#[test]
126126
fn test_tls_overwrite() {
127127
static my_key: Key<@~str> = &Key;
128128
set(my_key, @~"first data");
129129
set(my_key, @~"next data"); // Shouldn't leak.
130-
assert!(*(get(my_key, |k| k.map(|&k| *k)).unwrap()) == ~"next data");
130+
assert!(*(get(my_key, |k| k.map_move(|k| *k)).unwrap()) == ~"next data");
131131
}
132132
133133
#[test]

src/libstd/result.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,40 @@ impl<T, E: ToStr> Result<T, E> {
149149
}
150150
}
151151

152+
/// Call a method based on a previous result
153+
///
154+
/// If `self` is `Ok` then the value is extracted and passed to `op`
155+
/// whereupon `op`s result is wrapped in `Ok` and returned. if `self` is
156+
/// `Err` then it is immediately returned. This function can be used to
157+
/// compose the results of two functions.
158+
///
159+
/// Example:
160+
///
161+
/// let res = do read_file(file).map_move |buf| {
162+
/// parse_bytes(buf)
163+
/// }
164+
#[inline]
165+
pub fn map_move<U>(self, op: &fn(T) -> U) -> Result<U,E> {
166+
match self {
167+
Ok(t) => Ok(op(t)),
168+
Err(e) => Err(e)
169+
}
170+
}
171+
172+
/// Call a method based on a previous result
173+
///
174+
/// If `self` is `Err` then the value is extracted and passed to `op`
175+
/// whereupon `op`s result is wrapped in an `Err` and returned. if `self` is
176+
/// `Ok` then it is immediately returned. This function can be used to pass
177+
/// through a successful result while handling an error.
178+
#[inline]
179+
pub fn map_err_move<F>(self, op: &fn(E) -> F) -> Result<T,F> {
180+
match self {
181+
Ok(t) => Ok(t),
182+
Err(e) => Err(op(e))
183+
}
184+
}
185+
152186
/// Call a method based on a previous result
153187
///
154188
/// If `self` is `Ok` then the value is extracted and passed to `op`
@@ -312,7 +346,9 @@ pub fn iter_vec2<S, T, U: ToStr>(ss: &[S], ts: &[T],
312346
#[cfg(test)]
313347
mod tests {
314348
use super::*;
349+
315350
use either;
351+
use str::OwnedStr;
316352

317353
pub fn op1() -> Result<int, ~str> { Ok(666) }
318354

@@ -359,14 +395,26 @@ mod tests {
359395
360396
#[test]
361397
pub fn test_impl_map() {
362-
assert_eq!(Ok::<~str, ~str>(~"a").map(|_x| ~"b"), Ok(~"b"));
363-
assert_eq!(Err::<~str, ~str>(~"a").map(|_x| ~"b"), Err(~"a"));
398+
assert_eq!(Ok::<~str, ~str>(~"a").map(|x| (~"b").append(*x)), Ok(~"ba"));
399+
assert_eq!(Err::<~str, ~str>(~"a").map(|x| (~"b").append(*x)), Err(~"a"));
364400
}
365401
366402
#[test]
367403
pub fn test_impl_map_err() {
368-
assert_eq!(Ok::<~str, ~str>(~"a").map_err(|_x| ~"b"), Ok(~"a"));
369-
assert_eq!(Err::<~str, ~str>(~"a").map_err(|_x| ~"b"), Err(~"b"));
404+
assert_eq!(Ok::<~str, ~str>(~"a").map_err(|x| (~"b").append(*x)), Ok(~"a"));
405+
assert_eq!(Err::<~str, ~str>(~"a").map_err(|x| (~"b").append(*x)), Err(~"ba"));
406+
}
407+
408+
#[test]
409+
pub fn test_impl_map_move() {
410+
assert_eq!(Ok::<~str, ~str>(~"a").map_move(|x| x + ~"b"), Ok(~"ab"));
411+
assert_eq!(Err::<~str, ~str>(~"a").map_move(|x| x + ~"b"), Err(~"a"));
412+
}
413+
414+
#[test]
415+
pub fn test_impl_map_err_move() {
416+
assert_eq!(Ok::<~str, ~str>(~"a").map_err_move(|x| x + ~"b"), Ok(~"a"));
417+
assert_eq!(Err::<~str, ~str>(~"a").map_err_move(|x| x + ~"b"), Err(~"ab"));
370418
}
371419

372420
#[test]

src/libstd/rt/task.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,10 @@ mod test {
465465
do run_in_newsched_task() {
466466
static key: local_data::Key<@~str> = &local_data::Key;
467467
local_data::set(key, @~"data");
468-
assert!(*local_data::get(key, |k| k.map(|&k| *k)).unwrap() == ~"data");
468+
assert!(*local_data::get(key, |k| k.map_move(|k| *k)).unwrap() == ~"data");
469469
static key2: local_data::Key<@~str> = &local_data::Key;
470470
local_data::set(key2, @~"data");
471-
assert!(*local_data::get(key2, |k| k.map(|&k| *k)).unwrap() == ~"data");
471+
assert!(*local_data::get(key2, |k| k.map_move(|k| *k)).unwrap() == ~"data");
472472
}
473473
}
474474

0 commit comments

Comments
 (0)