From cd0e4b3c2e737a3905bdc01080185843159227d0 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 20 Feb 2019 12:54:46 -0800 Subject: [PATCH] Add `Error::is_timeout()` accessor --- src/error.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/error.rs b/src/error.rs index b367619..912567a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -163,6 +163,23 @@ impl Error { } } + /// Returns true if the error is related to a timeout. + pub fn is_timeout(&self) -> bool { + match self.inner.kind { + Kind::Io(ref io) => io.kind() == io::ErrorKind::TimedOut, + Kind::Hyper(ref error) => { + error + .cause2() + .and_then(|cause| { + cause.downcast_ref::() + }) + .map(|io| io.kind() == io::ErrorKind::TimedOut) + .unwrap_or(false) + }, + _ => false, + } + } + /// Returns true if the error is serialization related. #[inline] pub fn is_serialization(&self) -> bool { @@ -411,12 +428,6 @@ where T: Into { } } -#[cfg(unix)] -fn io_timeout() -> io::Error { - io::Error::new(io::ErrorKind::WouldBlock, "timed out") -} - -#[cfg(windows)] fn io_timeout() -> io::Error { io::Error::new(io::ErrorKind::TimedOut, "timed out") }