mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-22 00:00:15 +02:00
Fix chain remaining_mut(), allowing to chain growing buffer (#488)
This commit is contained in:
@@ -56,6 +56,10 @@ pub unsafe trait BufMut {
|
|||||||
/// Implementations of `remaining_mut` should ensure that the return value
|
/// Implementations of `remaining_mut` should ensure that the return value
|
||||||
/// does not change unless a call is made to `advance_mut` or any other
|
/// does not change unless a call is made to `advance_mut` or any other
|
||||||
/// function that is documented to change the `BufMut`'s current position.
|
/// function that is documented to change the `BufMut`'s current position.
|
||||||
|
///
|
||||||
|
/// # Note
|
||||||
|
///
|
||||||
|
/// `remaining_mut` may return value smaller than actual available space.
|
||||||
fn remaining_mut(&self) -> usize;
|
fn remaining_mut(&self) -> usize;
|
||||||
|
|
||||||
/// Advance the internal cursor of the BufMut
|
/// Advance the internal cursor of the BufMut
|
||||||
|
|||||||
+1
-2
@@ -198,8 +198,7 @@ where
|
|||||||
fn remaining_mut(&self) -> usize {
|
fn remaining_mut(&self) -> usize {
|
||||||
self.a
|
self.a
|
||||||
.remaining_mut()
|
.remaining_mut()
|
||||||
.checked_add(self.b.remaining_mut())
|
.saturating_add(self.b.remaining_mut())
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn chunk_mut(&mut self) -> &mut UninitSlice {
|
fn chunk_mut(&mut self) -> &mut UninitSlice {
|
||||||
|
|||||||
@@ -133,6 +133,28 @@ fn vectored_read() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn chain_growing_buffer() {
|
||||||
|
let mut buff = [' ' as u8; 10];
|
||||||
|
let mut vec = b"wassup".to_vec();
|
||||||
|
|
||||||
|
let mut chained = (&mut buff[..]).chain_mut(&mut vec).chain_mut(Vec::new()); // Required for potential overflow because remaining_mut for Vec is isize::MAX - vec.len(), but for chain_mut is usize::MAX
|
||||||
|
|
||||||
|
chained.put_slice(b"hey there123123");
|
||||||
|
|
||||||
|
assert_eq!(&buff, b"hey there1");
|
||||||
|
assert_eq!(&vec, b"wassup23123");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn chain_overflow_remaining_mut() {
|
||||||
|
let mut chained = Vec::<u8>::new().chain_mut(Vec::new()).chain_mut(Vec::new());
|
||||||
|
|
||||||
|
assert_eq!(chained.remaining_mut(), usize::MAX);
|
||||||
|
chained.put_slice(&[0; 256]);
|
||||||
|
assert_eq!(chained.remaining_mut(), usize::MAX);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn chain_get_bytes() {
|
fn chain_get_bytes() {
|
||||||
let mut ab = Bytes::copy_from_slice(b"ab");
|
let mut ab = Bytes::copy_from_slice(b"ab");
|
||||||
|
|||||||
Reference in New Issue
Block a user