diff --git a/src/librustc/metadata/filesearch.rs b/src/librustc/metadata/filesearch.rs index 6017b804b57..7ac215e3949 100644 --- a/src/librustc/metadata/filesearch.rs +++ b/src/librustc/metadata/filesearch.rs @@ -147,7 +147,7 @@ pub fn get_rustpkg_root() -> Result { } pub fn get_rustpkg_root_nearest() -> Result { - do result::chain(get_rustpkg_root()) |p| { + do get_rustpkg_root().chain |p| { let cwd = os::getcwd(); let cwd_rustpkg = cwd.push(".rustpkg"); let rustpkg_is_non_root_file = @@ -173,13 +173,13 @@ pub fn get_rustpkg_root_nearest() -> Result { } fn get_rustpkg_lib_path() -> Result { - do result::chain(get_rustpkg_root()) |p| { + do get_rustpkg_root().chain |p| { result::Ok(p.push(libdir())) } } fn get_rustpkg_lib_path_nearest() -> Result { - do result::chain(get_rustpkg_root_nearest()) |p| { + do get_rustpkg_root_nearest().chain |p| { result::Ok(p.push(libdir())) } } diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index de4815ab7a6..aa3557e8a9d 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -138,7 +138,7 @@ fn config_from_opts( let config = default_config(input_crate); let result = result::Ok(config); - let result = do result::chain(result) |config| { + let result = do result.chain |config| { let output_dir = getopts::opt_maybe_str(matches, opt_output_dir()); let output_dir = output_dir.map(|s| Path(*s)); result::Ok(Config { @@ -146,14 +146,10 @@ fn config_from_opts( .. config }) }; - let result = do result::chain(result) |config| { - let output_format = getopts::opt_maybe_str( - matches, opt_output_format()); - do output_format.map_default(result::Ok(config.clone())) - |output_format| { - do result::chain(parse_output_format(*output_format)) - |output_format| { - + let result = do result.chain |config| { + let output_format = getopts::opt_maybe_str(matches, opt_output_format()); + do output_format.map_default(result::Ok(config.clone())) |output_format| { + do parse_output_format(*output_format).chain |output_format| { result::Ok(Config { output_format: output_format, .. config.clone() @@ -161,13 +157,11 @@ fn config_from_opts( } } }; - let result = do result::chain(result) |config| { + let result = do result.chain |config| { let output_style = getopts::opt_maybe_str(matches, opt_output_style()); - do output_style.map_default(result::Ok(config.clone())) - |output_style| { - do result::chain(parse_output_style(*output_style)) - |output_style| { + do output_style.map_default(result::Ok(config.clone())) |output_style| { + do parse_output_style(*output_style).chain |output_style| { result::Ok(Config { output_style: output_style, .. config.clone() @@ -176,11 +170,11 @@ fn config_from_opts( } }; let process_output = Cell::new(process_output); - let result = do result::chain(result) |config| { + let result = do result.chain |config| { let pandoc_cmd = getopts::opt_maybe_str(matches, opt_pandoc_cmd()); let pandoc_cmd = maybe_find_pandoc( &config, pandoc_cmd, process_output.take()); - do result::chain(pandoc_cmd) |pandoc_cmd| { + do pandoc_cmd.chain |pandoc_cmd| { result::Ok(Config { pandoc_cmd: pandoc_cmd, .. config.clone() diff --git a/src/libstd/io.rs b/src/libstd/io.rs index fcc53c33a5d..0d781a1aea2 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -1726,21 +1726,21 @@ pub fn seek_in_buf(offset: int, pos: uint, len: uint, whence: SeekStyle) -> } pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> { - result::chain(read_whole_file(file), |bytes| { + do read_whole_file(file).chain |bytes| { if str::is_utf8(bytes) { result::Ok(str::from_bytes(bytes)) } else { result::Err(file.to_str() + " is not UTF-8") } - }) + } } // FIXME (#2004): implement this in a low-level way. Going through the // abstractions is pointless. pub fn read_whole_file(file: &Path) -> Result<~[u8], ~str> { - result::chain(file_reader(file), |rdr| { + do file_reader(file).chain |rdr| { result::Ok(rdr.read_whole_stream()) - }) + } } // fsync related @@ -1851,10 +1851,10 @@ mod tests { ~"A hoopy frood who really knows where his towel is."; debug!(frood.clone()); { - let out: @io::Writer = io::file_writer(tmpfile, [io::Create, io::Truncate]).unwrap(); + let out = io::file_writer(tmpfile, [io::Create, io::Truncate]).unwrap(); out.write_str(frood); } - let inp: @io::Reader = io::file_reader(tmpfile).unwrap(); + let inp = io::file_reader(tmpfile).unwrap(); let frood2: ~str = inp.read_c_str(); debug!(frood2.clone()); assert_eq!(frood, frood2); diff --git a/src/libstd/result.rs b/src/libstd/result.rs index ec2715fcf2e..0aeb9654f56 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -77,61 +77,6 @@ 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 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 = chain(read_file(file)) { |buf| - * ok(parse_bytes(buf)) - * } - */ -#[inline] -pub fn chain(res: Result, op: &fn(T) - -> Result) -> Result { - match res { - Ok(t) => op(t), - Err(e) => Err(e) - } -} - -/** - * 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 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 chain_err( - res: Result, - op: &fn(t: V) -> Result) - -> Result { - match res { - Ok(t) => Ok(t), - Err(v) => op(v) - } -} - - - - -/** - * 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 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. - */ - - /** * Call a function based on a previous result * @@ -208,7 +153,7 @@ impl Result { * Call a function based on a previous result * * If `*self` is `ok` then the value is extracted and passed to `op` whereupon - * `op`s result is returned. if `res` is `err` then it is immediately + * `op`s result is returned. if `*self` is `err` then it is immediately * returned. This function can be used to compose the results of two * functions. * @@ -230,7 +175,7 @@ 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 returned. if `res` is `ok` then it is immediately returned. + * `op`s result is 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. */ @@ -260,14 +205,42 @@ impl Result { } } + /** + * Call a function based on a previous result + * + * If `self` is `ok` then the value is extracted and passed to `op` whereupon + * `op`s result is returned. if `self` is `err` then it is immediately + * returned. This function can be used to compose the results of two + * functions. + * + * Example: + * + * let res = read_file(file).chain(op) { |buf| + * ok(parse_bytes(buf)) + * } + */ #[inline] pub fn chain(self, op: &fn(T) -> Result) -> Result { - chain(self, op) + match self { + Ok(t) => op(t), + Err(e) => Err(e) + } } + /** + * 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 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 chain_err(self, op: &fn(E) -> Result) -> Result { - chain_err(self, op) + match self { + Ok(t) => Ok(t), + Err(v) => op(v) + } } } @@ -390,7 +363,7 @@ pub fn iter_vec2(ss: &[S], ts: &[T], #[cfg(test)] mod tests { - use result::{Err, Ok, Result, chain, get, get_err}; + use result::{Err, Ok, Result, get, get_err}; use result; pub fn op1() -> result::Result { result::Ok(666) } @@ -403,12 +376,12 @@ mod tests { #[test] pub fn chain_success() { - assert_eq!(get(&chain(op1(), op2)), 667u); + assert_eq!(get(&(op1().chain(op2))), 667u); } #[test] pub fn chain_failure() { - assert_eq!(get_err(&chain(op3(), op2)), ~"sadface"); + assert_eq!(get_err(&op3().chain( op2)), ~"sadface"); } #[test]