Skip to content

Commit

Permalink
Remove redundant reserve call (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar authored Mar 4, 2024
1 parent c5fae00 commit 7968f6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/bytes_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,9 +1283,7 @@ impl Extend<u8> for BytesMut {

// TODO: optimize
// 1. If self.kind() == KIND_VEC, use Vec::extend
// 2. Make `reserve` inline-able
for b in iter {
self.reserve(1);
self.put_u8(b);
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,28 @@ fn extend_mut_from_bytes() {
assert_eq!(*bytes, LONG[..]);
}

#[test]
fn extend_past_lower_limit_of_size_hint() {
// See https://github.com/tokio-rs/bytes/pull/674#pullrequestreview-1913035700
struct Iter<I>(I);

impl<I: Iterator<Item = u8>> Iterator for Iter<I> {
type Item = u8;

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}

fn size_hint(&self) -> (usize, Option<usize>) {
(5, None)
}
}

let mut bytes = BytesMut::with_capacity(5);
bytes.extend(Iter(std::iter::repeat(0).take(10)));
assert_eq!(bytes.len(), 10);
}

#[test]
fn extend_mut_without_size_hint() {
let mut bytes = BytesMut::with_capacity(0);
Expand Down

0 comments on commit 7968f6f

Please sign in to comment.