From 225f1c760d998b2f26107c3938d4ca8baa57c6c0 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 26 Jul 2013 18:03:26 -0700 Subject: cleanup get_ref --- src/libstd/result.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/result.rs b/src/libstd/result.rs index acc4ece8fd5..7b660ed8624 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -47,22 +47,6 @@ pub fn get(res: &Result) -> T { } } -/** - * Get a reference to the value out of a successful result - * - * # Failure - * - * If the result is an error - */ -#[inline] -pub fn get_ref<'a, T, U>(res: &'a Result) -> &'a T { - match *res { - Ok(ref t) => t, - Err(ref the_err) => - fail!("get_ref called on error result: %?", *the_err) - } -} - /** * Get the value out of an error result * @@ -229,8 +213,21 @@ pub fn map_err(res: &Result, op: &fn(&E) -> F) } impl Result { + /** + * Get a reference to the value out of a successful result + * + * # Failure + * + * If the result is an error + */ #[inline] - pub fn get_ref<'a>(&'a self) -> &'a T { get_ref(self) } + pub fn get_ref<'a>(&'a self) -> &'a T { + match *self { + Ok(ref t) => t, + Err(ref the_err) => + fail!("get_ref called on error result: %?", *the_err) + } + } #[inline] pub fn is_ok(&self) -> bool { is_ok(self) } -- cgit 1.4.1-3-g733a5 From 4eb95f6d1c22419b50980d0bf8effc6e2f22feb5 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 26 Jul 2013 18:03:44 -0700 Subject: cleanup .iter and .iter_err --- src/libstd/result.rs | 95 ++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 47 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 7b660ed8624..809244af12a 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -62,21 +62,6 @@ pub fn get_err(res: &Result) -> U { } } -/// Returns true if the result is `ok` -#[inline] -pub fn is_ok(res: &Result) -> bool { - match *res { - Ok(_) => true, - Err(_) => false - } -} - -/// Returns true if the result is `err` -#[inline] -pub fn is_err(res: &Result) -> bool { - !is_ok(res) -} - /** * Convert to the `either` type * @@ -134,27 +119,8 @@ pub fn chain_err( } } -/** - * 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: - * - * iter(read_file(file)) { |buf| - * print_buf(buf) - * } - */ -#[inline] -pub fn iter(res: &Result, f: &fn(&T)) { - match *res { - Ok(ref t) => f(t), - Err(_) => () - } -} + + /** * Call a function based on a previous result @@ -164,13 +130,7 @@ pub fn iter(res: &Result, f: &fn(&T)) { * This function can be used to pass through a successful result while * handling an error. */ -#[inline] -pub fn iter_err(res: &Result, f: &fn(&E)) { - match *res { - Ok(_) => (), - Err(ref e) => f(e) - } -} + /** * Call a function based on a previous result @@ -229,17 +189,58 @@ impl Result { } } + /// Returns true if the result is `ok` #[inline] - pub fn is_ok(&self) -> bool { is_ok(self) } + pub fn is_ok(&self) -> bool { + match *self { + Ok(_) => true, + Err(_) => false + } + } + /// Returns true if the result is `err` #[inline] - pub fn is_err(&self) -> bool { is_err(self) } + pub fn is_err(&self) -> bool { + !self.is_ok() + } + /** + * 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 + * returned. This function can be used to compose the results of two + * functions. + * + * Example: + * + * read_file(file).iter() { |buf| + * print_buf(buf) + * } + */ #[inline] - pub fn iter(&self, f: &fn(&T)) { iter(self, f) } + pub fn iter(&self, f: &fn(&T)) { + match *self { + Ok(ref t) => f(t), + Err(_) => () + } + } + /** + * 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. + * This function can be used to pass through a successful result while + * handling an error. + */ #[inline] - pub fn iter_err(&self, f: &fn(&E)) { iter_err(self, f) } + pub fn iter_err(&self, f: &fn(&E)) { + match *self { + Ok(_) => (), + Err(ref e) => f(e) + } + } #[inline] pub fn unwrap(self) -> T { unwrap(self) } -- cgit 1.4.1-3-g733a5 From e308167a2fe558924fcebcc99358c057ae0b90b4 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 26 Jul 2013 18:03:57 -0700 Subject: cleanup .unwrap and .unwrap_err fixing io tests --- src/libextra/workcache.rs | 3 +-- src/libstd/io.rs | 6 ++---- src/libstd/result.rs | 33 ++++++++++++++------------------- 3 files changed, 17 insertions(+), 25 deletions(-) (limited to 'src/libstd') diff --git a/src/libextra/workcache.rs b/src/libextra/workcache.rs index 42210d0cd89..1cabab713b8 100644 --- a/src/libextra/workcache.rs +++ b/src/libextra/workcache.rs @@ -22,7 +22,6 @@ use std::cell::Cell; use std::comm::{PortOne, oneshot, send_one, recv_one}; use std::either::{Either, Left, Right}; use std::io; -use std::result; use std::run; use std::task; @@ -208,7 +207,7 @@ fn json_encode>(t: &T) -> ~str { // FIXME(#5121) fn json_decode>(s: &str) -> T { do io::with_str_reader(s) |rdr| { - let j = result::unwrap(json::from_reader(rdr)); + let j = json::from_reader(rdr).unwrap(); let mut decoder = json::Decoder(j); Decodable::decode(&mut decoder) } diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 05a5184ccba..fcc53c33a5d 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -1851,12 +1851,10 @@ mod tests { ~"A hoopy frood who really knows where his towel is."; debug!(frood.clone()); { - let out: @io::Writer = - result::unwrap( - io::file_writer(tmpfile, [io::Create, io::Truncate])); + let out: @io::Writer = io::file_writer(tmpfile, [io::Create, io::Truncate]).unwrap(); out.write_str(frood); } - let inp: @io::Reader = result::unwrap(io::file_reader(tmpfile)); + let inp: @io::Reader = 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 809244af12a..ec2715fcf2e 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -242,11 +242,23 @@ impl Result { } } + /// Unwraps a result, assuming it is an `ok(T)` #[inline] - pub fn unwrap(self) -> T { unwrap(self) } + pub fn unwrap(self) -> T { + match self { + Ok(t) => t, + Err(_) => fail!("unwrap called on an err result") + } + } + /// Unwraps a result, assuming it is an `err(U)` #[inline] - pub fn unwrap_err(self) -> E { unwrap_err(self) } + pub fn unwrap_err(self) -> E { + match self { + Err(u) => u, + Ok(_) => fail!("unwrap called on an ok result") + } + } #[inline] pub fn chain(self, op: &fn(T) -> Result) -> Result { @@ -375,23 +387,6 @@ pub fn iter_vec2(ss: &[S], ts: &[T], return Ok(()); } -/// Unwraps a result, assuming it is an `ok(T)` -#[inline] -pub fn unwrap(res: Result) -> T { - match res { - Ok(t) => t, - Err(_) => fail!("unwrap called on an err result") - } -} - -/// Unwraps a result, assuming it is an `err(U)` -#[inline] -pub fn unwrap_err(res: Result) -> U { - match res { - Err(u) => u, - Ok(_) => fail!("unwrap called on an ok result") - } -} #[cfg(test)] mod tests { -- cgit 1.4.1-3-g733a5 From f6bcf5d5f11a24930c8b3eea4269ba0df583b5cf Mon Sep 17 00:00:00 2001 From: maikklein Date: Tue, 23 Jul 2013 02:27:53 +0200 Subject: cleanup .chain and .chain_err + fixing other files --- src/librustc/metadata/filesearch.rs | 6 +-- src/librustdoc/config.rs | 26 ++++------ src/libstd/io.rs | 12 ++--- src/libstd/result.rs | 97 +++++++++++++------------------------ 4 files changed, 54 insertions(+), 87 deletions(-) (limited to 'src/libstd') 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] -- cgit 1.4.1-3-g733a5 From 9dc1de4c9e84a5b8625ff1f066b3f05c39531d9d Mon Sep 17 00:00:00 2001 From: maikklein Date: Tue, 23 Jul 2013 03:41:53 +0200 Subject: cleanup .get and .get_err --- src/libstd/result.rs | 65 +++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 36 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 0aeb9654f56..54c901f3a24 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -31,37 +31,6 @@ pub enum Result { Err(U) } -/** - * Get the value out of a successful result - * - * # Failure - * - * If the result is an error - */ -#[inline] -pub fn get(res: &Result) -> T { - match *res { - Ok(ref t) => (*t).clone(), - Err(ref the_err) => - fail!("get called on error result: %?", *the_err) - } -} - -/** - * Get the value out of an error result - * - * # Failure - * - * If the result is not an error - */ -#[inline] -pub fn get_err(res: &Result) -> U { - match *res { - Err(ref u) => (*u).clone(), - Ok(_) => fail!("get_err called on ok result") - } -} - /** * Convert to the `either` type * @@ -245,8 +214,20 @@ impl Result { } impl Result { + /** + * Get the value out of a successful result + * + * # Failure + * + * If the result is an error + */ #[inline] - pub fn get(&self) -> T { get(self) } + pub fn get(&self) -> T { + match *self { + Ok(ref t) => (*t).clone(), + Err(ref e) => fail!("get called on error result: %?", *e), + } + } #[inline] pub fn map_err(&self, op: &fn(&E) -> F) -> Result { @@ -255,8 +236,20 @@ impl Result { } impl Result { + /** + * Get the value out of an error result + * + * # Failure + * + * If the result is not an error + */ #[inline] - pub fn get_err(&self) -> E { get_err(self) } + pub fn get_err(&self) -> E { + match *self { + Err(ref u) => (*u).clone(), + Ok(_) => fail!("get_err called on ok result"), + } + } #[inline] pub fn map(&self, op: &fn(&T) -> U) -> Result { @@ -363,7 +356,7 @@ pub fn iter_vec2(ss: &[S], ts: &[T], #[cfg(test)] mod tests { - use result::{Err, Ok, Result, get, get_err}; + use result::{Err, Ok, Result}; use result; pub fn op1() -> result::Result { result::Ok(666) } @@ -376,12 +369,12 @@ mod tests { #[test] pub fn chain_success() { - assert_eq!(get(&(op1().chain(op2))), 667u); + assert_eq!(op1().chain(op2).get(), 667u); } #[test] pub fn chain_failure() { - assert_eq!(get_err(&op3().chain( op2)), ~"sadface"); + assert_eq!(op3().chain( op2).get_err(), ~"sadface"); } #[test] -- cgit 1.4.1-3-g733a5 From aad53cb6e298192cc75cd5769a1b997a3457666c Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 26 Jul 2013 19:19:33 -0700 Subject: cleanup .map and .map_err --- src/libstd/result.rs | 73 ++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 40 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 54c901f3a24..8e44a42e038 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()) + } } } -- cgit 1.4.1-3-g733a5 From 2a68c719f4a32123fbd428fe280f41af47d00fea Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 26 Jul 2013 18:36:51 -0700 Subject: to_either + fixes --- src/libextra/json.rs | 19 +++--- src/libextra/time.rs | 8 +-- src/librustpkg/tests.rs | 4 +- src/libstd/result.rs | 94 ++++++++++++++------------- src/test/bench/core-std.rs | 2 +- src/test/bench/shootout-fasta.rs | 4 +- src/test/bench/shootout-k-nucleotide-pipes.rs | 2 +- src/test/run-fail/result-get-fail.rs | 2 +- src/test/run-pass/cleanup-copy-mode.rs | 4 +- src/test/run-pass/issue-4016.rs | 3 +- 10 files changed, 71 insertions(+), 71 deletions(-) (limited to 'src/libstd') 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), + } } -} - - - - - -impl Result { /** * 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!() -- cgit 1.4.1-3-g733a5 From 03a16dbb8e6341e622f7d09c9be99ec5502239f3 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 23 Jul 2013 21:29:02 -0700 Subject: std: fix the casing of option::{Some,None} in the docs --- src/libstd/option.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/option.rs b/src/libstd/option.rs index eb3f227562a..ff35af4cff0 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -271,7 +271,7 @@ impl Option { pub fn get_ref<'a>(&'a self) -> &'a T { match *self { Some(ref x) => x, - None => fail!("option::get_ref None") + None => fail!("option::get_ref `None`"), } } @@ -293,7 +293,7 @@ impl Option { pub fn get_mut_ref<'a>(&'a mut self) -> &'a mut T { match *self { Some(ref mut x) => x, - None => fail!("option::get_mut_ref None") + None => fail!("option::get_mut_ref `None`"), } } @@ -317,7 +317,7 @@ impl Option { */ match self { Some(x) => x, - None => fail!("option::unwrap None") + None => fail!("option::unwrap `None`"), } } @@ -331,7 +331,7 @@ impl Option { */ #[inline] pub fn take_unwrap(&mut self) -> T { - if self.is_none() { fail!("option::take_unwrap None") } + if self.is_none() { fail!("option::take_unwrap `None`") } self.take().unwrap() } @@ -369,7 +369,7 @@ impl Option { pub fn get(self) -> T { match self { Some(x) => return x, - None => fail!("option::get None") + None => fail!("option::get `None`") } } @@ -379,7 +379,7 @@ impl Option { match self { Some(x) => x, None => def } } - /// Applies a function zero or more times until the result is None. + /// Applies a function zero or more times until the result is `None`. #[inline] pub fn while_some(self, blk: &fn(v: T) -> Option) { let mut opt = self; -- cgit 1.4.1-3-g733a5 From 796b3371f9bbb6f6ed189219d1b055d7ed737e78 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 23 Jul 2013 21:30:25 -0700 Subject: std: Put the option tests into a tests submodule --- src/libstd/option.rs | 216 ++++++++++++++++++++++++++------------------------- 1 file changed, 111 insertions(+), 105 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/option.rs b/src/libstd/option.rs index ff35af4cff0..6e4880550eb 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -441,134 +441,140 @@ impl<'self, A> Iterator<&'self mut A> for OptionMutIterator<'self, A> { } } -#[test] -fn test_unwrap_ptr() { - unsafe { - let x = ~0; - let addr_x: *int = ::cast::transmute(&*x); +#[cfg(test)] +mod tests { + use super::*; + use util; + + #[test] + fn test_unwrap_ptr() { + unsafe { + let x = ~0; + let addr_x: *int = ::cast::transmute(&*x); + let opt = Some(x); + let y = opt.unwrap(); + let addr_y: *int = ::cast::transmute(&*y); + assert_eq!(addr_x, addr_y); + } + } + + #[test] + fn test_unwrap_str() { + let x = ~"test"; + let addr_x = x.as_imm_buf(|buf, _len| buf); let opt = Some(x); let y = opt.unwrap(); - let addr_y: *int = ::cast::transmute(&*y); + let addr_y = y.as_imm_buf(|buf, _len| buf); assert_eq!(addr_x, addr_y); } -} -#[test] -fn test_unwrap_str() { - let x = ~"test"; - let addr_x = x.as_imm_buf(|buf, _len| buf); - let opt = Some(x); - let y = opt.unwrap(); - let addr_y = y.as_imm_buf(|buf, _len| buf); - assert_eq!(addr_x, addr_y); -} + #[test] + fn test_unwrap_resource() { + struct R { + i: @mut int, + } -#[test] -fn test_unwrap_resource() { - struct R { - i: @mut int, - } + #[unsafe_destructor] + impl ::ops::Drop for R { + fn drop(&self) { *(self.i) += 1; } + } - #[unsafe_destructor] - impl ::ops::Drop for R { - fn drop(&self) { *(self.i) += 1; } - } + fn R(i: @mut int) -> R { + R { + i: i + } + } - fn R(i: @mut int) -> R { - R { - i: i + let i = @mut 0; + { + let x = R(i); + let opt = Some(x); + let _y = opt.unwrap(); } + assert_eq!(*i, 1); } - let i = @mut 0; - { - let x = R(i); - let opt = Some(x); - let _y = opt.unwrap(); + #[test] + fn test_option_dance() { + let x = Some(()); + let mut y = Some(5); + let mut y2 = 0; + for x.iter().advance |_x| { + y2 = y.take_unwrap(); + } + assert_eq!(y2, 5); + assert!(y.is_none()); + } + #[test] #[should_fail] #[ignore(cfg(windows))] + fn test_option_too_much_dance() { + let mut y = Some(util::NonCopyable); + let _y2 = y.take_unwrap(); + let _y3 = y.take_unwrap(); + } + + #[test] + fn test_option_while_some() { + let mut i = 0; + do Some(10).while_some |j| { + i += 1; + if (j > 0) { + Some(j-1) + } else { + None + } + } + assert_eq!(i, 11); } - assert_eq!(*i, 1); -} -#[test] -fn test_option_dance() { - let x = Some(()); - let mut y = Some(5); - let mut y2 = 0; - for x.iter().advance |_x| { - y2 = y.take_unwrap(); + #[test] + fn test_get_or_zero() { + let some_stuff = Some(42); + assert_eq!(some_stuff.get_or_zero(), 42); + let no_stuff: Option = None; + assert_eq!(no_stuff.get_or_zero(), 0); } - assert_eq!(y2, 5); - assert!(y.is_none()); -} -#[test] #[should_fail] #[ignore(cfg(windows))] -fn test_option_too_much_dance() { - let mut y = Some(util::NonCopyable); - let _y2 = y.take_unwrap(); - let _y3 = y.take_unwrap(); -} -#[test] -fn test_option_while_some() { - let mut i = 0; - do Some(10).while_some |j| { - i += 1; - if (j > 0) { - Some(j-1) - } else { - None - } + #[test] + fn test_filtered() { + let some_stuff = Some(42); + let modified_stuff = some_stuff.filtered(|&x| {x < 10}); + assert_eq!(some_stuff.get(), 42); + assert!(modified_stuff.is_none()); } - assert_eq!(i, 11); -} -#[test] -fn test_get_or_zero() { - let some_stuff = Some(42); - assert_eq!(some_stuff.get_or_zero(), 42); - let no_stuff: Option = None; - assert_eq!(no_stuff.get_or_zero(), 0); -} + #[test] + fn test_iter() { + let val = 5; -#[test] -fn test_filtered() { - let some_stuff = Some(42); - let modified_stuff = some_stuff.filtered(|&x| {x < 10}); - assert_eq!(some_stuff.get(), 42); - assert!(modified_stuff.is_none()); -} + let x = Some(val); + let mut it = x.iter(); -#[test] -fn test_iter() { - let val = 5; - - let x = Some(val); - let mut it = x.iter(); - - assert_eq!(it.size_hint(), (1, Some(1))); - assert_eq!(it.next(), Some(&val)); - assert_eq!(it.size_hint(), (0, Some(0))); - assert!(it.next().is_none()); -} + assert_eq!(it.size_hint(), (1, Some(1))); + assert_eq!(it.next(), Some(&val)); + assert_eq!(it.size_hint(), (0, Some(0))); + assert!(it.next().is_none()); + } -#[test] -fn test_mut_iter() { - let val = 5; - let new_val = 11; + #[test] + fn test_mut_iter() { + let val = 5; + let new_val = 11; - let mut x = Some(val); - let mut it = x.mut_iter(); + let mut x = Some(val); + let mut it = x.mut_iter(); - assert_eq!(it.size_hint(), (1, Some(1))); + assert_eq!(it.size_hint(), (1, Some(1))); - match it.next() { - Some(interior) => { - assert_eq!(*interior, val); - *interior = new_val; - assert_eq!(x, Some(new_val)); + match it.next() { + Some(interior) => { + assert_eq!(*interior, val); + *interior = new_val; + assert_eq!(x, Some(new_val)); + } + None => assert!(false), } - None => assert!(false), - } - assert_eq!(it.size_hint(), (0, Some(0))); - assert!(it.next().is_none()); + assert_eq!(it.size_hint(), (0, Some(0))); + assert!(it.next().is_none()); + } } -- cgit 1.4.1-3-g733a5 From ea106f72f7d28b3fc977899b6f272d5b69cbbc31 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 24 Jul 2013 20:41:13 -0700 Subject: core: correct the casing of result::{Ok,Err} in the docs --- src/libstd/result.rs | 116 +++++++++++++++++------------------ src/test/run-fail/result-get-fail.rs | 2 +- 2 files changed, 58 insertions(+), 60 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/result.rs b/src/libstd/result.rs index f43b3e73113..5919acc1125 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -35,8 +35,8 @@ impl Result { /** * Convert to the `either` type * - * `ok` result variants are converted to `either::right` variants, `err` - * result variants are converted to `either::left`. + * `Ok` result variants are converted to `either::Right` variants, `Err` + * result variants are converted to `either::Left`. */ #[inline] pub fn to_either(self)-> Either{ @@ -56,13 +56,12 @@ impl Result { #[inline] pub fn get_ref<'a>(&'a self) -> &'a T { match *self { - Ok(ref t) => t, - Err(ref the_err) => - fail!("get_ref called on error result: %?", *the_err) + Ok(ref t) => t, + Err(ref e) => fail!("get_ref called on `Err` result: %?", *e), } } - /// Returns true if the result is `ok` + /// Returns true if the result is `Ok` #[inline] pub fn is_ok(&self) -> bool { match *self { @@ -71,7 +70,7 @@ impl Result { } } - /// Returns true if the result is `err` + /// Returns true if the result is `Err` #[inline] pub fn is_err(&self) -> bool { !self.is_ok() @@ -80,14 +79,14 @@ impl 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 - * returned. This function can be used to compose the results of two - * functions. + * 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: * - * read_file(file).iter() { |buf| + * do read_file(file).iter |buf| { * print_buf(buf) * } */ @@ -95,84 +94,84 @@ impl Result { pub fn iter(&self, f: &fn(&T)) { match *self { Ok(ref t) => f(t), - Err(_) => () + Err(_) => (), } } /** * 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. - * This function can be used to pass through a successful result while - * handling an error. + * 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 iter_err(&self, f: &fn(&E)) { match *self { Ok(_) => (), - Err(ref e) => f(e) + Err(ref e) => f(e), } } - /// Unwraps a result, assuming it is an `ok(T)` + /// Unwraps a result, assuming it is an `Ok(T)` #[inline] pub fn unwrap(self) -> T { match self { Ok(t) => t, - Err(_) => fail!("unwrap called on an err result") + Err(_) => fail!("unwrap called on an `Err` result"), } } - /// Unwraps a result, assuming it is an `err(U)` + /// Unwraps a result, assuming it is an `Err(U)` #[inline] pub fn unwrap_err(self) -> E { match self { - Err(u) => u, - Ok(_) => fail!("unwrap called on an ok result") + Err(e) => e, + Ok(_) => fail!("unwrap called on an `Ok` 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 - * returned. This function can be used to compose the results of two - * functions. + * 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 = do read_file(file).chain |buf| { - * ok(parse_bytes(buf)) + * let res = do read_file(file) |buf| { + * Ok(parse_bytes(buf)) * }; */ #[inline] - pub fn chain(self, op: &fn(T) -> Result) -> Result { + pub fn chain(self, op: &fn(T) -> Result) -> Result { match self { Ok(t) => op(t), - Err(e) => Err(e) + Err(e) => Err(e), } } /** - * 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. This function can be used to pass through a - * successful result while handling an error. - */ + * 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 { + pub fn chain_err(self, op: &fn(E) -> Result) -> Result { match self { Ok(t) => Ok(t), - Err(v) => op(v) + Err(e) => op(e), } } } -impl Result { +impl Result { /** * Get the value out of a successful result * @@ -183,18 +182,18 @@ impl Result { #[inline] pub fn get(&self) -> T { match *self { - Ok(ref t) => (*t).clone(), - Err(ref e) => fail!("get called on error result: %?", *e), + Ok(ref t) => t.clone(), + Err(ref e) => fail!("get called on `Err` result: %?", *e), } } /** * 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. + * 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 { @@ -205,7 +204,7 @@ impl Result { } } -impl Result { +impl Result { /** * Get the value out of an error result * @@ -216,24 +215,24 @@ impl Result { #[inline] pub fn get_err(&self) -> E { match *self { - Err(ref u) => (*u).clone(), - Ok(_) => fail!("get_err called on ok result"), + Err(ref e) => e.clone(), + Ok(_) => fail!("get_err called on `Ok` result") } } /** * 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. + * If `self` is `Ok` then the value is extracted and passed to `op` + * whereupon `op`s result is wrapped in `Ok` and 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).map() { |buf| + * let res = do read_file(file).map |buf| { * parse_bytes(buf) - * }); + * }; */ #[inline] pub fn map(&self, op: &fn(&T) -> U) -> Result { @@ -254,8 +253,8 @@ impl Result { * checking for overflow: * * fn inc_conditionally(x: uint) -> result { - * if x == uint::max_value { return err("overflow"); } - * else { return ok(x+1u); } + * if x == uint::max_value { return Err("overflow"); } + * else { return Ok(x+1u); } * } * map(~[1u, 2u, 3u], inc_conditionally).chain {|incd| * assert!(incd == ~[2u, 3u, 4u]); @@ -340,7 +339,6 @@ pub fn iter_vec2(ss: &[S], ts: &[T], return Ok(()); } - #[cfg(test)] mod tests { use result::{Err, Ok, Result}; diff --git a/src/test/run-fail/result-get-fail.rs b/src/test/run-fail/result-get-fail.rs index 6e5005fe03d..782ce61cab2 100644 --- a/src/test/run-fail/result-get-fail.rs +++ b/src/test/run-fail/result-get-fail.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// error-pattern:get called on error result: ~"kitty" +// error-pattern:get called on `Err` result: ~"kitty" use std::result; -- cgit 1.4.1-3-g733a5 From 3478589b99b296b1c0014478826f03f848b536bf Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 24 Jul 2013 20:23:38 -0700 Subject: std and rustc: cleanup uses of result methods --- src/libstd/io.rs | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 0d781a1aea2..761d07da805 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -46,28 +46,26 @@ implement `Reader` and `Writer`, where appropriate. #[allow(missing_doc)]; -use result::Result; - +use cast; use clone::Clone; use container::Container; use int; -use libc; -use libc::{c_int, c_long, c_void, size_t, ssize_t}; +use iterator::IteratorUtil; use libc::consts::os::posix88::*; +use libc::{c_int, c_long, c_void, size_t, ssize_t}; +use libc; use num; +use ops::Drop; use os; -use cast; use path::Path; -use ops::Drop; -use iterator::IteratorUtil; use ptr; -use result; -use str; +use result::{Result, Ok, Err}; use str::{StrSlice, OwnedStr}; +use str; use to_str::ToStr; use uint; -use vec; use vec::{MutableVector, ImmutableVector, OwnedVector, OwnedCopyableVector, CopyableVector}; +use vec; #[allow(non_camel_case_types)] // not sure what to do about this pub type fd_t = c_int; @@ -1038,9 +1036,9 @@ pub fn file_reader(path: &Path) -> Result<@Reader, ~str> { }; if f as uint == 0u { - result::Err(~"error opening " + path.to_str()) + Err(~"error opening " + path.to_str()) } else { - result::Ok(FILE_reader(f, true)) + Ok(FILE_reader(f, true)) } } @@ -1287,10 +1285,9 @@ pub fn mk_file_writer(path: &Path, flags: &[FileFlag]) } }; if fd < (0 as c_int) { - result::Err(fmt!("error opening %s: %s", path.to_str(), - os::last_os_error())) + Err(fmt!("error opening %s: %s", path.to_str(), os::last_os_error())) } else { - result::Ok(fd_writer(fd, true)) + Ok(fd_writer(fd, true)) } } @@ -1559,7 +1556,7 @@ impl WriterUtil for T { } pub fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str> { - mk_file_writer(path, flags).chain(|w| result::Ok(w)) + mk_file_writer(path, flags).chain(|w| Ok(w)) } @@ -1572,9 +1569,9 @@ pub fn buffered_file_writer(path: &Path) -> Result<@Writer, ~str> { } }; return if f as uint == 0u { - result::Err(~"error opening " + path.to_str()) + Err(~"error opening " + path.to_str()) } else { - result::Ok(FILE_writer(f, true)) + Ok(FILE_writer(f, true)) } } } @@ -1728,9 +1725,9 @@ pub fn seek_in_buf(offset: int, pos: uint, len: uint, whence: SeekStyle) -> pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> { do read_whole_file(file).chain |bytes| { if str::is_utf8(bytes) { - result::Ok(str::from_bytes(bytes)) + Ok(str::from_bytes(bytes)) } else { - result::Err(file.to_str() + " is not UTF-8") + Err(file.to_str() + " is not UTF-8") } } } @@ -1739,7 +1736,7 @@ pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> { // abstractions is pointless. pub fn read_whole_file(file: &Path) -> Result<~[u8], ~str> { do file_reader(file).chain |rdr| { - result::Ok(rdr.read_whole_stream()) + Ok(rdr.read_whole_stream()) } } @@ -1839,6 +1836,7 @@ mod tests { use io::{BytesWriter, SeekCur, SeekEnd, SeekSet}; use io; use path::Path; + use result::{Ok, Err}; use result; use u64; use vec; @@ -1939,10 +1937,10 @@ mod tests { #[test] fn file_reader_not_exist() { match io::file_reader(&Path("not a file")) { - result::Err(e) => { + Err(e) => { assert_eq!(e, ~"error opening not a file"); } - result::Ok(_) => fail!() + Ok(_) => fail!() } } @@ -1980,20 +1978,20 @@ mod tests { #[test] fn file_writer_bad_name() { match io::file_writer(&Path("?/?"), []) { - result::Err(e) => { + Err(e) => { assert!(e.starts_with("error opening")); } - result::Ok(_) => fail!() + Ok(_) => fail!() } } #[test] fn buffered_file_writer_bad_name() { match io::buffered_file_writer(&Path("?/?")) { - result::Err(e) => { + Err(e) => { assert!(e.starts_with("error opening")); } - result::Ok(_) => fail!() + Ok(_) => fail!() } } -- cgit 1.4.1-3-g733a5 From b147d70b08f93ea0a13a5331f413d547819b2f4c Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 24 Jul 2013 20:25:18 -0700 Subject: std: cleanup imports in result::tests --- src/libstd/result.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 5919acc1125..c045c191488 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -15,7 +15,6 @@ use clone::Clone; use cmp::Eq; use either; -use either::Either; use iterator::IteratorUtil; use option::{None, Option, Some}; use vec; @@ -39,7 +38,7 @@ impl Result { * result variants are converted to `either::Left`. */ #[inline] - pub fn to_either(self)-> Either{ + pub fn to_either(self)-> either::Either{ match self { Ok(t) => either::Right(t), Err(e) => either::Left(e), @@ -341,17 +340,16 @@ pub fn iter_vec2(ss: &[S], ts: &[T], #[cfg(test)] mod tests { - use result::{Err, Ok, Result}; - use result; + use super::*; use either; - pub fn op1() -> result::Result { result::Ok(666) } + pub fn op1() -> Result { Ok(666) } - pub fn op2(i: int) -> result::Result { - result::Ok(i as uint + 1u) + pub fn op2(i: int) -> Result { + Ok(i as uint + 1u) } - pub fn op3() -> result::Result { result::Err(~"sadface") } + pub fn op3() -> Result { Err(~"sadface") } #[test] pub fn chain_success() { -- cgit 1.4.1-3-g733a5