Skip to content
Snippets Groups Projects
Commit 2e319b51 authored by Carl Lerche's avatar Carl Lerche
Browse files

Impl Extend for BytesMut

parent 06b94c55
No related branches found
No related tags found
No related merge requests found
...@@ -1298,6 +1298,28 @@ impl<'a> IntoIterator for &'a BytesMut { ...@@ -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 ===== * ===== Inner =====
......
...@@ -290,6 +290,13 @@ fn inline_storage() { ...@@ -290,6 +290,13 @@ fn inline_storage() {
assert_eq!(*bytes, zero[0..inline_cap()]); 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] #[test]
fn stress() { fn stress() {
// Tests promoting a buffer from a vec -> shared in a concurrent situation // Tests promoting a buffer from a vec -> shared in a concurrent situation
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment