Add constructors for Bytes and ByteBuf

This commit adds `Bytes::new(&[u8])` and `ByteBuf::from<T: Into<Vec<u8>>>(T)`.
This commit is contained in:
David Tolnay 2016-08-23 16:23:44 -04:00
parent d690ffda8d
commit fb7ba225d1
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

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\""));
@ -101,6 +110,13 @@ mod bytebuf {
bytes: Vec::with_capacity(cap) bytes: Vec::with_capacity(cap)
} }
} }
/// Wrap existing bytes in a `ByteBuf`.
pub fn from<T: Into<Vec<u8>>>(bytes: T) -> Self {
ByteBuf {
bytes: bytes.into(),
}
}
} }
impl fmt::Debug for ByteBuf { impl fmt::Debug for ByteBuf {