Skip to content

Commit 06112ab

Browse files
committed
Rollup merge of rust-lang#47120 - clarcharr:io_error_debug, r=dtolnay
Better Debug impl for io::Error. This PR includes the below changes: 1. The former impl wrapped the entire thing in `Error { repr: ... }` which was unhelpful; this has been removed. 2. The `Os` variant of `io::Error` included the code and message, but not the kind; this has been fixed. 3. The `Custom` variant of `io::Error` included a `Custom(Custom { ... })`, which is now just `Custom { ... }`. Example of previous impl: ```rust Error { repr: Custom( Custom { kind: InvalidData, error: Error { repr: Os { code: 2, message: "no such file or directory" } } } ) } ``` Example of new impl: ```rust Custom { kind: InvalidData, error: Os { code: 2, kind: NotFound, message: "no such file or directory" } } ```
2 parents 8ff449d + 52e074e commit 06112ab

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/libstd/io/error.rs

+35-8
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ pub type Result<T> = result::Result<T, Error>;
6262
/// [`Write`]: ../io/trait.Write.html
6363
/// [`Seek`]: ../io/trait.Seek.html
6464
/// [`ErrorKind`]: enum.ErrorKind.html
65-
#[derive(Debug)]
6665
#[stable(feature = "rust1", since = "1.0.0")]
6766
pub struct Error {
6867
repr: Repr,
6968
}
7069

70+
#[stable(feature = "rust1", since = "1.0.0")]
71+
impl fmt::Debug for Error {
72+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73+
fmt::Debug::fmt(&self.repr, f)
74+
}
75+
}
76+
7177
enum Repr {
7278
Os(i32),
7379
Simple(ErrorKind),
@@ -511,10 +517,12 @@ impl Error {
511517
impl fmt::Debug for Repr {
512518
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
513519
match *self {
514-
Repr::Os(ref code) =>
515-
fmt.debug_struct("Os").field("code", code)
516-
.field("message", &sys::os::error_string(*code)).finish(),
517-
Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(),
520+
Repr::Os(code) =>
521+
fmt.debug_struct("Os")
522+
.field("code", &code)
523+
.field("kind", &sys::decode_error_kind(code))
524+
.field("message", &sys::os::error_string(code)).finish(),
525+
Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
518526
Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
519527
}
520528
}
@@ -559,17 +567,36 @@ fn _assert_error_is_sync_send() {
559567

560568
#[cfg(test)]
561569
mod test {
562-
use super::{Error, ErrorKind};
570+
use super::{Error, ErrorKind, Repr, Custom};
563571
use error;
564572
use fmt;
565573
use sys::os::error_string;
574+
use sys::decode_error_kind;
566575

567576
#[test]
568577
fn test_debug_error() {
569578
let code = 6;
570579
let msg = error_string(code);
571-
let err = Error { repr: super::Repr::Os(code) };
572-
let expected = format!("Error {{ repr: Os {{ code: {:?}, message: {:?} }} }}", code, msg);
580+
let kind = decode_error_kind(code);
581+
let err = Error {
582+
repr: Repr::Custom(box Custom {
583+
kind: ErrorKind::InvalidInput,
584+
error: box Error {
585+
repr: super::Repr::Os(code)
586+
},
587+
})
588+
};
589+
let expected = format!(
590+
"Custom {{ \
591+
kind: InvalidInput, \
592+
error: Os {{ \
593+
code: {:?}, \
594+
kind: {:?}, \
595+
message: {:?} \
596+
}} \
597+
}}",
598+
code, kind, msg
599+
);
573600
assert_eq!(format!("{:?}", err), expected);
574601
}
575602

0 commit comments

Comments
 (0)