refactor(chunk): make use of Bytes::extend instead of custom code
This commit is contained in:
		| @@ -20,7 +20,7 @@ include = [ | |||||||
|  |  | ||||||
| [dependencies] | [dependencies] | ||||||
| base64 = "0.5" | base64 = "0.5" | ||||||
| bytes = "0.4" | bytes = "0.4.4" | ||||||
| futures = "0.1.11" | futures = "0.1.11" | ||||||
| futures-cpupool = "0.1" | futures-cpupool = "0.1" | ||||||
| httparse = "1.0" | httparse = "1.0" | ||||||
|   | |||||||
| @@ -1,41 +1,13 @@ | |||||||
| use std::fmt; | use std::fmt; | ||||||
| use std::mem; | //use std::mem; | ||||||
|  |  | ||||||
| use bytes::{Bytes, BytesMut, BufMut}; | use bytes::Bytes; | ||||||
|  |  | ||||||
| /// A piece of a message body. | /// A piece of a message body. | ||||||
| pub struct Chunk(Inner); | pub struct Chunk(Inner); | ||||||
|  |  | ||||||
| enum Inner { | enum Inner { | ||||||
|     Mut(BytesMut), |  | ||||||
|     Shared(Bytes), |     Shared(Bytes), | ||||||
|     Swapping, |  | ||||||
| } |  | ||||||
|  |  | ||||||
| impl Inner { |  | ||||||
|     fn as_bytes_mut(&mut self, reserve: usize) -> &mut BytesMut { |  | ||||||
|         match *self { |  | ||||||
|             Inner::Mut(ref mut bytes) => return bytes, |  | ||||||
|             _ => () |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         let bytes = match mem::replace(self, Inner::Swapping) { |  | ||||||
|             Inner::Shared(bytes) => bytes, |  | ||||||
|             _ => unreachable!(), |  | ||||||
|         }; |  | ||||||
|  |  | ||||||
|         let bytes_mut = bytes.try_mut().unwrap_or_else(|bytes| { |  | ||||||
|             let mut bytes_mut = BytesMut::with_capacity(reserve + bytes.len()); |  | ||||||
|             bytes_mut.put_slice(bytes.as_ref()); |  | ||||||
|             bytes_mut |  | ||||||
|         }); |  | ||||||
|  |  | ||||||
|         *self = Inner::Mut(bytes_mut); |  | ||||||
|         match *self { |  | ||||||
|             Inner::Mut(ref mut bytes) => bytes, |  | ||||||
|             _ => unreachable!(), |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| impl From<Vec<u8>> for Chunk { | impl From<Vec<u8>> for Chunk { | ||||||
| @@ -67,17 +39,17 @@ impl From<&'static str> for Chunk { | |||||||
| } | } | ||||||
|  |  | ||||||
| impl From<Bytes> for Chunk { | impl From<Bytes> for Chunk { | ||||||
|  |     #[inline] | ||||||
|     fn from(mem: Bytes) -> Chunk { |     fn from(mem: Bytes) -> Chunk { | ||||||
|         Chunk(Inner::Shared(mem)) |         Chunk(Inner::Shared(mem)) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| impl From<Chunk> for Bytes { | impl From<Chunk> for Bytes { | ||||||
|  |     #[inline] | ||||||
|     fn from(chunk: Chunk) -> Bytes { |     fn from(chunk: Chunk) -> Bytes { | ||||||
|         match chunk.0 { |         match chunk.0 { | ||||||
|             Inner::Mut(bytes_mut) => bytes_mut.freeze(), |  | ||||||
|             Inner::Shared(bytes) => bytes, |             Inner::Shared(bytes) => bytes, | ||||||
|             Inner::Swapping => unreachable!(), |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -95,9 +67,7 @@ impl AsRef<[u8]> for Chunk { | |||||||
|     #[inline] |     #[inline] | ||||||
|     fn as_ref(&self) -> &[u8] { |     fn as_ref(&self) -> &[u8] { | ||||||
|         match self.0 { |         match self.0 { | ||||||
|             Inner::Mut(ref slice) => slice, |  | ||||||
|             Inner::Shared(ref slice) => slice, |             Inner::Shared(ref slice) => slice, | ||||||
|             Inner::Swapping => unreachable!(), |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -120,19 +90,19 @@ impl IntoIterator for Chunk { | |||||||
|     type Item = u8; |     type Item = u8; | ||||||
|     type IntoIter = <Bytes as IntoIterator>::IntoIter; |     type IntoIter = <Bytes as IntoIterator>::IntoIter; | ||||||
|  |  | ||||||
|  |     #[inline] | ||||||
|     fn into_iter(self) -> Self::IntoIter { |     fn into_iter(self) -> Self::IntoIter { | ||||||
|         match self.0 { |         match self.0 { | ||||||
|             Inner::Mut(bytes) => bytes.freeze().into_iter(), |  | ||||||
|             Inner::Shared(bytes) => bytes.into_iter(), |             Inner::Shared(bytes) => bytes.into_iter(), | ||||||
|             Inner::Swapping => unreachable!(), |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| impl Extend<u8> for Chunk { | impl Extend<u8> for Chunk { | ||||||
|  |     #[inline] | ||||||
|     fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item=u8> { |     fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item=u8> { | ||||||
|         let iter = iter.into_iter(); |         match self.0 { | ||||||
|  |             Inner::Shared(ref mut bytes) => bytes.extend(iter) | ||||||
|         self.0.as_bytes_mut(iter.size_hint().0).extend(iter); |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user