Fix panic in FromIterator for BytesMut

This commit is contained in:
Carl Lerche
2018-05-11 08:45:04 -07:00
parent a8e62dfc7a
commit 13aaeee345
2 changed files with 22 additions and 0 deletions
+1
View File
@@ -869,6 +869,7 @@ impl FromIterator<u8> for BytesMut {
let mut out = BytesMut::with_capacity(maybe_max.unwrap_or(min));
for i in iter {
out.reserve(1);
out.put(i);
}
+21
View File
@@ -674,3 +674,24 @@ fn unsplit_two_split_offs() {
buf.unsplit(buf2);
assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
}
#[test]
fn from_iter_no_size_hint() {
use std::iter;
let mut expect = vec![];
let actual: Bytes = iter::repeat(b'x')
.scan(100, |cnt, item| {
if *cnt >= 1 {
*cnt -= 1;
expect.push(item);
Some(item)
} else {
None
}
})
.collect();
assert_eq!(&actual[..], &expect[..]);
}