From 280f2b4f6565fc1db51100701353a5ddeaeb4e1c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 26 Jul 2017 13:00:42 -0700 Subject: [PATCH] Panic on reuse of builders --- src/request.rs | 12 +++++++----- src/response.rs | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/request.rs b/src/request.rs index b703990..f644028 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,7 +1,6 @@ //! HTTP request types. use std::any::Any; -use std::io; use {Uri, Error, Result, HttpTryFrom, Extensions}; use header::{HeaderMap, HeaderValue, HeaderMapKey}; @@ -511,13 +510,11 @@ impl Builder { /// "Consumes" this builder, returning the constructed `Head`. fn head(&mut self) -> Result { + let ret = self.head.take().expect("cannot reuse request builder"); if let Some(e) = self.err.take() { return Err(e) } - self.head.take().ok_or_else(|| { - io::Error::new(io::ErrorKind::Other, "cannot reuse `Builder`") - .into() - }) + Ok(ret) } /// "Consumes" this builder, using the provided `body` to return a @@ -531,6 +528,11 @@ impl Builder { /// "Bar\r\n")` the error will be returned when this function is called /// rather than when `header` was called. /// + /// # Panics + /// + /// This method will panic if the builder is reused. The `body` function can + /// only be called once. + /// /// # Examples /// /// ``` diff --git a/src/response.rs b/src/response.rs index 621b6df..27f6feb 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,7 +1,6 @@ //! HTTP response types. use std::any::Any; -use std::io; use {Error, Result, HttpTryFrom, Extensions}; use header::{HeaderMap, HeaderValue}; @@ -453,13 +452,11 @@ impl Builder { /// "Consumes" this builder, returning the constructed `Parts`. fn head(&mut self) -> Result { + let ret = self.head.take().expect("cannot reuse response builder"); if let Some(e) = self.err.take() { return Err(e) } - self.head.take().ok_or_else(|| { - io::Error::new(io::ErrorKind::Other, "cannot reuse `Builder`") - .into() - }) + Ok(ret) } /// "Consumes" this builder, using the provided `body` to return a @@ -473,6 +470,11 @@ impl Builder { /// "Bar\r\n")` the error will be returned when this function is called /// rather than when `header` was called. /// + /// # Panics + /// + /// This method will panic if the builder is reused. The `body` function can + /// only be called once. + /// /// # Examples /// /// ```