Write option::chain and result::chain with match move

Closes #3590
This commit is contained in:
Tim Chevalier
2012-10-11 14:14:46 -07:00
parent 5a8ba073bc
commit 1ab914df1d
2 changed files with 6 additions and 10 deletions

View File

@@ -119,11 +119,9 @@ pub pure fn chain<T, U>(opt: Option<T>,
* function that returns an option. * function that returns an option.
*/ */
// XXX write with move match match move opt {
if opt.is_some() { Some(move t) => f(t),
f(unwrap(opt)) None => None
} else {
None
} }
} }

View File

@@ -105,11 +105,9 @@ pub pure fn to_either<T: Copy, U: Copy>(res: &Result<U, T>)
*/ */
pub fn chain<T, U: Copy, V: Copy>(res: Result<T, V>, op: fn(t: T) pub fn chain<T, U: Copy, V: Copy>(res: Result<T, V>, op: fn(t: T)
-> Result<U, V>) -> Result<U, V> { -> Result<U, V>) -> Result<U, V> {
// XXX: Should be writable with move + match match move res {
if res.is_ok() { Ok(move t) => op(t),
op(unwrap(res)) Err(move e) => Err(e)
} else {
Err(unwrap_err(res))
} }
} }