Skip to content

Commit

Permalink
Merge pull request #520 from serde-rs/bytes
Browse files Browse the repository at this point in the history
Add constructors for Bytes and ByteBuf
  • Loading branch information
dtolnay committed Sep 5, 2016
2 parents 5a258ad + 08bc2d2 commit 8fe66c7
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions serde/src/bytes.rs
Expand Up @@ -19,6 +19,15 @@ pub struct Bytes<'a> {
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> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(f.write_str("b\""));
Expand All @@ -31,18 +40,14 @@ impl<'a> fmt::Debug for Bytes<'a> {

impl<'a> From<&'a [u8]> for Bytes<'a> {
fn from(bytes: &'a [u8]) -> Self {
Bytes {
bytes: bytes,
}
Bytes::new(bytes)
}
}

#[cfg(any(feature = "std", feature = "collections"))]
impl<'a> From<&'a Vec<u8>> for Bytes<'a> {
fn from(bytes: &'a Vec<u8>) -> Self {
Bytes {
bytes: bytes,
}
Bytes::new(bytes)
}
}

Expand Down Expand Up @@ -90,15 +95,18 @@ mod bytebuf {
impl ByteBuf {
/// Construct a new, empty `ByteBuf`.
pub fn new() -> Self {
ByteBuf {
bytes: Vec::new(),
}
ByteBuf::from(Vec::new())
}

/// Construct a new, empty `ByteBuf` with the specified capacity.
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 {
bytes: Vec::with_capacity(cap)
bytes: bytes.into(),
}
}
}
Expand All @@ -121,9 +129,7 @@ mod bytebuf {

impl From<Vec<u8>> for ByteBuf {
fn from(bytes: Vec<u8>) -> Self {
ByteBuf {
bytes: bytes,
}
ByteBuf::from(bytes)
}
}

Expand Down Expand Up @@ -179,9 +185,7 @@ mod bytebuf {
fn visit_unit<E>(&mut self) -> Result<ByteBuf, E>
where E: de::Error,
{
Ok(ByteBuf {
bytes: Vec::new(),
})
Ok(ByteBuf::new())
}

#[inline]
Expand All @@ -197,9 +201,7 @@ mod bytebuf {

try!(visitor.end());

Ok(ByteBuf {
bytes: values,
})
Ok(ByteBuf::from(values))
}

#[inline]
Expand All @@ -213,9 +215,7 @@ mod bytebuf {
fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<ByteBuf, E>
where E: de::Error,
{
Ok(ByteBuf {
bytes: v,
})
Ok(ByteBuf::from(v))
}
}

Expand Down

0 comments on commit 8fe66c7

Please sign in to comment.