diff --git a/src/libextra/json.rs b/src/libextra/json.rs index 6a7f0607dd6..4cd67d6ebac 100644 --- a/src/libextra/json.rs +++ b/src/libextra/json.rs @@ -1867,27 +1867,26 @@ mod tests { col: 8u, msg: @~"EOF while parsing object"})); - assert_eq!(result::unwrap(from_str("{}")), mk_object([])); - assert_eq!(result::unwrap(from_str("{\"a\": 3}")), + assert_eq!(from_str("{}").unwrap(), mk_object([])); + assert_eq!(from_str("{\"a\": 3}").unwrap(), mk_object([(~"a", Number(3.0f))])); - assert_eq!(result::unwrap(from_str( - "{ \"a\": null, \"b\" : true }")), + assert_eq!(from_str( + "{ \"a\": null, \"b\" : true }").unwrap(), mk_object([ (~"a", Null), (~"b", Boolean(true))])); - assert_eq!(result::unwrap( - from_str("\n{ \"a\": null, \"b\" : true }\n")), + assert_eq!(from_str("\n{ \"a\": null, \"b\" : true }\n").unwrap(), mk_object([ (~"a", Null), (~"b", Boolean(true))])); - assert_eq!(result::unwrap(from_str( - "{\"a\" : 1.0 ,\"b\": [ true ]}")), + assert_eq!(from_str( + "{\"a\" : 1.0 ,\"b\": [ true ]}").unwrap(), mk_object([ (~"a", Number(1.0)), (~"b", List(~[Boolean(true)])) ])); - assert_eq!(result::unwrap(from_str( + assert_eq!(from_str( ~"{" + "\"a\": 1.0, " + "\"b\": [" + @@ -1895,7 +1894,7 @@ mod tests { "\"foo\\nbar\", " + "{ \"c\": {\"d\": null} } " + "]" + - "}")), + "}").unwrap(), mk_object([ (~"a", Number(1.0f)), (~"b", List(~[ diff --git a/src/libextra/time.rs b/src/libextra/time.rs index b1c07e83f52..f926572dad6 100644 --- a/src/libextra/time.rs +++ b/src/libextra/time.rs @@ -1132,13 +1132,13 @@ mod tests { assert!(test("6", "%w")); assert!(test("2009", "%Y")); assert!(test("09", "%y")); - assert!(result::unwrap(strptime("UTC", "%Z")).tm_zone == + assert!(strptime("UTC", "%Z").unwrap().tm_zone == ~"UTC"); - assert!(result::unwrap(strptime("PST", "%Z")).tm_zone == + assert!(strptime("PST", "%Z").unwrap().tm_zone == ~""); - assert!(result::unwrap(strptime("-0000", "%z")).tm_gmtoff == + assert!(strptime("-0000", "%z").unwrap().tm_gmtoff == 0); - assert!(result::unwrap(strptime("-0800", "%z")).tm_gmtoff == + assert!(strptime("-0800", "%z").unwrap().tm_gmtoff == 0); assert!(test("%", "%%")); diff --git a/src/librustpkg/tests.rs b/src/librustpkg/tests.rs index 286b1f84802..a203fccfb5f 100644 --- a/src/librustpkg/tests.rs +++ b/src/librustpkg/tests.rs @@ -62,9 +62,7 @@ fn git_repo_pkg() -> PkgId { } fn writeFile(file_path: &Path, contents: &str) { - let out: @io::Writer = - result::unwrap(io::file_writer(file_path, - [io::Create, io::Truncate])); + let out = io::file_writer(file_path, [io::Create, io::Truncate]).unwrap(); out.write_line(contents); } diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 8e44a42e038..f43b3e73113 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -31,27 +31,21 @@ pub enum Result { Err(U) } -/** - * Convert to the `either` type - * - * `ok` result variants are converted to `either::right` variants, `err` - * result variants are converted to `either::left`. - */ -#[inline] -pub fn to_either(res: &Result) - -> Either { - match *res { - Ok(ref res) => either::Right((*res).clone()), - Err(ref fail_) => either::Left((*fail_).clone()) - } -} - - - - - - impl Result { + /** + * Convert to the `either` type + * + * `ok` result variants are converted to `either::right` variants, `err` + * result variants are converted to `either::left`. + */ + #[inline] + pub fn to_either(self)-> Either{ + match self { + Ok(t) => either::Right(t), + Err(e) => either::Left(e), + } + } + /** * Get a reference to the value out of a successful result * @@ -84,7 +78,7 @@ impl Result { } /** - * Call a function based on a previous result + * Call a method 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 @@ -106,7 +100,7 @@ impl Result { } /** - * Call a function based on a previous result + * Call a method 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. @@ -140,7 +134,7 @@ impl Result { } /** - * Call a function based on a previous result + * Call a method 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 @@ -149,9 +143,9 @@ impl Result { * * Example: * - * let res = read_file(file).chain(op) { |buf| + * let res = do read_file(file).chain |buf| { * ok(parse_bytes(buf)) - * } + * }; */ #[inline] pub fn chain(self, op: &fn(T) -> Result) -> Result { @@ -162,7 +156,7 @@ impl Result { } /** - * Call a function based on a previous result + * Call a method 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 @@ -195,13 +189,13 @@ 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. - */ + * Call a method 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 { match *self { @@ -228,19 +222,19 @@ 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) - * } - */ + * Call a method 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 = read_file(file).map() { |buf| + * parse_bytes(buf) + * }); + */ #[inline] pub fn map(&self, op: &fn(&T) -> U) -> Result { match *self { @@ -351,6 +345,7 @@ pub fn iter_vec2(ss: &[S], ts: &[T], mod tests { use result::{Err, Ok, Result}; use result; + use either; pub fn op1() -> result::Result { result::Ok(666) } @@ -408,4 +403,13 @@ mod tests { let foo: Result = Ok(100); assert_eq!(*foo.get_ref(), 100); } + + #[test] + pub fn test_to_either() { + let r: Result = Ok(100); + let err: Result<(), int> = Err(404); + + assert_eq!(r.to_either(), either::Right(100)); + assert_eq!(err.to_either(), either::Left(404)); + } } diff --git a/src/test/bench/core-std.rs b/src/test/bench/core-std.rs index 0d93bdb6f94..754181f9cd8 100644 --- a/src/test/bench/core-std.rs +++ b/src/test/bench/core-std.rs @@ -75,7 +75,7 @@ fn read_line() { .push_rel(&Path("src/test/bench/shootout-k-nucleotide.data")); for int::range(0, 3) |_i| { - let reader = result::unwrap(io::file_reader(&path)); + let reader = io::file_reader(&path).unwrap(); while !reader.eof() { reader.read_line(); } diff --git a/src/test/bench/shootout-fasta.rs b/src/test/bench/shootout-fasta.rs index 5d05817e512..4597212b390 100644 --- a/src/test/bench/shootout-fasta.rs +++ b/src/test/bench/shootout-fasta.rs @@ -124,8 +124,8 @@ fn main() { }; let writer = if os::getenv("RUST_BENCH").is_some() { - result::unwrap(io::file_writer(&Path("./shootout-fasta.data"), - [io::Truncate, io::Create])) + io::file_writer(&Path("./shootout-fasta.data"), + [io::Truncate, io::Create]).unwrap() } else { io::stdout() }; diff --git a/src/test/bench/shootout-k-nucleotide-pipes.rs b/src/test/bench/shootout-k-nucleotide-pipes.rs index 46b882f7b82..8b4664ac060 100644 --- a/src/test/bench/shootout-k-nucleotide-pipes.rs +++ b/src/test/bench/shootout-k-nucleotide-pipes.rs @@ -161,7 +161,7 @@ fn main() { // get to this massive data set, but include_bin! chokes on it (#2598) let path = Path(env!("CFG_SRC_DIR")) .push_rel(&Path("src/test/bench/shootout-k-nucleotide.data")); - result::unwrap(io::file_reader(&path)) + io::file_reader(&path).unwrap() } else { io::stdin() }; diff --git a/src/test/run-fail/result-get-fail.rs b/src/test/run-fail/result-get-fail.rs index cb9cce3249f..6e5005fe03d 100644 --- a/src/test/run-fail/result-get-fail.rs +++ b/src/test/run-fail/result-get-fail.rs @@ -13,5 +13,5 @@ use std::result; fn main() { - error!(result::get(&result::Err::(~"kitty"))); + error!(result::Err::(~"kitty").get()); } diff --git a/src/test/run-pass/cleanup-copy-mode.rs b/src/test/run-pass/cleanup-copy-mode.rs index 2446e9057c2..45375efe9d6 100644 --- a/src/test/run-pass/cleanup-copy-mode.rs +++ b/src/test/run-pass/cleanup-copy-mode.rs @@ -16,7 +16,7 @@ use std::task; fn adder(x: @int, y: @int) -> int { return *x + *y; } fn failer() -> @int { fail!(); } pub fn main() { - assert!(result::is_err(&task::try(|| { + assert!(task::try(|| { adder(@2, failer()); () - }))); + }).is_err()); } diff --git a/src/test/run-pass/issue-4016.rs b/src/test/run-pass/issue-4016.rs index 6b0dd6cb947..c4178961d9e 100644 --- a/src/test/run-pass/issue-4016.rs +++ b/src/test/run-pass/issue-4016.rs @@ -11,14 +11,13 @@ extern mod extra; -use std::result; use extra::json; use extra::serialize::Decodable; trait JD : Decodable { } fn exec() { - let doc = result::unwrap(json::from_str("")); + let doc = json::from_str("").unwrap(); let mut decoder = json::Decoder(doc); let _v: T = Decodable::decode(&mut decoder); fail!()