mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-25 00:00:22 +02:00
committed by
Sean McArthur
parent
90e7e650c9
commit
81550da474
+2
-3
@@ -559,9 +559,8 @@ impl BytesMut {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let (off, prev) = self.get_vec_pos();
|
let (off, prev) = self.get_vec_pos();
|
||||||
|
|
||||||
// Only reuse space if we stand to gain at least capacity/2
|
// Only reuse space if we can satisfy the requested additional space.
|
||||||
// bytes of space back
|
if self.capacity() - self.len() + off >= additional {
|
||||||
if off >= additional && off >= (self.cap / 2) {
|
|
||||||
// There's space - reuse it
|
// There's space - reuse it
|
||||||
//
|
//
|
||||||
// Just move the pointer back to the start after copying
|
// Just move the pointer back to the start after copying
|
||||||
|
|||||||
@@ -929,6 +929,22 @@ fn bytes_buf_mut_advance() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bytes_buf_mut_reuse_when_fully_consumed() {
|
||||||
|
use bytes::{Buf, BytesMut};
|
||||||
|
let mut buf = BytesMut::new();
|
||||||
|
buf.reserve(8192);
|
||||||
|
buf.extend_from_slice(&[0u8; 100][..]);
|
||||||
|
|
||||||
|
let p = &buf[0] as *const u8;
|
||||||
|
buf.advance(100);
|
||||||
|
|
||||||
|
buf.reserve(8192);
|
||||||
|
buf.extend_from_slice(b" ");
|
||||||
|
|
||||||
|
assert_eq!(&buf[0] as *const u8, p);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn bytes_reserve_overflow() {
|
fn bytes_reserve_overflow() {
|
||||||
|
|||||||
Reference in New Issue
Block a user