]> git.lizzy.rs Git - rust.git/commitdiff
Make Result::{unwrap, unwrap_err} require Show
authorSteven Fackler <sfackler@gmail.com>
Sat, 12 Apr 2014 03:59:18 +0000 (20:59 -0700)
committerSteven Fackler <sfackler@gmail.com>
Mon, 14 Apr 2014 06:47:53 +0000 (23:47 -0700)
`foo.ok().unwrap()` and `foo.err().unwrap()` are the fallbacks for types
that aren't `Show`.

Closes #13379

src/librustc/middle/astencode.rs
src/libstd/comm/mod.rs
src/libstd/result.rs
src/libterm/lib.rs

index 858e873f7ed3592e83d2c3a68d13f8dc6394dfd9..49a2b6326c81d3d1646345ed60e3d7fede60960c 100644 (file)
@@ -266,7 +266,7 @@ trait def_id_encoder_helpers {
 
 impl<S:serialize::Encoder<E>, E> def_id_encoder_helpers for S {
     fn emit_def_id(&mut self, did: ast::DefId) {
-        did.encode(self).unwrap()
+        did.encode(self).ok().unwrap()
     }
 }
 
@@ -278,13 +278,13 @@ fn read_def_id_noxcx(&mut self,
 
 impl<D:serialize::Decoder<E>, E> def_id_decoder_helpers for D {
     fn read_def_id(&mut self, xcx: &ExtendedDecodeContext) -> ast::DefId {
-        let did: ast::DefId = Decodable::decode(self).unwrap();
+        let did: ast::DefId = Decodable::decode(self).ok().unwrap();
         did.tr(xcx)
     }
 
     fn read_def_id_noxcx(&mut self,
                          cdata: @cstore::crate_metadata) -> ast::DefId {
-        let did: ast::DefId = Decodable::decode(self).unwrap();
+        let did: ast::DefId = Decodable::decode(self).ok().unwrap();
         decoder::translate_def_id(cdata, did)
     }
 }
index 58781c01d662e69af43816752572978871881d3c..e2d7a869fbcdc888698e381946b1651e8e34056d 100644 (file)
@@ -496,7 +496,7 @@ pub fn send_opt(&self, t: T) -> Result<(), T> {
                                 // This send cannot fail because the task is
                                 // asleep (we're looking at it), so the receiver
                                 // can't go away.
-                                (*a.get()).send(t).unwrap();
+                                (*a.get()).send(t).ok().unwrap();
                                 task.wake().map(|t| t.reawaken());
                                 (a, Ok(()))
                             }
index 8bd36127db2d2f2913724af485a773fe7f26088e..9ca1443ea50178f0b77b70714624af5e2e2dd31a 100644 (file)
@@ -12,6 +12,7 @@
 
 use clone::Clone;
 use cmp::Eq;
+use std::fmt::Show;
 use iter::{Iterator, FromIterator};
 use option::{None, Option, Some};
 
@@ -174,46 +175,50 @@ pub fn or_else<F>(self, op: |E| -> Result<T, F>) -> Result<T, F> {
         }
     }
 
-    /////////////////////////////////////////////////////////////////////////
-    // Common special cases
-    /////////////////////////////////////////////////////////////////////////
-
     /// Unwraps a result, yielding the content of an `Ok`.
-    /// Fails if the value is an `Err`.
+    /// Else it returns `optb`.
     #[inline]
-    pub fn unwrap(self) -> T {
+    pub fn unwrap_or(self, optb: T) -> T {
         match self {
             Ok(t) => t,
-            Err(_) => fail!("called `Result::unwrap()` on an `Err` value")
+            Err(_) => optb
         }
     }
 
     /// Unwraps a result, yielding the content of an `Ok`.
-    /// Else it returns `optb`.
+    /// If the value is an `Err` then it calls `op` with its value.
     #[inline]
-    pub fn unwrap_or(self, optb: T) -> T {
+    pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
         match self {
             Ok(t) => t,
-            Err(_) => optb
+            Err(e) => op(e)
         }
     }
+}
 
+impl<T, E: Show> Result<T, E> {
     /// Unwraps a result, yielding the content of an `Ok`.
-    /// If the value is an `Err` then it calls `op` with its value.
+    ///
+    /// Fails if the value is an `Err`.
     #[inline]
-    pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
+    pub fn unwrap(self) -> T {
         match self {
             Ok(t) => t,
-            Err(e) => op(e)
+            Err(e) =>
+                fail!("called `Result::unwrap()` on an `Err` value: {}", e)
         }
     }
+}
 
+impl<T: Show, E> Result<T, E> {
     /// Unwraps a result, yielding the content of an `Err`.
+    ///
     /// Fails if the value is an `Ok`.
     #[inline]
     pub fn unwrap_err(self) -> E {
         match self {
-            Ok(_) => fail!("called `Result::unwrap_err()` on an `Ok` value"),
+            Ok(t) =>
+                fail!("called `Result::unwrap_err()` on an `Ok` value: {}", t),
             Err(e) => e
         }
     }
index a94a13aa0177c65bd8b945f5cdf2574e4155f7c8..84ba122631ecdacd368dfdc2a3a09e06ecb0ba6c 100644 (file)
@@ -132,21 +132,22 @@ pub fn new(out: T) -> Result<Terminal<T>, ~str> {
             None => return Err(~"TERM environment variable undefined")
         };
 
-        let entry = open(term);
-        if entry.is_err() {
-            if "cygwin" == term { // msys terminal
-                return Ok(Terminal {out: out, ti: msys_terminfo(), num_colors: 8});
+        let mut file = match open(term) {
+            Ok(file) => file,
+            Err(err) => {
+                if "cygwin" == term { // msys terminal
+                    return Ok(Terminal {
+                        out: out,
+                        ti: msys_terminfo(),
+                        num_colors: 8
+                    });
+                }
+                return Err(err);
             }
-            return Err(entry.unwrap_err());
-        }
+        };
 
-        let mut file = entry.unwrap();
-        let ti = parse(&mut file, false);
-        if ti.is_err() {
-            return Err(ti.unwrap_err());
-        }
+        let inf = try!(parse(&mut file, false));
 
-        let inf = ti.unwrap();
         let nc = if inf.strings.find_equiv(&("setaf")).is_some()
                  && inf.strings.find_equiv(&("setab")).is_some() {
                      inf.numbers.find_equiv(&("colors")).map_or(0, |&n| n)