Skip to content

Commit

Permalink
Auto merge of #21558 - alexcrichton:result-debug, r=aturon
Browse files Browse the repository at this point in the history
This commit relaxes the bound on `Result::unwrap` and `Result::unwrap_err` from
the `Display` trait to the `Debug` trait for generating an error message about
the unwrapping operation.

This commit is a breaking change and any breakage should be mitigated by
ensuring that `Debug` is implemented on the relevant type.

[breaking-change]
  • Loading branch information
bors committed Jan 25, 2015
2 parents 70b13a7 + 0824652 commit 43046be
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/libcore/result.rs
Expand Up @@ -229,7 +229,7 @@
use self::Result::{Ok, Err};

use clone::Clone;
use fmt::Display;
use fmt::Debug;
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator, ExactSizeIterator};
use ops::{FnMut, FnOnce};
use option::Option::{self, None, Some};
Expand Down Expand Up @@ -714,7 +714,7 @@ impl<T, E> Result<T, E> {
}

#[stable]
impl<T, E: Display> Result<T, E> {
impl<T, E: Debug> Result<T, E> {
/// Unwraps a result, yielding the content of an `Ok`.
///
/// # Panics
Expand All @@ -739,13 +739,13 @@ impl<T, E: Display> Result<T, E> {
match self {
Ok(t) => t,
Err(e) =>
panic!("called `Result::unwrap()` on an `Err` value: {}", e)
panic!("called `Result::unwrap()` on an `Err` value: {:?}", e)
}
}
}

#[stable]
impl<T: Display, E> Result<T, E> {
impl<T: Debug, E> Result<T, E> {
/// Unwraps a result, yielding the content of an `Err`.
///
/// # Panics
Expand All @@ -769,7 +769,7 @@ impl<T: Display, E> Result<T, E> {
pub fn unwrap_err(self) -> E {
match self {
Ok(t) =>
panic!("called `Result::unwrap_err()` on an `Ok` value: {}", t),
panic!("called `Result::unwrap_err()` on an `Ok` value: {:?}", t),
Err(e) => e
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/libstd/sync/mpsc/mod.rs
Expand Up @@ -382,7 +382,7 @@ impl<T> !Sync for SyncSender<T> {}
/// A `send` operation can only fail if the receiving end of a channel is
/// disconnected, implying that the data could never be received. The error
/// contains the data being sent as a payload so it can be recovered.
#[derive(PartialEq, Eq, Show)]
#[derive(PartialEq, Eq)]
#[stable]
pub struct SendError<T>(pub T);

Expand Down Expand Up @@ -412,7 +412,7 @@ pub enum TryRecvError {

/// This enumeration is the list of the possible error outcomes for the
/// `SyncSender::try_send` method.
#[derive(PartialEq, Clone, Show)]
#[derive(PartialEq, Clone)]
#[stable]
pub enum TrySendError<T> {
/// The data could not be sent on the channel because it would require that
Expand Down Expand Up @@ -961,13 +961,30 @@ impl<T: Send> Drop for Receiver<T> {
}
}

#[stable]
impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
"SendError(..)".fmt(f)
}
}

#[stable]
impl<T> fmt::Display for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
"sending on a closed channel".fmt(f)
}
}

#[stable]
impl<T> fmt::Debug for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
TrySendError::Full(..) => "Full(..)".fmt(f),
TrySendError::Disconnected(..) => "Disconnected(..)".fmt(f),
}
}
}

#[stable]
impl<T> fmt::Display for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
19 changes: 17 additions & 2 deletions src/libstd/sync/poison.rs
Expand Up @@ -53,15 +53,13 @@ pub struct Guard {
/// is held. The precise semantics for when a lock is poisoned is documented on
/// each lock, but once a lock is poisoned then all future acquisitions will
/// return this error.
#[derive(Show)]
#[stable]
pub struct PoisonError<T> {
guard: T,
}

/// An enumeration of possible errors which can occur while calling the
/// `try_lock` method.
#[derive(Show)]
#[stable]
pub enum TryLockError<T> {
/// The lock could not be acquired because another task failed while holding
Expand Down Expand Up @@ -92,6 +90,13 @@ pub type LockResult<Guard> = Result<Guard, PoisonError<Guard>>;
#[stable]
pub type TryLockResult<Guard> = Result<Guard, TryLockError<Guard>>;

#[stable]
impl<T> fmt::Debug for PoisonError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
"PoisonError { inner: .. }".fmt(f)
}
}

#[stable]
impl<T> fmt::Display for PoisonError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -133,6 +138,16 @@ impl<T> FromError<PoisonError<T>> for TryLockError<T> {
}
}

#[stable]
impl<T> fmt::Debug for TryLockError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
TryLockError::Poisoned(..) => "Poisoned(..)".fmt(f),
TryLockError::WouldBlock => "WouldBlock".fmt(f)
}
}
}

#[stable]
impl<T> fmt::Display for TryLockError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down

0 comments on commit 43046be

Please sign in to comment.