Skip to content

Commit 40a9797

Browse files
committed
auto merge of #13479 : sfackler/rust/result-unwrap, r=cmr
`foo.ok().unwrap()` and `foo.err().unwrap()` are the fallbacks for types that aren't `Show`. Closes #13379
2 parents bb9b2e0 + eb0473d commit 40a9797

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

src/librustc/middle/astencode.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ trait def_id_encoder_helpers {
266266

267267
impl<S:serialize::Encoder<E>, E> def_id_encoder_helpers for S {
268268
fn emit_def_id(&mut self, did: ast::DefId) {
269-
did.encode(self).unwrap()
269+
did.encode(self).ok().unwrap()
270270
}
271271
}
272272

@@ -278,13 +278,13 @@ trait def_id_decoder_helpers {
278278

279279
impl<D:serialize::Decoder<E>, E> def_id_decoder_helpers for D {
280280
fn read_def_id(&mut self, xcx: &ExtendedDecodeContext) -> ast::DefId {
281-
let did: ast::DefId = Decodable::decode(self).unwrap();
281+
let did: ast::DefId = Decodable::decode(self).ok().unwrap();
282282
did.tr(xcx)
283283
}
284284

285285
fn read_def_id_noxcx(&mut self,
286286
cdata: @cstore::crate_metadata) -> ast::DefId {
287-
let did: ast::DefId = Decodable::decode(self).unwrap();
287+
let did: ast::DefId = Decodable::decode(self).ok().unwrap();
288288
decoder::translate_def_id(cdata, did)
289289
}
290290
}

src/libstd/comm/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl<T: Send> Sender<T> {
496496
// This send cannot fail because the task is
497497
// asleep (we're looking at it), so the receiver
498498
// can't go away.
499-
(*a.get()).send(t).unwrap();
499+
(*a.get()).send(t).ok().unwrap();
500500
task.wake().map(|t| t.reawaken());
501501
(a, Ok(()))
502502
}

src/libstd/result.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
use clone::Clone;
1414
use cmp::Eq;
15+
use std::fmt::Show;
1516
use iter::{Iterator, FromIterator};
1617
use option::{None, Option, Some};
1718

@@ -174,46 +175,50 @@ impl<T, E> Result<T, E> {
174175
}
175176
}
176177

177-
/////////////////////////////////////////////////////////////////////////
178-
// Common special cases
179-
/////////////////////////////////////////////////////////////////////////
180-
181178
/// Unwraps a result, yielding the content of an `Ok`.
182-
/// Fails if the value is an `Err`.
179+
/// Else it returns `optb`.
183180
#[inline]
184-
pub fn unwrap(self) -> T {
181+
pub fn unwrap_or(self, optb: T) -> T {
185182
match self {
186183
Ok(t) => t,
187-
Err(_) => fail!("called `Result::unwrap()` on an `Err` value")
184+
Err(_) => optb
188185
}
189186
}
190187

191188
/// Unwraps a result, yielding the content of an `Ok`.
192-
/// Else it returns `optb`.
189+
/// If the value is an `Err` then it calls `op` with its value.
193190
#[inline]
194-
pub fn unwrap_or(self, optb: T) -> T {
191+
pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
195192
match self {
196193
Ok(t) => t,
197-
Err(_) => optb
194+
Err(e) => op(e)
198195
}
199196
}
197+
}
200198

199+
impl<T, E: Show> Result<T, E> {
201200
/// Unwraps a result, yielding the content of an `Ok`.
202-
/// If the value is an `Err` then it calls `op` with its value.
201+
///
202+
/// Fails if the value is an `Err`.
203203
#[inline]
204-
pub fn unwrap_or_handle(self, op: |E| -> T) -> T {
204+
pub fn unwrap(self) -> T {
205205
match self {
206206
Ok(t) => t,
207-
Err(e) => op(e)
207+
Err(e) =>
208+
fail!("called `Result::unwrap()` on an `Err` value: {}", e)
208209
}
209210
}
211+
}
210212

213+
impl<T: Show, E> Result<T, E> {
211214
/// Unwraps a result, yielding the content of an `Err`.
215+
///
212216
/// Fails if the value is an `Ok`.
213217
#[inline]
214218
pub fn unwrap_err(self) -> E {
215219
match self {
216-
Ok(_) => fail!("called `Result::unwrap_err()` on an `Ok` value"),
220+
Ok(t) =>
221+
fail!("called `Result::unwrap_err()` on an `Ok` value: {}", t),
217222
Err(e) => e
218223
}
219224
}

src/libterm/lib.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,22 @@ impl<T: Writer> Terminal<T> {
132132
None => return Err(~"TERM environment variable undefined")
133133
};
134134

135-
let entry = open(term);
136-
if entry.is_err() {
137-
if "cygwin" == term { // msys terminal
138-
return Ok(Terminal {out: out, ti: msys_terminfo(), num_colors: 8});
135+
let mut file = match open(term) {
136+
Ok(file) => file,
137+
Err(err) => {
138+
if "cygwin" == term { // msys terminal
139+
return Ok(Terminal {
140+
out: out,
141+
ti: msys_terminfo(),
142+
num_colors: 8
143+
});
144+
}
145+
return Err(err);
139146
}
140-
return Err(entry.unwrap_err());
141-
}
147+
};
142148

143-
let mut file = entry.unwrap();
144-
let ti = parse(&mut file, false);
145-
if ti.is_err() {
146-
return Err(ti.unwrap_err());
147-
}
149+
let inf = try!(parse(&mut file, false));
148150

149-
let inf = ti.unwrap();
150151
let nc = if inf.strings.find_equiv(&("setaf")).is_some()
151152
&& inf.strings.find_equiv(&("setab")).is_some() {
152153
inf.numbers.find_equiv(&("colors")).map_or(0, |&n| n)

0 commit comments

Comments
 (0)