mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-25 00:00:22 +02:00
Fix panic in FromIterator for BytesMut
This commit is contained in:
@@ -869,6 +869,7 @@ impl FromIterator<u8> for BytesMut {
|
|||||||
let mut out = BytesMut::with_capacity(maybe_max.unwrap_or(min));
|
let mut out = BytesMut::with_capacity(maybe_max.unwrap_or(min));
|
||||||
|
|
||||||
for i in iter {
|
for i in iter {
|
||||||
|
out.reserve(1);
|
||||||
out.put(i);
|
out.put(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -674,3 +674,24 @@ fn unsplit_two_split_offs() {
|
|||||||
buf.unsplit(buf2);
|
buf.unsplit(buf2);
|
||||||
assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
|
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[..]);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user