mirror of
https://github.com/openharmony/third_party_rust_bytes.git
synced 2026-07-18 16:24:29 -04:00
committed by
Sean McArthur
parent
1e82681d16
commit
b83ea40780
+2
-3
@@ -559,9 +559,8 @@ impl BytesMut {
|
||||
unsafe {
|
||||
let (off, prev) = self.get_vec_pos();
|
||||
|
||||
// Only reuse space if we stand to gain at least capacity/2
|
||||
// bytes of space back
|
||||
if off >= additional && off >= (self.cap / 2) {
|
||||
// Only reuse space if we can satisfy the requested additional space.
|
||||
if self.capacity() - self.len() + off >= additional {
|
||||
// There's space - reuse it
|
||||
//
|
||||
// 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]
|
||||
#[should_panic]
|
||||
fn bytes_reserve_overflow() {
|
||||
|
||||
Reference in New Issue
Block a user