Remove IntoBuf/FromBuf (#288)

As consequence Buf::collect is removed as well, which is replaced with `Buf::into_bytes`. The advantage of `Buf::into_bytes` is that it can be optimized in cases where converting a `T: Buf` into a `Bytes` instance is efficient.
This commit is contained in:
Douman
2019-08-27 13:09:43 -07:00
committed by Carl Lerche
parent 79e4b2847f
commit b6cb346adf
16 changed files with 97 additions and 373 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ fn test_vec_as_mut_buf() {
#[test]
fn test_put_u8() {
let mut buf = Vec::with_capacity(8);
buf.put::<u8>(33);
buf.put_u8(33);
assert_eq!(b"\x21", &buf[..]);
}
+6 -6
View File
@@ -302,14 +302,14 @@ fn fns_defined_for_bytes_mut() {
fn reserve_convert() {
// Inline -> Vec
let mut bytes = BytesMut::with_capacity(8);
bytes.put("hello");
bytes.put("hello".as_bytes());
bytes.reserve(40);
assert_eq!(bytes.capacity(), 45);
assert_eq!(bytes, "hello");
// Inline -> Inline
let mut bytes = BytesMut::with_capacity(inline_cap());
bytes.put("abcdefghijkl");
bytes.put("abcdefghijkl".as_bytes());
let a = bytes.split_to(10);
bytes.reserve(inline_cap() - 3);
@@ -336,7 +336,7 @@ fn reserve_convert() {
#[test]
fn reserve_growth() {
let mut bytes = BytesMut::with_capacity(64);
bytes.put("hello world");
bytes.put("hello world".as_bytes());
let _ = bytes.split();
bytes.reserve(65);
@@ -348,7 +348,7 @@ fn reserve_allocates_at_least_original_capacity() {
let mut bytes = BytesMut::with_capacity(1024);
for i in 0..1020 {
bytes.put(i as u8);
bytes.put_u8(i as u8);
}
let _other = bytes.split();
@@ -364,7 +364,7 @@ fn reserve_max_original_capacity_value() {
let mut bytes = BytesMut::with_capacity(SIZE);
for _ in 0..SIZE {
bytes.put(0u8);
bytes.put_u8(0u8);
}
let _other = bytes.split();
@@ -381,7 +381,7 @@ fn reserve_max_original_capacity_value() {
fn reserve_vec_recycling() {
let mut bytes = BytesMut::from(Vec::with_capacity(16));
assert_eq!(bytes.capacity(), 16);
bytes.put("0123456789012345");
bytes.put("0123456789012345".as_bytes());
bytes.advance(10);
assert_eq!(bytes.capacity(), 6);
bytes.reserve(8);
+3 -3
View File
@@ -9,7 +9,7 @@ fn collect_two_bufs() {
let a = Bytes::from(&b"hello"[..]);
let b = Bytes::from(&b"world"[..]);
let res: Vec<u8> = a.chain(b).collect();
let res = a.chain(b).to_bytes();
assert_eq!(res, &b"helloworld"[..]);
}
@@ -21,8 +21,8 @@ fn writing_chained() {
{
let mut buf = Chain::new(&mut a, &mut b);
for i in 0..128 {
buf.put(i as u8);
for i in 0u8..128 {
buf.put_u8(i);
}
}
-33
View File
@@ -1,33 +0,0 @@
#![deny(warnings, rust_2018_idioms)]
use bytes::{Buf, Bytes, BytesMut};
const LONG: &'static [u8] = b"mary had a little lamb, little lamb, little lamb";
const SHORT: &'static [u8] = b"hello world";
#[test]
fn collect_to_vec() {
let buf: Vec<u8> = SHORT.collect();
assert_eq!(buf, SHORT);
let buf: Vec<u8> = LONG.collect();
assert_eq!(buf, LONG);
}
#[test]
fn collect_to_bytes() {
let buf: Bytes = SHORT.collect();
assert_eq!(buf, SHORT);
let buf: Bytes = LONG.collect();
assert_eq!(buf, LONG);
}
#[test]
fn collect_to_bytes_mut() {
let buf: BytesMut = SHORT.collect();
assert_eq!(buf, SHORT);
let buf: BytesMut = LONG.collect();
assert_eq!(buf, LONG);
}