implicitly grow BytesMut; add BufMutExt::chain_mut (#316)

This brings `BytesMut` in line with `Vec<u8>` behavior.

This also fixes an existing bug in BytesMut::bytes_mut that exposes
invalid slices. The bug was recently introduced and was only on master
and never released to `crates.io`.

In order to fix a test, `BufMutExt::chain_mut` is provided. Withou this,
it is not possible to chain two `&mut [u8]`.

Closes #170
This commit is contained in:
Carl Lerche
2019-11-20 12:11:40 -08:00
committed by GitHub
parent 59aea9e871
commit 8135c1f606
6 changed files with 88 additions and 27 deletions
+5 -8
View File
@@ -1,7 +1,7 @@
#![deny(warnings, rust_2018_idioms)]
use bytes::{Buf, BufMut, Bytes, BytesMut};
use bytes::buf::BufExt;
use bytes::{Buf, BufMut, Bytes};
use bytes::buf::{BufExt, BufMutExt};
use std::io::IoSlice;
#[test]
@@ -15,20 +15,17 @@ fn collect_two_bufs() {
#[test]
fn writing_chained() {
let mut a = BytesMut::with_capacity(64);
let mut b = BytesMut::with_capacity(64);
let mut a = [0u8; 64];
let mut b = [0u8; 64];
{
let mut buf = (&mut a).chain(&mut b);
let mut buf = (&mut a[..]).chain_mut(&mut b[..]);
for i in 0u8..128 {
buf.put_u8(i);
}
}
assert_eq!(64, a.len());
assert_eq!(64, b.len());
for i in 0..64 {
let expect = i as u8;
assert_eq!(expect, a[i]);