From e40cc33e529444e30a50ce4acc79a25e53c1110c Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Fri, 5 Oct 2018 13:03:00 -0700 Subject: [PATCH] fix unreachable code if polling async Body created from a single buffer --- src/async_impl/body.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/async_impl/body.rs b/src/async_impl/body.rs index 0b5b48a..da1798e 100644 --- a/src/async_impl/body.rs +++ b/src/async_impl/body.rs @@ -1,4 +1,4 @@ -use std::fmt; +use std::{fmt, mem}; use futures::{Stream, Poll, Async}; use bytes::{Buf, Bytes}; @@ -17,7 +17,19 @@ enum Inner { impl Body { fn poll_inner(&mut self) -> &mut ::hyper::Body { match self.inner { - Inner::Hyper(ref mut body) => body, + Inner::Hyper(ref mut body) => return body, + Inner::Reusable(_) => (), + } + + let bytes = match mem::replace(&mut self.inner, Inner::Reusable(Bytes::new())) { + Inner::Reusable(bytes) => bytes, + Inner::Hyper(_) => unreachable!(), + }; + + self.inner = Inner::Hyper(bytes.into()); + + match self.inner { + Inner::Hyper(ref mut body) => return body, Inner::Reusable(_) => unreachable!(), } } @@ -56,8 +68,7 @@ impl Body { Inner::Reusable(chunk) => (Some(chunk.clone()), chunk.into()), Inner::Hyper(b) => (None, b), } -} - + } } impl Stream for Body {