Merge pull request #520 from serde-rs/bytes

Add constructors for Bytes and ByteBuf
This commit is contained in:
David Tolnay
2016-09-05 08:37:10 -07:00
committed by GitHub
+22 -22
View File
@@ -19,6 +19,15 @@ pub struct Bytes<'a> {
bytes: &'a [u8], bytes: &'a [u8],
} }
impl<'a> Bytes<'a> {
/// Wrap an existing `&[u8]`.
pub fn new(bytes: &'a [u8]) -> Self {
Bytes {
bytes: bytes,
}
}
}
impl<'a> fmt::Debug for Bytes<'a> { impl<'a> fmt::Debug for Bytes<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(f.write_str("b\"")); try!(f.write_str("b\""));
@@ -31,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,
}
} }
} }
@@ -90,15 +95,18 @@ 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::from(Vec::with_capacity(cap))
}
/// Wrap existing bytes in a `ByteBuf`.
pub fn from<T: Into<Vec<u8>>>(bytes: T) -> Self {
ByteBuf { ByteBuf {
bytes: Vec::with_capacity(cap) bytes: bytes.into(),
} }
} }
} }
@@ -121,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,
}
} }
} }
@@ -179,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]
@@ -197,9 +201,7 @@ mod bytebuf {
try!(visitor.end()); try!(visitor.end());
Ok(ByteBuf { Ok(ByteBuf::from(values))
bytes: values,
})
} }
#[inline] #[inline]
@@ -213,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,
})
} }
} }