Skip to content

Commit 1cbce31

Browse files
committed
Use std::io::Error for Debug and Display when it is available.
1 parent f8a69c5 commit 1cbce31

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/error.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(feature = "std")]
2+
extern crate std;
3+
14
use core::{fmt, num::NonZeroU32};
25

36
/// A small and `no_std` compatible error type
@@ -103,6 +106,8 @@ impl fmt::Debug for Error {
103106
let mut dbg = f.debug_struct("Error");
104107
if let Some(errno) = self.raw_os_error() {
105108
dbg.field("os_error", &errno);
109+
#[cfg(feature = "std")]
110+
dbg.field("description", &std::io::Error::from_raw_os_error(errno));
106111
} else if let Some(desc) = internal_desc(*self) {
107112
dbg.field("internal_code", &self.0.get());
108113
dbg.field("description", &desc);
@@ -116,7 +121,13 @@ impl fmt::Debug for Error {
116121
impl fmt::Display for Error {
117122
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118123
if let Some(errno) = self.raw_os_error() {
119-
write!(f, "OS Error: {}", errno)
124+
cfg_if! {
125+
if #[cfg(feature = "std")] {
126+
std::io::Error::from_raw_os_error(errno).fmt(f)
127+
} else {
128+
write!(f, "OS Error: {}", errno)
129+
}
130+
}
120131
} else if let Some(desc) = internal_desc(*self) {
121132
f.write_str(desc)
122133
} else {

0 commit comments

Comments
 (0)