diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 54c901f3a24f..8e44a42e038a 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -46,45 +46,10 @@ pub fn to_either(res: &Result) } } -/** - * Call a function based on a previous result - * - * If `res` is `ok` then the value is extracted and passed to `op` whereupon - * `op`s result is wrapped in `ok` and returned. if `res` is `err` then it is - * immediately returned. This function can be used to compose the results of - * two functions. - * - * Example: - * - * let res = map(read_file(file)) { |buf| - * parse_bytes(buf) - * } - */ -#[inline] -pub fn map(res: &Result, op: &fn(&T) -> U) - -> Result { - match *res { - Ok(ref t) => Ok(op(t)), - Err(ref e) => Err((*e).clone()) - } -} -/** - * Call a function based on a previous result - * - * If `res` is `err` then the value is extracted and passed to `op` whereupon - * `op`s result is wrapped in an `err` and returned. if `res` is `ok` then it - * is immediately returned. This function can be used to pass through a - * successful result while handling an error. - */ -#[inline] -pub fn map_err(res: &Result, op: &fn(&E) -> F) - -> Result { - match *res { - Ok(ref t) => Ok((*t).clone()), - Err(ref e) => Err(op(e)) - } -} + + + impl Result { /** @@ -229,9 +194,20 @@ impl Result { } } + /** + * Call a function based on a previous result + * + * If `*self` is `err` then the value is extracted and passed to `op` whereupon + * `op`s result is wrapped in an `err` and returned. if `*self` is `ok` then it + * is immediately returned. This function can be used to pass through a + * successful result while handling an error. + */ #[inline] pub fn map_err(&self, op: &fn(&E) -> F) -> Result { - map_err(self, op) + match *self { + Ok(ref t) => Ok(t.clone()), + Err(ref e) => Err(op(e)) + } } } @@ -251,9 +227,26 @@ impl Result { } } + /** + * Call a function based on a previous result + * + * If `res` is `ok` then the value is extracted and passed to `op` whereupon + * `op`s result is wrapped in `ok` and returned. if `res` is `err` then it is + * immediately returned. This function can be used to compose the results of + * two functions. + * + * Example: + * + * let res = map(read_file(file)) { |buf| + * parse_bytes(buf) + * } + */ #[inline] pub fn map(&self, op: &fn(&T) -> U) -> Result { - map(self, op) + match *self { + Ok(ref t) => Ok(op(t)), + Err(ref e) => Err(e.clone()) + } } }