From 5be6d26662cda447999b6adcb9b34688a3267026 Mon Sep 17 00:00:00 2001 From: Nathaniel McCallum Date: Sat, 4 Jul 2020 17:38:24 -0400 Subject: [PATCH] impl From for std::io::Error This enables the question mark operator to be used in places where std::io::Error is expected. --- src/error.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/error.rs b/src/error.rs index b1a6a459..8205c0d3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -245,6 +245,26 @@ impl From for Error { } } +#[cfg(feature = "std")] +impl From for io::Error { + fn from(e: Error) -> Self { + use std::error::Error; + + let kind = match e.classify() { + Category::Syntax => io::ErrorKind::InvalidInput, + Category::Data => io::ErrorKind::InvalidData, + Category::Eof => io::ErrorKind::UnexpectedEof, + Category::Io => e + .source() + .and_then(|e| e.downcast_ref::()) + .map(|e| e.kind()) + .unwrap_or(io::ErrorKind::Other), + }; + + Self::new(kind, e) + } +} + #[cfg(not(feature = "std"))] impl From for Error { fn from(_: core::fmt::Error) -> Error {