Make Result::{unwrap, unwrap_err} require Show
`foo.ok().unwrap()` and `foo.err().unwrap()` are the fallbacks for types that aren't `Show`. Closes #13379
This commit is contained in:
parent
bb9b2e0ebe
commit
eb0473df93
4 changed files with 37 additions and 31 deletions
|
@ -266,7 +266,7 @@ trait def_id_encoder_helpers {
|
||||||
|
|
||||||
impl<S:serialize::Encoder<E>, E> def_id_encoder_helpers for S {
|
impl<S:serialize::Encoder<E>, E> def_id_encoder_helpers for S {
|
||||||
fn emit_def_id(&mut self, did: ast::DefId) {
|
fn emit_def_id(&mut self, did: ast::DefId) {
|
||||||
did.encode(self).unwrap()
|
did.encode(self).ok().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,13 +278,13 @@ trait def_id_decoder_helpers {
|
||||||
|
|
||||||
impl<D:serialize::Decoder<E>, E> def_id_decoder_helpers for D {
|
impl<D:serialize::Decoder<E>, E> def_id_decoder_helpers for D {
|
||||||
fn read_def_id(&mut self, xcx: &ExtendedDecodeContext) -> ast::DefId {
|
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)
|
did.tr(xcx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_def_id_noxcx(&mut self,
|
fn read_def_id_noxcx(&mut self,
|
||||||
cdata: @cstore::crate_metadata) -> ast::DefId {
|
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)
|
decoder::translate_def_id(cdata, did)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -496,7 +496,7 @@ impl<T: Send> Sender<T> {
|
||||||
// This send cannot fail because the task is
|
// This send cannot fail because the task is
|
||||||
// asleep (we're looking at it), so the receiver
|
// asleep (we're looking at it), so the receiver
|
||||||
// can't go away.
|
// can't go away.
|
||||||
(*a.get()).send(t).unwrap();
|
(*a.get()).send(t).ok().unwrap();
|
||||||
task.wake().map(|t| t.reawaken());
|
task.wake().map(|t| t.reawaken());
|
||||||
(a, Ok(()))
|
(a, Ok(()))
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
use clone::Clone;
|
use clone::Clone;
|
||||||
use cmp::Eq;
|
use cmp::Eq;
|
||||||
|
use std::fmt::Show;
|
||||||
use iter::{Iterator, FromIterator};
|
use iter::{Iterator, FromIterator};
|
||||||
use option::{None, Option, Some};
|
use option::{None, Option, Some};
|
||||||
|
|
||||||
|
@ -174,20 +175,6 @@ impl<T, E> Result<T, E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////
|
|
||||||
// Common special cases
|
|
||||||
/////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
/// Unwraps a result, yielding the content of an `Ok`.
|
|
||||||
/// Fails if the value is an `Err`.
|
|
||||||
#[inline]
|
|
||||||
pub fn unwrap(self) -> T {
|
|
||||||
match self {
|
|
||||||
Ok(t) => t,
|
|
||||||
Err(_) => fail!("called `Result::unwrap()` on an `Err` value")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Unwraps a result, yielding the content of an `Ok`.
|
/// Unwraps a result, yielding the content of an `Ok`.
|
||||||
/// Else it returns `optb`.
|
/// Else it returns `optb`.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -207,13 +194,31 @@ impl<T, E> Result<T, E> {
|
||||||
Err(e) => op(e)
|
Err(e) => op(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, E: Show> Result<T, E> {
|
||||||
|
/// Unwraps a result, yielding the content of an `Ok`.
|
||||||
|
///
|
||||||
|
/// Fails if the value is an `Err`.
|
||||||
|
#[inline]
|
||||||
|
pub fn unwrap(self) -> T {
|
||||||
|
match self {
|
||||||
|
Ok(t) => t,
|
||||||
|
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`.
|
/// Unwraps a result, yielding the content of an `Err`.
|
||||||
|
///
|
||||||
/// Fails if the value is an `Ok`.
|
/// Fails if the value is an `Ok`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn unwrap_err(self) -> E {
|
pub fn unwrap_err(self) -> E {
|
||||||
match self {
|
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
|
Err(e) => e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -132,21 +132,22 @@ impl<T: Writer> Terminal<T> {
|
||||||
None => return Err(~"TERM environment variable undefined")
|
None => return Err(~"TERM environment variable undefined")
|
||||||
};
|
};
|
||||||
|
|
||||||
let entry = open(term);
|
let mut file = match open(term) {
|
||||||
if entry.is_err() {
|
Ok(file) => file,
|
||||||
if "cygwin" == term { // msys terminal
|
Err(err) => {
|
||||||
return Ok(Terminal {out: out, ti: msys_terminfo(), num_colors: 8});
|
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 inf = try!(parse(&mut file, false));
|
||||||
let ti = parse(&mut file, false);
|
|
||||||
if ti.is_err() {
|
|
||||||
return Err(ti.unwrap_err());
|
|
||||||
}
|
|
||||||
|
|
||||||
let inf = ti.unwrap();
|
|
||||||
let nc = if inf.strings.find_equiv(&("setaf")).is_some()
|
let nc = if inf.strings.find_equiv(&("setaf")).is_some()
|
||||||
&& inf.strings.find_equiv(&("setab")).is_some() {
|
&& inf.strings.find_equiv(&("setab")).is_some() {
|
||||||
inf.numbers.find_equiv(&("colors")).map_or(0, |&n| n)
|
inf.numbers.find_equiv(&("colors")).map_or(0, |&n| n)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue