mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-07 00:00:08 +02:00
Test semantics of buffer allocation of get_mut()
* if remaining bytes are smaller then 8 * 1024, allocate 8 * 1024 * otherwise allocate as much as needed to hold the remaining bytes without re-allocations.
This commit is contained in:
+12
-2
@@ -153,7 +153,7 @@ impl EasyBuf {
|
|||||||
|
|
||||||
// If we couldn't get access above then we give ourself a new buffer
|
// If we couldn't get access above then we give ourself a new buffer
|
||||||
// here.
|
// here.
|
||||||
let mut v = Vec::with_capacity(cmp::min(INITIAL_CAPACITY, self.as_ref().len()));
|
let mut v = Vec::with_capacity(cmp::max(INITIAL_CAPACITY, self.as_ref().len()));
|
||||||
v.extend_from_slice(self.as_ref());
|
v.extend_from_slice(self.as_ref());
|
||||||
self.start = 0;
|
self.start = 0;
|
||||||
self.buf = Arc::new(v);
|
self.buf = Arc::new(v);
|
||||||
@@ -465,7 +465,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn easybuf_get_mut_sliced_allocating() {
|
fn easybuf_get_mut_sliced_allocating_at_least_initial_capacity() {
|
||||||
let vec: Vec<u8> = (0u8..10u8).collect();
|
let vec: Vec<u8> = (0u8..10u8).collect();
|
||||||
let mut buf: EasyBuf = vec.into();
|
let mut buf: EasyBuf = vec.into();
|
||||||
buf.split_off(9);
|
buf.split_off(9);
|
||||||
@@ -477,6 +477,16 @@ mod tests {
|
|||||||
mem::drop(clone); // prevent unused warning
|
mem::drop(clone); // prevent unused warning
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn easybuf_get_mut_sliced_allocating_required_capacity() {
|
||||||
|
let vec: Vec<u8> = (0..INITIAL_CAPACITY * 2).map(|_|0u8).collect();
|
||||||
|
let mut buf: EasyBuf = vec.into();
|
||||||
|
buf.drain_to(INITIAL_CAPACITY / 2);
|
||||||
|
let clone = buf.clone();
|
||||||
|
assert_eq!(buf.get_mut().buf.capacity(), INITIAL_CAPACITY + INITIAL_CAPACITY / 2);
|
||||||
|
mem::drop(clone)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn easybuf_into_vec_simple() {
|
fn easybuf_into_vec_simple() {
|
||||||
let vec: Vec<u8> = (0u8..10u8).collect();
|
let vec: Vec<u8> = (0u8..10u8).collect();
|
||||||
|
|||||||
Reference in New Issue
Block a user