From b68fa46e3da7bff20f1be7850a4487131cb24a27 Mon Sep 17 00:00:00 2001 From: Carl Lerche <me@carllerche.com> Date: Fri, 11 May 2018 08:45:04 -0700 Subject: [PATCH] Fix panic in FromIterator for BytesMut --- src/bytes.rs | 1 + tests/test_bytes.rs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/bytes.rs b/src/bytes.rs index 3228474..9aa24af 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -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); } diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index 15c3157..c940f01 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -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[..]); +} -- GitLab