diff --git a/src/libextra/json.rs b/src/libextra/json.rs index 6a7f0607dd66aa3240f999534240302839ec4ca0..4cd67d6ebacf3587e50d58f610904c76ecb50e31 100644 --- a/src/libextra/json.rs +++ b/src/libextra/json.rs @@ -1867,27 +1867,26 @@ fn test_read_object() { 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 @@ fn test_read_object() { "\"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 b1c07e83f52c7a3336246246c528dfe314e35abb..f926572dad61726a7ccb7356c1ffeba2f9023aa7 100644 --- a/src/libextra/time.rs +++ b/src/libextra/time.rs @@ -1132,13 +1132,13 @@ fn test(s: &str, format: &str) -> bool { 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 286b1f84802c3298eaec4541ab5306a605350916..a203fccfb5f3c7520ff3dbff0a78bccc5193e54a 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 8e44a42e038ab1545a7fed9b0bc93c20fc43b5b9..f43b3e73113056ed9f1f5d8f1fa546b7bc568ab8 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), + } } -} - - - - - -impl Result { /** * Get a reference to the value out of a successful result * @@ -84,7 +78,7 @@ pub fn is_err(&self) -> bool { } /** - * 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 @@ pub fn iter(&self, f: &fn(&T)) { } /** - * 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 @@ pub fn unwrap_err(self) -> E { } /** - * 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 @@ pub fn unwrap_err(self) -> E { * * 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 @@ pub fn chain(self, op: &fn(T) -> Result) -> 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 @@ pub fn get(&self) -> T { } /** - * 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 @@ pub fn get_err(&self) -> E { } /** - * 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 @@ pub fn test_get_ref_method() { 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 0d93bdb6f9446347d80912c2487b3d8f9ccc98c2..754181f9cd848ba7f83cb0acb72db6d629548b6f 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 5d05817e512a0e59ca1831365b34222af82637d2..4597212b390eb90cc6bf72a22e2140f401dec1e9 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 46b882f7b82534873d0c07b1df05c62f4a00a510..8b4664ac0606e94dbb049100f9501b4b58f49d2f 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 cb9cce3249f011046196ecb429389472ec53b987..6e5005fe03dc12aa003db7d3b9c8c5910ad6a6e2 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 2446e9057c2e7ccd27dd78ad05c15537f527574a..45375efe9d630a0d7e5e6a04d9fd867e7c66954c 100644 --- a/src/test/run-pass/cleanup-copy-mode.rs +++ b/src/test/run-pass/cleanup-copy-mode.rs @@ -16,7 +16,7 @@ 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 6b0dd6cb9472269d7c34ff1a1f9ca1bc71d971d6..c4178961d9e6946577d558a0018cdf8df04ca640 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!()