mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-11 00:00:23 +02:00
BytesMut::extend_from_slice shortcut (#112)
Similar to `Vec::extend_from_slice`: it a reserve followed by memcopy.
This commit is contained in:
committed by
Carl Lerche
parent
2c0cb1b6b8
commit
fa44c7e355
@@ -1160,6 +1160,27 @@ impl BytesMut {
|
||||
pub fn reserve(&mut self, additional: usize) {
|
||||
self.inner.reserve(additional)
|
||||
}
|
||||
|
||||
/// Append given bytes to this object.
|
||||
///
|
||||
/// If this `BytesMut` object has not enough capacity, it is resized first.
|
||||
/// So unlike `put_slice` operation, `extend_from_slice` does not panic.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::BytesMut;
|
||||
///
|
||||
/// let mut buf = BytesMut::with_capacity(0);
|
||||
/// buf.extend_from_slice(b"aaabbb");
|
||||
/// buf.extend_from_slice(b"cccddd");
|
||||
///
|
||||
/// assert_eq!(b"aaabbbcccddd", &buf[..]);
|
||||
/// ```
|
||||
pub fn extend_from_slice(&mut self, extend: &[u8]) {
|
||||
self.reserve(extend.len());
|
||||
self.put_slice(extend);
|
||||
}
|
||||
}
|
||||
|
||||
impl BufMut for BytesMut {
|
||||
|
||||
Reference in New Issue
Block a user