From e7d61639e4d2a950eecb46dc28b73183708d517e Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Thu, 27 Jul 2017 11:42:01 -0700 Subject: [PATCH] response: inline most methods, customize Debug --- src/response.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/response.rs b/src/response.rs index 28c0cbf..9278aa4 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,6 +1,7 @@ //! HTTP response types. use std::any::Any; +use std::fmt; use {Error, Result, HttpTryFrom, Extensions}; use header::{HeaderMap, HeaderValue}; @@ -14,7 +15,6 @@ use version::Version; /// component is generic, enabling arbitrary types to represent the HTTP body. /// For example, the body could be `Vec`, a `Stream` of byte chunks, or a /// value that has been deserialized. -#[derive(Debug)] pub struct Response { head: Parts, body: T, @@ -67,6 +67,7 @@ impl Response<()> { /// .body(()) /// .unwrap(); /// ``` + #[inline] pub fn builder() -> Builder { Builder::new() } @@ -87,6 +88,7 @@ impl Response { /// assert_eq!(request.status(), status::OK); /// assert_eq!(*request.body(), "hello world"); /// ``` + #[inline] pub fn new(body: T) -> Response { Response { head: Parts::new(), @@ -109,6 +111,7 @@ impl Response { /// assert_eq!(response.status(), status::BAD_REQUEST); /// assert_eq!(*response.body(), "hello world"); /// ``` + #[inline] pub fn from_parts(parts: Parts, body: T) -> Response { Response { head: parts, @@ -125,6 +128,7 @@ impl Response { /// let response: Response<()> = Response::default(); /// assert_eq!(response.status(), status::OK); /// ``` + #[inline] pub fn status(&self) -> StatusCode { self.head.status } @@ -139,6 +143,7 @@ impl Response { /// *response.status_mut() = status::CREATED; /// assert_eq!(response.status(), status::CREATED); /// ``` + #[inline] pub fn status_mut(&mut self) -> &mut StatusCode { &mut self.head.status } @@ -152,6 +157,7 @@ impl Response { /// let response: Response<()> = Response::default(); /// assert_eq!(response.version(), version::HTTP_11); /// ``` + #[inline] pub fn version(&self) -> Version { self.head.version } @@ -166,6 +172,7 @@ impl Response { /// *response.version_mut() = version::HTTP_2; /// assert_eq!(response.version(), version::HTTP_2); /// ``` + #[inline] pub fn version_mut(&mut self) -> &mut Version { &mut self.head.version } @@ -179,6 +186,7 @@ impl Response { /// let response: Response<()> = Response::default(); /// assert!(response.headers().is_empty()); /// ``` + #[inline] pub fn headers(&self) -> &HeaderMap { &self.head.headers } @@ -194,6 +202,7 @@ impl Response { /// response.headers_mut().insert("hello", HeaderValue::from_static("world")); /// assert!(!response.headers().is_empty()); /// ``` + #[inline] pub fn headers_mut(&mut self) -> &mut HeaderMap { &mut self.head.headers } @@ -207,6 +216,7 @@ impl Response { /// let response: Response<()> = Response::default(); /// assert!(response.extensions().get::().is_none()); /// ``` + #[inline] pub fn extensions(&self) -> &Extensions { &self.head.extensions } @@ -222,6 +232,7 @@ impl Response { /// response.extensions_mut().insert("hello"); /// assert_eq!(response.extensions().get(), Some(&"hello")); /// ``` + #[inline] pub fn extensions_mut(&mut self) -> &mut Extensions { &mut self.head.extensions } @@ -235,6 +246,7 @@ impl Response { /// let response: Response = Response::default(); /// assert!(response.body().is_empty()); /// ``` + #[inline] pub fn body(&self) -> &T { &self.body } @@ -249,6 +261,7 @@ impl Response { /// response.body_mut().push_str("hello world"); /// assert!(!response.body().is_empty()); /// ``` + #[inline] pub fn body_mut(&mut self) -> &mut T { &mut self.body } @@ -263,17 +276,31 @@ impl Response { /// let (parts, body) = response.into_parts(); /// assert_eq!(parts.status, status::OK); /// ``` + #[inline] pub fn into_parts(self) -> (Parts, T) { (self.head, self.body) } } impl Default for Response { + #[inline] fn default() -> Response { Response::new(T::default()) } } +impl fmt::Debug for Response { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Response") + .field("status", &self.status()) + .field("version", &self.version()) + .field("headers", self.headers()) + // omits Extensions because not useful + .field("body", self.body()) + .finish() + } +} + impl Parts { /// Creates a new default instance of `Parts` fn new() -> Parts { @@ -301,6 +328,7 @@ impl Builder { /// .body(()) /// .unwrap(); /// ``` + #[inline] pub fn new() -> Builder { Builder::default() } @@ -501,6 +529,7 @@ fn head<'a>(head: &'a mut Option, err: &Option) } impl Default for Builder { + #[inline] fn default() -> Builder { Builder { head: Some(Parts::new()),