Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Debug impl for io::Error #26416

Merged
merged 2 commits into from Jun 21, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/libstd/io/error.rs
Expand Up @@ -37,7 +37,6 @@ pub struct Error {
repr: Repr,
}

#[derive(Debug)]
enum Repr {
Os(i32),
Custom(Box<Custom>),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down