Auto merge of #26416 - retep998:error-debug, r=sfackler
Fixes https://github.com/rust-lang/rust/issues/26408 Example output when using `Result::unwrap`: ``` thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Error { repr: Os { code: 2, message: "The system cannot find the file specified.\r\n" } }', src/libcore\result.rs:731 ``` This could technically be considered a breaking change for any code that depends on the format of the `Debug` output for `io::Error` but I sincerely hope nobody wrote code like that.
This commit is contained in:
commit
ecfcd2a305
1 changed files with 21 additions and 1 deletions
|
|
@ -37,7 +37,6 @@ pub struct Error {
|
|||
repr: Repr,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Repr {
|
||||
Os(i32),
|
||||
Custom(Box<Custom>),
|
||||
|
|
@ -240,6 +239,17 @@ impl Error {
|
|||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Repr {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
&Repr::Os(ref code) =>
|
||||
fmt.debug_struct("Os").field("code", code)
|
||||
.field("message", &sys::os::error_string(*code)).finish(),
|
||||
&Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
|
|
@ -282,6 +292,16 @@ mod test {
|
|||
use error;
|
||||
use error::Error as error_Error;
|
||||
use fmt;
|
||||
use sys::os::error_string;
|
||||
|
||||
#[test]
|
||||
fn test_debug_error() {
|
||||
let code = 6;
|
||||
let msg = error_string(code);
|
||||
let err = Error { repr: super::Repr::Os(code) };
|
||||
let expected = format!("Error {{ repr: Os {{ code: {:?}, message: {:?} }} }}", code, msg);
|
||||
assert_eq!(format!("{:?}", err), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_downcasting() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue