From e308167a2fe558924fcebcc99358c057ae0b90b4 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 26 Jul 2013 18:03:57 -0700 Subject: [PATCH] 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(-) diff --git a/src/libextra/workcache.rs b/src/libextra/workcache.rs index 42210d0cd895..1cabab713b82 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 05a5184ccbac..fcc53c33a5de 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 809244af12a0..ec2715fcf2e0 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 {