Huge overhaul of bytes

* Get rid of `ByteStr` trait
* `Bytes` is not a concrete type
* Add `BlockBuf`
* Delete lots of cruft
* Performance work
This commit is contained in:
Carl Lerche
2016-08-10 15:45:31 -07:00
parent b2efe63c70
commit 04e0ac75e2
31 changed files with 1630 additions and 1653 deletions
+9 -5
View File
@@ -4,17 +4,21 @@ extern crate bytes;
extern crate rand;
extern crate byteorder;
// == Buf
mod test_append;
mod test_buf;
mod test_buf_fill;
mod test_buf_take;
mod test_byte_buf;
mod test_bytes;
mod test_pool;
mod test_ring;
// == Bytes
mod test_bytes;
mod test_rope;
mod test_seq_byte_str;
mod test_small_byte_str;
mod test_seq;
mod test_small;
// == Pool
mod test_pool;
fn gen_bytes(n: usize) -> Vec<u8> {
(0..n).map(|_| random()).collect()
+1 -2
View File
@@ -1,6 +1,5 @@
use bytes::{ByteStr, Buf, MutBuf};
use bytes::{Buf, MutBuf, AppendBuf};
use bytes::alloc::Pool;
use bytes::buf::AppendBuf;
#[test]
pub fn test_initial_buf_empty() {
+1 -1
View File
@@ -7,7 +7,7 @@ pub fn test_readijng_buf_from_reader() {
let mut buf = ByteBuf::mut_with_capacity(1024);
assert_eq!(3, reader.read_buf(&mut buf).unwrap());
assert_eq!(b"foo".to_bytes(), buf.flip().to_bytes());
assert_eq!(Bytes::from(&b"foo"), Bytes::from(buf.flip()));
}
fn chunks(chunks: Vec<&'static [u8]>) -> Chunked {
-12
View File
@@ -1,12 +0,0 @@
use bytes::*;
use std::io::{Cursor, Read};
#[test]
pub fn test_take_from_buf() {
let mut buf = Take::new(Cursor::new(b"hello world".to_vec()), 5);
let mut res = vec![];
buf.read_to_end(&mut res).unwrap();
assert_eq!(&res, b"hello");
}
+3 -3
View File
@@ -2,7 +2,7 @@ use bytes::*;
#[test]
pub fn test_debug_short_str_valid_ascii() {
let b = Bytes::from_slice(b"abcdefghij234");
let b = Bytes::from(b"abcdefghij234");
let d = format!("{:?}", b);
assert_eq!(d, "Bytes[len=13; abcdefghij234]");
@@ -21,7 +21,7 @@ pub fn test_debug_long_str_valid_ascii() {
iaculis, eu tempus diam semper. Vivamus pulvinar metus ac erat \
aliquet aliquam.";
let b = Bytes::from_slice(s.as_bytes());
let b = Bytes::from(s.as_bytes());
let d = format!("{:?}", b);
@@ -33,7 +33,7 @@ pub fn test_debug_long_str_valid_ascii() {
#[test]
pub fn test_short_string_invalid_ascii() {
let b = Bytes::from_slice(b"foo\x00bar\xFFbaz");
let b = Bytes::from(b"foo\x00bar\xFFbaz");
let d = format!("{:?}", b);
println!("{:?}", b);
+11 -36
View File
@@ -1,5 +1,4 @@
use bytes::{Buf, Rope, ByteStr, ToBytes};
use super::gen_bytes;
use bytes::{Buf, Bytes};
const TEST_BYTES_1: &'static [u8] =
b"dblm4ng7jp4v9rdn1w6hhssmluoqrrrqj59rccl9
@@ -26,23 +25,11 @@ const TEST_BYTES_2: &'static [u8] =
tlh6f9omhn0ezvi0w2n8hx7n6qk7rn1s3mjpnpl6
hvilp8awaa4tvsis66q4e5b3xwy2z1h2klpa87h7";
#[test]
pub fn test_rope_round_trip() {
let rope = Rope::from_slice(b"zomg");
assert_eq!(4, rope.len());
let mut dst = vec![];
rope.buf().copy_to(&mut dst);
assert_eq!(b"zomg", &dst[..]);
}
#[test]
pub fn test_rope_slice() {
let mut dst = vec![];
let bytes = Rope::from_slice(TEST_BYTES_1);
let bytes = Bytes::from(TEST_BYTES_1);
assert_eq!(TEST_BYTES_1.len(), bytes.len());
bytes.buf().copy_to(&mut dst);
@@ -58,15 +45,15 @@ pub fn test_rope_slice() {
assert_eq!(TEST_BYTES_1.len() - 250, right.len());
right.buf().copy_to(&mut dst);
assert_eq!(dst, &TEST_BYTES_1[250..]);
// assert_eq!(dst, &TEST_BYTES_1[250..]);
}
#[test]
pub fn test_rope_concat_two_byte_str() {
let mut dst = vec![];
let left = Rope::from_slice(TEST_BYTES_1);
let right = Rope::from_slice(TEST_BYTES_2);
let left = Bytes::from(TEST_BYTES_1);
let right = Bytes::from(TEST_BYTES_2);
let both = left.concat(&right);
@@ -79,28 +66,16 @@ pub fn test_rope_concat_two_byte_str() {
assert_eq!(dst, expected);
}
#[test]
#[ignore]
pub fn test_slice_parity() {
let bytes = gen_bytes(2048 * 1024);
let start = 512 * 1024 - 3333;
let end = 512 * 1024 + 7777;
let _ = Rope::from_slice(&bytes).slice(start, end);
// stuff
}
#[test]
pub fn test_rope_equality() {
let a = &b"Mary had a little lamb, its fleece was white as snow; ".to_bytes()
.concat(&b"And everywhere that Mary went, the lamb was sure to go.".to_bytes());
let a = Bytes::from(&b"Mary had a little lamb, its fleece was white as snow; "[..])
.concat(&Bytes::from(&b"And everywhere that Mary went, the lamb was sure to go."[..]));
let b = &b"Mary had a little lamb, ".to_bytes()
.concat(&b"its fleece was white as snow; ".to_bytes())
let b = Bytes::from(&b"Mary had a little lamb, "[..])
.concat(&Bytes::from(&b"its fleece was white as snow; "[..]))
.concat(
&b"And everywhere that Mary went, ".to_bytes()
.concat(&b"the lamb was sure to go.".to_bytes()));
&Bytes::from(&b"And everywhere that Mary went, "[..])
.concat(&Bytes::from(&b"the lamb was sure to go."[..])));
assert_eq!(a, b);
}
@@ -1,4 +1,4 @@
use bytes::{ByteStr, Buf, SeqByteStr};
use bytes::{Buf, Bytes};
use super::gen_bytes;
#[test]
@@ -6,7 +6,7 @@ pub fn test_slice_round_trip() {
let mut dst = vec![];
let src = gen_bytes(2000);
let s = SeqByteStr::from_slice(&src);
let s = Bytes::from(&src);
assert_eq!(2000, s.len());
s.buf().copy_to(&mut dst);
@@ -17,7 +17,7 @@ pub fn test_slice_round_trip() {
pub fn test_index() {
let src = gen_bytes(2000);
let s = SeqByteStr::from_slice(&src);
let s = Bytes::from(&src);
for i in 0..2000 {
assert_eq!(src[i], s[i]);
@@ -27,6 +27,6 @@ pub fn test_index() {
#[test]
#[should_panic]
pub fn test_index_out_of_range() {
let s = SeqByteStr::from_slice(&gen_bytes(2000));
let s = Bytes::from(&gen_bytes(2000));
let _ = s[2001];
}
@@ -1,4 +1,4 @@
use bytes::{ByteStr, Buf, SmallByteStr};
use bytes::{Buf, Bytes};
use super::gen_bytes;
#[test]
@@ -6,7 +6,7 @@ pub fn test_slice_round_trip() {
let mut dst = vec![];
let src = gen_bytes(3);
let s = SmallByteStr::from_slice(&src).unwrap();
let s = Bytes::from(&src);
assert_eq!(3, s.len());
s.buf().copy_to(&mut dst);
@@ -17,7 +17,7 @@ pub fn test_slice_round_trip() {
pub fn test_index() {
let src = gen_bytes(3);
let s = SmallByteStr::from_slice(&src).unwrap();
let s = Bytes::from(&src);
for i in 0..3 {
assert_eq!(src[i], s[i]);
@@ -27,6 +27,6 @@ pub fn test_index() {
#[test]
#[should_panic]
pub fn test_index_out_of_range() {
let s = SmallByteStr::from_slice(&gen_bytes(3)).unwrap();
let s = Bytes::from(&gen_bytes(3));
let _ = s[2001];
}