Faster From<[u8]> for BytesMut, remove panic in fmt::Write (#133)

* use slice.to_vec instead of buf.put in From<[u8]>

* don't panic in fmt::Write for BytesMut
This commit is contained in:
Sean McArthur
2017-06-15 10:55:02 -07:00
committed by Carl Lerche
parent 3240fb9cd9
commit 7e8373da8d
3 changed files with 55 additions and 6 deletions
+9 -6
View File
@@ -1418,10 +1418,7 @@ impl<'a> From<&'a [u8]> for BytesMut {
}
}
} else {
let mut buf = BytesMut::with_capacity(src.len());
let src: &[u8] = src.as_ref();
buf.put(src);
buf
BytesMut::from(src.to_vec())
}
}
}
@@ -1487,11 +1484,17 @@ impl Borrow<[u8]> for BytesMut {
}
impl fmt::Write for BytesMut {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
BufMut::put(self, s);
Ok(())
if self.remaining_mut() >= s.len() {
self.put_slice(s.as_bytes());
Ok(())
} else {
Err(fmt::Error)
}
}
#[inline]
fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result {
fmt::write(self, args)
}