diff options
| author | maikklein <maikklein@googlemail.com> | 2013-07-23 02:27:53 +0200 |
|---|---|---|
| committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-07-27 23:41:09 -0700 |
| commit | f6bcf5d5f11a24930c8b3eea4269ba0df583b5cf (patch) | |
| tree | 1767bb41f471226be2be3ac530141702e3915e53 /src/libstd | |
| parent | e308167a2fe558924fcebcc99358c057ae0b90b4 (diff) | |
| download | rust-f6bcf5d5f11a24930c8b3eea4269ba0df583b5cf.tar.gz rust-f6bcf5d5f11a24930c8b3eea4269ba0df583b5cf.zip | |
cleanup .chain and .chain_err + fixing other files
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io.rs | 12 | ||||
| -rw-r--r-- | src/libstd/result.rs | 97 |
2 files changed, 41 insertions, 68 deletions
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 @@ -81,61 +81,6 @@ pub fn to_either<T:Clone,U:Clone>(res: &Result<U, T>) * 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<T, U, V>(res: Result<T, V>, op: &fn(T) - -> Result<U, V>) -> Result<U, V> { - 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<T, U, V>( - res: Result<T, V>, - op: &fn(t: V) -> Result<T, U>) - -> Result<T, U> { - 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 - * - * 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. @@ -208,7 +153,7 @@ impl<T, E> Result<T, E> { * 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<T, E> Result<T, 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 `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<T, E> Result<T, E> { } } + /** + * 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<U>(self, op: &fn(T) -> Result<U,E>) -> Result<U,E> { - 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<F>(self, op: &fn(E) -> Result<T,F>) -> Result<T,F> { - chain_err(self, op) + match self { + Ok(t) => Ok(t), + Err(v) => op(v) + } } } @@ -390,7 +363,7 @@ pub fn iter_vec2<S,T,U>(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<int, ~str> { 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] |
