From 79a98ea05db83701b1c65bd9935f8cbbea66c084 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 27 Feb 2019 10:35:25 -0800 Subject: [PATCH] timeout: update to Box (#170) --- tower-timeout/src/lib.rs | 153 ++++++++------------------------------- 1 file changed, 29 insertions(+), 124 deletions(-) diff --git a/tower-timeout/src/lib.rs b/tower-timeout/src/lib.rs index daaec49..68bd347 100644 --- a/tower-timeout/src/lib.rs +++ b/tower-timeout/src/lib.rs @@ -12,11 +12,14 @@ extern crate tokio_timer; extern crate tower_service; use futures::{Async, Future, Poll}; -use tokio_timer::{clock, Delay, Error as TimerError}; +use tokio_timer::{clock, Delay}; use tower_service::Service; use std::time::Duration; -use std::{error, fmt}; + +use self::error::Elapsed; + +type Error = Box<::std::error::Error + Send + Sync>; /// Applies a timeout to requests. #[derive(Debug, Clone)] @@ -25,23 +28,6 @@ pub struct Timeout { timeout: Duration, } -/// Errors produced by `Timeout`. -#[derive(Debug)] -pub struct Error(Kind); - -/// Timeout error variants -#[derive(Debug)] -enum Kind { - /// Inner value returned an error - Inner(T), - - /// The timeout elapsed. - Elapsed, - - /// Timer returned an error. - Timer(TimerError), -} - /// `Timeout` response future #[derive(Debug)] pub struct ResponseFuture { @@ -61,13 +47,14 @@ impl Timeout { impl Service for Timeout where S: Service, + S::Error: Into, { type Response = S::Response; - type Error = Error; + type Error = Error; type Future = ResponseFuture; fn poll_ready(&mut self) -> Poll<(), Self::Error> { - self.inner.poll_ready().map_err(|e| Error(Kind::Inner(e))) + self.inner.poll_ready().map_err(Into::into) } fn call(&mut self, request: Request) -> Self::Future { @@ -83,123 +70,41 @@ where impl Future for ResponseFuture where T: Future, + T::Error: Into, { type Item = T::Item; - type Error = Error; + type Error = Error; fn poll(&mut self) -> Poll { // First, try polling the future - match self.response.poll() { - Ok(Async::Ready(v)) => return Ok(Async::Ready(v)), - Ok(Async::NotReady) => {} - Err(e) => return Err(Error(Kind::Inner(e))), + match self.response.poll().map_err(Into::into)? { + Async::Ready(v) => return Ok(Async::Ready(v)), + Async::NotReady => {} } // Now check the sleep - match self.sleep.poll() { - Ok(Async::NotReady) => Ok(Async::NotReady), - Ok(Async::Ready(_)) => Err(Error(Kind::Elapsed)), - Err(e) => Err(Error(Kind::Timer(e))), + match self.sleep.poll()? { + Async::NotReady => Ok(Async::NotReady), + Async::Ready(_) => Err(Elapsed(()).into()), } } } // ===== impl Error ===== -impl fmt::Display for Error -where - T: fmt::Display, -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self.0 { - Kind::Inner(ref why) => fmt::Display::fmt(why, f), - Kind::Elapsed => f.pad("request timed out"), - Kind::Timer(ref why) => fmt::Display::fmt(why, f), - } - } -} - -impl error::Error for Error -where - T: error::Error + 'static, -{ - fn description(&self) -> &str { - match self.0 { - Kind::Inner(ref e) => e.description(), - Kind::Elapsed => "request timed out", - Kind::Timer(ref e) => e.description(), - } - } - - fn source(&self) -> Option<&(error::Error + 'static)> { - let kind = &self.0; - if let Kind::Inner(ref why) = kind { - Some(why) - } else { - None - } - } -} - -// ===== impl Error ===== - -impl Error { - /// Create a new `Error` representing the inner value completing with `Err`. - pub fn inner(err: T) -> Error { - Error(Kind::Inner(err)) - } - - /// Returns `true` if the error was caused by the inner value completing - /// with `Err`. - pub fn is_inner(&self) -> bool { - match self.0 { - Kind::Inner(_) => true, - _ => false, - } - } - - /// Consumes `self`, returning the inner future error. - pub fn into_inner(self) -> Option { - match self.0 { - Kind::Inner(err) => Some(err), - _ => None, - } - } - - /// Create a new `Error` representing the inner value not completing before - /// the deadline is reached. - pub fn elapsed() -> Error { - Error(Kind::Elapsed) - } - - /// Returns `true` if the error was caused by the inner value not completing - /// before the deadline is reached. - pub fn is_elapsed(&self) -> bool { - match self.0 { - Kind::Elapsed => true, - _ => false, - } - } - - /// Creates a new `Error` representing an error encountered by the timer - /// implementation - pub fn timer(err: TimerError) -> Error { - Error(Kind::Timer(err)) - } - - /// Returns `true` if the error was caused by the timer. - pub fn is_timer(&self) -> bool { - match self.0 { - Kind::Timer(_) => true, - _ => false, - } - } - - /// Consumes `self`, returning the error raised by the timer implementation. - pub fn into_timer(self) -> Option { - match self.0 { - Kind::Timer(err) => Some(err), - _ => None, +/// Timeout error types +pub mod error { + use std::{error::Error, fmt}; + + /// The timeout elapsed. + #[derive(Debug)] + pub struct Elapsed(pub(super) ()); + + impl fmt::Display for Elapsed { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.pad("request timed out") } } + + impl Error for Elapsed {} }