Impl Extend for BytesMut

This commit is contained in:
Carl Lerche
2017-03-07 15:06:47 -08:00
parent 06b94c55b0
commit 2e319b51be
2 changed files with 29 additions and 0 deletions
+22
View File
@@ -1298,6 +1298,28 @@ impl<'a> IntoIterator for &'a BytesMut {
}
}
impl Extend<u8> for BytesMut {
fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item = u8> {
let iter = iter.into_iter();
let (lower, _) = iter.size_hint();
self.reserve(lower);
for b in iter {
unsafe {
self.bytes_mut()[0] = b;
self.advance_mut(1);
}
}
}
}
impl<'a> Extend<&'a u8> for BytesMut {
fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item = &'a u8> {
self.extend(iter.into_iter().map(|b| *b))
}
}
/*
*
* ===== Inner =====
+7
View File
@@ -290,6 +290,13 @@ fn inline_storage() {
assert_eq!(*bytes, zero[0..inline_cap()]);
}
#[test]
fn extend() {
let mut bytes = BytesMut::with_capacity(0);
bytes.extend(LONG);
assert_eq!(*bytes, LONG[..]);
}
#[test]
fn stress() {
// Tests promoting a buffer from a vec -> shared in a concurrent situation