reduce size of Error
This commit is contained in:
		
							
								
								
									
										97
									
								
								src/error.rs
									
									
									
									
									
								
							
							
						
						
									
										97
									
								
								src/error.rs
									
									
									
									
									
								
							| @@ -50,16 +50,30 @@ use {StatusCode, Url}; | |||||||
| ///    } | ///    } | ||||||
| /// } | /// } | ||||||
| /// ``` | /// ``` | ||||||
| #[derive(Debug)] |  | ||||||
| pub struct Error { | pub struct Error { | ||||||
|  |     inner: Box<Inner>, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #[derive(Debug)] | ||||||
|  | struct Inner { | ||||||
|     kind: Kind, |     kind: Kind, | ||||||
|     url: Option<Url>, |     url: Option<Url>, | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| /// A `Result` alias where the `Err` case is `reqwest::Error`. | /// A `Result` alias where the `Err` case is `reqwest::Error`. | ||||||
| pub type Result<T> = ::std::result::Result<T, Error>; | pub type Result<T> = ::std::result::Result<T, Error>; | ||||||
|  |  | ||||||
| impl Error { | impl Error { | ||||||
|  |     fn new(kind: Kind, url: Option<Url>) -> Error { | ||||||
|  |         Error { | ||||||
|  |             inner: Box::new(Inner { | ||||||
|  |                 kind, | ||||||
|  |                 url, | ||||||
|  |             }), | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /// Returns a possible URL related to this error. |     /// Returns a possible URL related to this error. | ||||||
|     /// |     /// | ||||||
|     /// # Examples |     /// # Examples | ||||||
| @@ -79,12 +93,12 @@ impl Error { | |||||||
|     /// ``` |     /// ``` | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn url(&self) -> Option<&Url> { |     pub fn url(&self) -> Option<&Url> { | ||||||
|         self.url.as_ref() |         self.inner.url.as_ref() | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub(crate) fn with_url(mut self, url: Url) -> Error { |     pub(crate) fn with_url(mut self, url: Url) -> Error { | ||||||
|         debug_assert_eq!(self.url, None, "with_url overriding existing url"); |         debug_assert_eq!(self.inner.url, None, "with_url overriding existing url"); | ||||||
|         self.url = Some(url); |         self.inner.url = Some(url); | ||||||
|         self |         self | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -116,7 +130,7 @@ impl Error { | |||||||
|     /// ``` |     /// ``` | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn get_ref(&self) -> Option<&(StdError + Send + Sync + 'static)> { |     pub fn get_ref(&self) -> Option<&(StdError + Send + Sync + 'static)> { | ||||||
|         match self.kind { |         match self.inner.kind { | ||||||
|             Kind::Http(ref e) => Some(e), |             Kind::Http(ref e) => Some(e), | ||||||
|             Kind::Hyper(ref e) => Some(e), |             Kind::Hyper(ref e) => Some(e), | ||||||
|             Kind::Mime(ref e) => Some(e), |             Kind::Mime(ref e) => Some(e), | ||||||
| @@ -136,7 +150,7 @@ impl Error { | |||||||
|     /// Returns true if the error is related to HTTP. |     /// Returns true if the error is related to HTTP. | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn is_http(&self) -> bool { |     pub fn is_http(&self) -> bool { | ||||||
|         match self.kind { |         match self.inner.kind { | ||||||
|             Kind::Http(_) => true, |             Kind::Http(_) => true, | ||||||
|             Kind::Hyper(_) => true, |             Kind::Hyper(_) => true, | ||||||
|             _ => false, |             _ => false, | ||||||
| @@ -146,7 +160,7 @@ impl Error { | |||||||
|     /// Returns true if the error is serialization related. |     /// Returns true if the error is serialization related. | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn is_serialization(&self) -> bool { |     pub fn is_serialization(&self) -> bool { | ||||||
|         match self.kind { |         match self.inner.kind { | ||||||
|             Kind::Json(_) | |             Kind::Json(_) | | ||||||
|             Kind::UrlEncoded(_) => true, |             Kind::UrlEncoded(_) => true, | ||||||
|             _ => false, |             _ => false, | ||||||
| @@ -156,7 +170,7 @@ impl Error { | |||||||
|     /// Returns true if the error is from a `RedirectPolicy`. |     /// Returns true if the error is from a `RedirectPolicy`. | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn is_redirect(&self) -> bool { |     pub fn is_redirect(&self) -> bool { | ||||||
|         match self.kind { |         match self.inner.kind { | ||||||
|             Kind::TooManyRedirects | |             Kind::TooManyRedirects | | ||||||
|             Kind::RedirectLoop => true, |             Kind::RedirectLoop => true, | ||||||
|             _ => false, |             _ => false, | ||||||
| @@ -166,7 +180,7 @@ impl Error { | |||||||
|     /// Returns true if the error is from a request returning a 4xx error. |     /// Returns true if the error is from a request returning a 4xx error. | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn is_client_error(&self) -> bool { |     pub fn is_client_error(&self) -> bool { | ||||||
|         match self.kind { |         match self.inner.kind { | ||||||
|             Kind::ClientError(_) => true, |             Kind::ClientError(_) => true, | ||||||
|             _ => false, |             _ => false, | ||||||
|         } |         } | ||||||
| @@ -175,7 +189,7 @@ impl Error { | |||||||
|     /// Returns true if the error is from a request returning a 5xx error. |     /// Returns true if the error is from a request returning a 5xx error. | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn is_server_error(&self) -> bool { |     pub fn is_server_error(&self) -> bool { | ||||||
|         match self.kind { |         match self.inner.kind { | ||||||
|             Kind::ServerError(_) => true, |             Kind::ServerError(_) => true, | ||||||
|             _ => false, |             _ => false, | ||||||
|         } |         } | ||||||
| @@ -184,7 +198,7 @@ impl Error { | |||||||
|     /// Returns the status code, if the error was generated from a response. |     /// Returns the status code, if the error was generated from a response. | ||||||
|     #[inline] |     #[inline] | ||||||
|     pub fn status(&self) -> Option<StatusCode> { |     pub fn status(&self) -> Option<StatusCode> { | ||||||
|         match self.kind { |         match self.inner.kind { | ||||||
|             Kind::ClientError(code) | |             Kind::ClientError(code) | | ||||||
|             Kind::ServerError(code) => Some(code), |             Kind::ServerError(code) => Some(code), | ||||||
|             _ => None, |             _ => None, | ||||||
| @@ -192,13 +206,19 @@ impl Error { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | impl fmt::Debug for Error { | ||||||
|  |     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
|  |         fmt::Debug::fmt(&self.inner, f) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| impl fmt::Display for Error { | impl fmt::Display for Error { | ||||||
|     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
|         if let Some(ref url) = self.url { |         if let Some(ref url) = self.inner.url { | ||||||
|             try!(fmt::Display::fmt(url, f)); |             try!(fmt::Display::fmt(url, f)); | ||||||
|             try!(f.write_str(": ")); |             try!(f.write_str(": ")); | ||||||
|         } |         } | ||||||
|         match self.kind { |         match self.inner.kind { | ||||||
|             Kind::Http(ref e) => fmt::Display::fmt(e, f), |             Kind::Http(ref e) => fmt::Display::fmt(e, f), | ||||||
|             Kind::Hyper(ref e) => fmt::Display::fmt(e, f), |             Kind::Hyper(ref e) => fmt::Display::fmt(e, f), | ||||||
|             Kind::Mime(ref e) => fmt::Display::fmt(e, f), |             Kind::Mime(ref e) => fmt::Display::fmt(e, f), | ||||||
| @@ -224,7 +244,7 @@ impl fmt::Display for Error { | |||||||
|  |  | ||||||
| impl StdError for Error { | impl StdError for Error { | ||||||
|     fn description(&self) -> &str { |     fn description(&self) -> &str { | ||||||
|         match self.kind { |         match self.inner.kind { | ||||||
|             Kind::Http(ref e) => e.description(), |             Kind::Http(ref e) => e.description(), | ||||||
|             Kind::Hyper(ref e) => e.description(), |             Kind::Hyper(ref e) => e.description(), | ||||||
|             Kind::Mime(ref e) => e.description(), |             Kind::Mime(ref e) => e.description(), | ||||||
| @@ -242,7 +262,7 @@ impl StdError for Error { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn cause(&self) -> Option<&StdError> { |     fn cause(&self) -> Option<&StdError> { | ||||||
|         match self.kind { |         match self.inner.kind { | ||||||
|             Kind::Http(ref e) => e.cause(), |             Kind::Http(ref e) => e.cause(), | ||||||
|             Kind::Hyper(ref e) => e.cause(), |             Kind::Hyper(ref e) => e.cause(), | ||||||
|             Kind::Mime(ref e) => e.cause(), |             Kind::Mime(ref e) => e.cause(), | ||||||
| @@ -373,10 +393,7 @@ where | |||||||
| { | { | ||||||
|     #[inline] |     #[inline] | ||||||
|     fn from(other: InternalFrom<T>) -> Error { |     fn from(other: InternalFrom<T>) -> Error { | ||||||
|         Error { |         Error::new(other.0.into(), other.1) | ||||||
|             kind: other.0.into(), |  | ||||||
|             url: other.1, |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -388,7 +405,7 @@ where | |||||||
| } | } | ||||||
|  |  | ||||||
| pub(crate) fn into_io(e: Error) -> io::Error { | pub(crate) fn into_io(e: Error) -> io::Error { | ||||||
|     match e.kind { |     match e.inner.kind { | ||||||
|         Kind::Io(io) => io, |         Kind::Io(io) => io, | ||||||
|         _ => io::Error::new(io::ErrorKind::Other, e), |         _ => io::Error::new(io::ErrorKind::Other, e), | ||||||
|     } |     } | ||||||
| @@ -415,45 +432,27 @@ macro_rules! try_ { | |||||||
| } | } | ||||||
|  |  | ||||||
| pub(crate) fn loop_detected(url: Url) -> Error { | pub(crate) fn loop_detected(url: Url) -> Error { | ||||||
|     Error { |     Error::new(Kind::RedirectLoop, Some(url)) | ||||||
|         kind: Kind::RedirectLoop, |  | ||||||
|         url: Some(url), |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| pub(crate) fn too_many_redirects(url: Url) -> Error { | pub(crate) fn too_many_redirects(url: Url) -> Error { | ||||||
|     Error { |     Error::new(Kind::TooManyRedirects, Some(url)) | ||||||
|         kind: Kind::TooManyRedirects, |  | ||||||
|         url: Some(url), |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| pub(crate) fn timedout(url: Option<Url>) -> Error { | pub(crate) fn timedout(url: Option<Url>) -> Error { | ||||||
|     Error { |     Error::new(Kind::Io(io_timeout()), url) | ||||||
|         kind: Kind::Io(io_timeout()), |  | ||||||
|         url: url, |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| pub(crate) fn client_error(url: Url, status: StatusCode) -> Error { | pub(crate) fn client_error(url: Url, status: StatusCode) -> Error { | ||||||
|     Error { |     Error::new(Kind::ClientError(status), Some(url)) | ||||||
|         kind: Kind::ClientError(status), |  | ||||||
|         url: Some(url), |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| pub(crate) fn server_error(url: Url, status: StatusCode) -> Error { | pub(crate) fn server_error(url: Url, status: StatusCode) -> Error { | ||||||
|     Error { |     Error::new(Kind::ServerError(status), Some(url)) | ||||||
|         kind: Kind::ServerError(status), |  | ||||||
|         url: Some(url), |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| pub(crate) fn url_bad_scheme(url: Url) -> Error { | pub(crate) fn url_bad_scheme(url: Url) -> Error { | ||||||
|     Error { |     Error::new(Kind::UrlBadScheme, Some(url)) | ||||||
|         kind: Kind::UrlBadScheme, |  | ||||||
|         url: Some(url), |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| #[cfg(test)] | #[cfg(test)] | ||||||
| @@ -494,7 +493,7 @@ mod tests { | |||||||
|  |  | ||||||
|         let root = Chain(None::<Error>); |         let root = Chain(None::<Error>); | ||||||
|         let io = ::std::io::Error::new(::std::io::ErrorKind::Other, root); |         let io = ::std::io::Error::new(::std::io::ErrorKind::Other, root); | ||||||
|         let err = Error { kind: Kind::Io(io), url: None }; |         let err = Error::new(Kind::Io(io), None); | ||||||
|         assert!(err.cause().is_none()); |         assert!(err.cause().is_none()); | ||||||
|         assert_eq!(err.to_string(), "root"); |         assert_eq!(err.to_string(), "root"); | ||||||
|  |  | ||||||
| @@ -502,8 +501,14 @@ mod tests { | |||||||
|         let root = ::std::io::Error::new(::std::io::ErrorKind::Other, Chain(None::<Error>)); |         let root = ::std::io::Error::new(::std::io::ErrorKind::Other, Chain(None::<Error>)); | ||||||
|         let link = Chain(Some(root)); |         let link = Chain(Some(root)); | ||||||
|         let io = ::std::io::Error::new(::std::io::ErrorKind::Other, link); |         let io = ::std::io::Error::new(::std::io::ErrorKind::Other, link); | ||||||
|         let err = Error { kind: Kind::Io(io), url: None }; |         let err = Error::new(Kind::Io(io), None); | ||||||
|         assert!(err.cause().is_some()); |         assert!(err.cause().is_some()); | ||||||
|         assert_eq!(err.to_string(), "chain: root"); |         assert_eq!(err.to_string(), "chain: root"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     #[test] | ||||||
|  |     fn mem_size_of() { | ||||||
|  |         use std::mem::size_of; | ||||||
|  |         assert_eq!(size_of::<Error>(), size_of::<usize>()); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user