Use constructors to create Bytes and ByteBuf

This commit is contained in:
David Tolnay
2016-09-05 08:09:23 -07:00
parent fb7ba225d1
commit 08bc2d2e76
+8 -24
View File
@@ -40,18 +40,14 @@ impl<'a> fmt::Debug for Bytes<'a> {
impl<'a> From<&'a [u8]> for Bytes<'a> { impl<'a> From<&'a [u8]> for Bytes<'a> {
fn from(bytes: &'a [u8]) -> Self { fn from(bytes: &'a [u8]) -> Self {
Bytes { Bytes::new(bytes)
bytes: bytes,
}
} }
} }
#[cfg(any(feature = "std", feature = "collections"))] #[cfg(any(feature = "std", feature = "collections"))]
impl<'a> From<&'a Vec<u8>> for Bytes<'a> { impl<'a> From<&'a Vec<u8>> for Bytes<'a> {
fn from(bytes: &'a Vec<u8>) -> Self { fn from(bytes: &'a Vec<u8>) -> Self {
Bytes { Bytes::new(bytes)
bytes: bytes,
}
} }
} }
@@ -99,16 +95,12 @@ mod bytebuf {
impl ByteBuf { impl ByteBuf {
/// Construct a new, empty `ByteBuf`. /// Construct a new, empty `ByteBuf`.
pub fn new() -> Self { pub fn new() -> Self {
ByteBuf { ByteBuf::from(Vec::new())
bytes: Vec::new(),
}
} }
/// Construct a new, empty `ByteBuf` with the specified capacity. /// Construct a new, empty `ByteBuf` with the specified capacity.
pub fn with_capacity(cap: usize) -> Self { pub fn with_capacity(cap: usize) -> Self {
ByteBuf { ByteBuf::from(Vec::with_capacity(cap))
bytes: Vec::with_capacity(cap)
}
} }
/// Wrap existing bytes in a `ByteBuf`. /// Wrap existing bytes in a `ByteBuf`.
@@ -137,9 +129,7 @@ mod bytebuf {
impl From<Vec<u8>> for ByteBuf { impl From<Vec<u8>> for ByteBuf {
fn from(bytes: Vec<u8>) -> Self { fn from(bytes: Vec<u8>) -> Self {
ByteBuf { ByteBuf::from(bytes)
bytes: bytes,
}
} }
} }
@@ -195,9 +185,7 @@ mod bytebuf {
fn visit_unit<E>(&mut self) -> Result<ByteBuf, E> fn visit_unit<E>(&mut self) -> Result<ByteBuf, E>
where E: de::Error, where E: de::Error,
{ {
Ok(ByteBuf { Ok(ByteBuf::new())
bytes: Vec::new(),
})
} }
#[inline] #[inline]
@@ -213,9 +201,7 @@ mod bytebuf {
try!(visitor.end()); try!(visitor.end());
Ok(ByteBuf { Ok(ByteBuf::from(values))
bytes: values,
})
} }
#[inline] #[inline]
@@ -229,9 +215,7 @@ mod bytebuf {
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<ByteBuf, E> fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<ByteBuf, E>
where E: de::Error, where E: de::Error,
{ {
Ok(ByteBuf { Ok(ByteBuf::from(v))
bytes: v,
})
} }
} }