Skip to content

Commit 7968f6f

Browse files
authored
Remove redundant reserve call (#674)
1 parent c5fae00 commit 7968f6f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/bytes_mut.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,9 +1283,7 @@ impl Extend<u8> for BytesMut {
12831283

12841284
// TODO: optimize
12851285
// 1. If self.kind() == KIND_VEC, use Vec::extend
1286-
// 2. Make `reserve` inline-able
12871286
for b in iter {
1288-
self.reserve(1);
12891287
self.put_u8(b);
12901288
}
12911289
}

tests/test_bytes.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,28 @@ fn extend_mut_from_bytes() {
598598
assert_eq!(*bytes, LONG[..]);
599599
}
600600

601+
#[test]
602+
fn extend_past_lower_limit_of_size_hint() {
603+
// See https://github.com/tokio-rs/bytes/pull/674#pullrequestreview-1913035700
604+
struct Iter<I>(I);
605+
606+
impl<I: Iterator<Item = u8>> Iterator for Iter<I> {
607+
type Item = u8;
608+
609+
fn next(&mut self) -> Option<Self::Item> {
610+
self.0.next()
611+
}
612+
613+
fn size_hint(&self) -> (usize, Option<usize>) {
614+
(5, None)
615+
}
616+
}
617+
618+
let mut bytes = BytesMut::with_capacity(5);
619+
bytes.extend(Iter(std::iter::repeat(0).take(10)));
620+
assert_eq!(bytes.len(), 10);
621+
}
622+
601623
#[test]
602624
fn extend_mut_without_size_hint() {
603625
let mut bytes = BytesMut::with_capacity(0);

0 commit comments

Comments
 (0)