feat(http): use the bytes crate for Chunk and internally

This commit is contained in:
Sean McArthur
2017-03-01 14:14:34 -08:00
parent cf7cc50ad0
commit 65b3e08f69
14 changed files with 306 additions and 598 deletions

View File

@@ -1,27 +1,25 @@
use std::fmt;
use http::buf::MemSlice;
use bytes::Bytes;
/// A piece of a message body.
pub struct Chunk(Inner);
enum Inner {
Owned(Vec<u8>),
Mem(MemSlice),
Static(&'static [u8]),
Shared(Bytes),
}
impl From<Vec<u8>> for Chunk {
#[inline]
fn from(v: Vec<u8>) -> Chunk {
Chunk(Inner::Owned(v))
Chunk::from(Bytes::from(v))
}
}
impl From<&'static [u8]> for Chunk {
#[inline]
fn from(slice: &'static [u8]) -> Chunk {
Chunk(Inner::Static(slice))
Chunk::from(Bytes::from_static(slice))
}
}
@@ -39,9 +37,9 @@ impl From<&'static str> for Chunk {
}
}
impl From<MemSlice> for Chunk {
fn from(mem: MemSlice) -> Chunk {
Chunk(Inner::Mem(mem))
impl From<Bytes> for Chunk {
fn from(mem: Bytes) -> Chunk {
Chunk(Inner::Shared(mem))
}
}
@@ -58,9 +56,7 @@ impl AsRef<[u8]> for Chunk {
#[inline]
fn as_ref(&self) -> &[u8] {
match self.0 {
Inner::Owned(ref vec) => vec,
Inner::Mem(ref slice) => slice.as_ref(),
Inner::Static(slice) => slice,
Inner::Shared(ref slice) => slice,
}
}
}