mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-19 00:00:12 +02:00
Restructure and trim down the library
This commit is a significant overhaul of the library in an effort to head towards a stable API. The rope implementation as well as a number of buffer implementations have been removed from the library and will live at https://github.com/carllerche/bytes-more while they incubate. **Bytes / BytesMut** `Bytes` is now an atomic ref counted byte slice. As it is contigous, it offers a richer API than before. `BytesMut` is a mutable variant. It is safe by ensuring that it is the only handle to a given byte slice. **AppendBuf -> ByteBuf** `AppendBuf` has been replaced by `ByteBuf`. The API is not identical, but is close enough to be considered a suitable replacement. **Removed types** The following types have been removed in favor of living in bytes-more * RingBuf * BlockBuf * `Bytes` as a rope implementation * ReadExt * WriteExt
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
extern crate bytes;
|
||||
extern crate byteorder;
|
||||
|
||||
use bytes::{Buf, Sink};
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn test_fresh_cursor_vec() {
|
||||
let mut buf = Cursor::new(b"hello".to_vec());
|
||||
|
||||
assert_eq!(buf.remaining(), 5);
|
||||
assert_eq!(buf.bytes(), b"hello");
|
||||
|
||||
buf.advance(2);
|
||||
|
||||
assert_eq!(buf.remaining(), 3);
|
||||
assert_eq!(buf.bytes(), b"llo");
|
||||
|
||||
buf.advance(3);
|
||||
|
||||
assert_eq!(buf.remaining(), 0);
|
||||
assert_eq!(buf.bytes(), b"");
|
||||
|
||||
buf.advance(1);
|
||||
|
||||
assert_eq!(buf.remaining(), 0);
|
||||
assert_eq!(buf.bytes(), b"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_u8() {
|
||||
let mut buf = Cursor::new(b"\x21zomg");
|
||||
assert_eq!(0x21, buf.get_u8());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_u16() {
|
||||
let buf = b"\x21\x54zomg";
|
||||
assert_eq!(0x2154, Cursor::new(buf).get_u16::<byteorder::BigEndian>());
|
||||
assert_eq!(0x5421, Cursor::new(buf).get_u16::<byteorder::LittleEndian>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_get_u16_buffer_underflow() {
|
||||
let mut buf = Cursor::new(b"\x21");
|
||||
buf.get_u16::<byteorder::BigEndian>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_sink_capacity() {
|
||||
let mut sink: Vec<u8> = Vec::new();
|
||||
sink.reserve(16);
|
||||
assert!(sink.capacity() >= 16, "Capacity {} must be at least 16", sink.capacity());
|
||||
let mut source = Cursor::new(b"0123456789abcdef0123456789abcdef");
|
||||
sink.sink(&mut source);
|
||||
assert!(sink.len() <= sink.capacity(), "Length {} must be less than or equal to capacity {}", sink.len(), sink.capacity());
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
extern crate bytes;
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
|
||||
fn is_sync<T: Sync>() {}
|
||||
fn is_send<T: Send>() {}
|
||||
|
||||
#[test]
|
||||
fn test_bounds() {
|
||||
is_sync::<Bytes>();
|
||||
is_send::<Bytes>();
|
||||
is_send::<BytesMut>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_slice() {
|
||||
let a = Bytes::from_slice(b"abcdefgh");
|
||||
assert_eq!(a, b"abcdefgh"[..]);
|
||||
assert_eq!(a, &b"abcdefgh"[..]);
|
||||
assert_eq!(a, Vec::from(&b"abcdefgh"[..]));
|
||||
assert_eq!(b"abcdefgh"[..], a);
|
||||
assert_eq!(&b"abcdefgh"[..], a);
|
||||
assert_eq!(Vec::from(&b"abcdefgh"[..]), a);
|
||||
|
||||
let a = BytesMut::from_slice(b"abcdefgh");
|
||||
assert_eq!(a, b"abcdefgh"[..]);
|
||||
assert_eq!(a, &b"abcdefgh"[..]);
|
||||
assert_eq!(a, Vec::from(&b"abcdefgh"[..]));
|
||||
assert_eq!(b"abcdefgh"[..], a);
|
||||
assert_eq!(&b"abcdefgh"[..], a);
|
||||
assert_eq!(Vec::from(&b"abcdefgh"[..]), a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fmt() {
|
||||
let a = format!("{:?}", Bytes::from_slice(b"abcdefg"));
|
||||
let b = format!("{:?}", b"abcdefg");
|
||||
|
||||
assert_eq!(a, b);
|
||||
|
||||
let a = format!("{:?}", BytesMut::from_slice(b"abcdefg"));
|
||||
assert_eq!(a, b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn len() {
|
||||
let a = Bytes::from_slice(b"abcdefg");
|
||||
assert_eq!(a.len(), 7);
|
||||
|
||||
let a = BytesMut::from_slice(b"abcdefg");
|
||||
assert_eq!(a.len(), 7);
|
||||
|
||||
let a = Bytes::from_slice(b"");
|
||||
assert!(a.is_empty());
|
||||
|
||||
let a = BytesMut::from_slice(b"");
|
||||
assert!(a.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn index() {
|
||||
let a = Bytes::from_slice(b"hello world");
|
||||
assert_eq!(a[0..5], *b"hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice() {
|
||||
let a = Bytes::from_slice(b"hello world");
|
||||
|
||||
let b = a.slice(3, 5);
|
||||
assert_eq!(b, b"lo"[..]);
|
||||
|
||||
let b = a.slice_to(5);
|
||||
assert_eq!(b, b"hello"[..]);
|
||||
|
||||
let b = a.slice_from(3);
|
||||
assert_eq!(b, b"lo world"[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn slice_oob_1() {
|
||||
let a = Bytes::from_slice(b"hello world");
|
||||
a.slice(5, 20);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn slice_oob_2() {
|
||||
let a = Bytes::from_slice(b"hello world");
|
||||
a.slice(15, 20);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_off() {
|
||||
let mut hello = Bytes::from_slice(b"helloworld");
|
||||
let world = hello.split_off(5);
|
||||
|
||||
assert_eq!(hello, &b"hello"[..]);
|
||||
assert_eq!(world, &b"world"[..]);
|
||||
|
||||
let mut hello = BytesMut::from_slice(b"helloworld");
|
||||
let world = hello.split_off(5);
|
||||
|
||||
assert_eq!(hello, &b"hello"[..]);
|
||||
assert_eq!(world, &b"world"[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn split_off_oob() {
|
||||
let mut hello = Bytes::from_slice(b"helloworld");
|
||||
hello.split_off(11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn split_off_oob_mut() {
|
||||
let mut hello = BytesMut::from_slice(b"helloworld");
|
||||
hello.split_off(11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_off_uninitialized() {
|
||||
let mut bytes = BytesMut::with_capacity(1024);
|
||||
let other = bytes.split_off(128);
|
||||
|
||||
assert_eq!(bytes.len(), 0);
|
||||
assert_eq!(bytes.capacity(), 128);
|
||||
|
||||
assert_eq!(other.len(), 0);
|
||||
assert_eq!(other.capacity(), 896);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drain_to() {
|
||||
let mut world = Bytes::from_slice(b"helloworld");
|
||||
let hello = world.drain_to(5);
|
||||
|
||||
assert_eq!(hello, &b"hello"[..]);
|
||||
assert_eq!(world, &b"world"[..]);
|
||||
|
||||
let mut world = BytesMut::from_slice(b"helloworld");
|
||||
let hello = world.drain_to(5);
|
||||
|
||||
assert_eq!(hello, &b"hello"[..]);
|
||||
assert_eq!(world, &b"world"[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn drain_to_oob() {
|
||||
let mut hello = Bytes::from_slice(b"helloworld");
|
||||
hello.drain_to(11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn drain_to_oob_mut() {
|
||||
let mut hello = BytesMut::from_slice(b"helloworld");
|
||||
hello.drain_to(11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drain_to_uninitialized() {
|
||||
let mut bytes = BytesMut::with_capacity(1024);
|
||||
let other = bytes.drain_to(128);
|
||||
|
||||
assert_eq!(bytes.len(), 0);
|
||||
assert_eq!(bytes.capacity(), 896);
|
||||
|
||||
assert_eq!(other.len(), 0);
|
||||
assert_eq!(other.capacity(), 128);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
extern crate bytes;
|
||||
extern crate byteorder;
|
||||
|
||||
use bytes::BufMut;
|
||||
use std::usize;
|
||||
|
||||
#[test]
|
||||
fn test_vec_as_mut_buf() {
|
||||
let mut buf = Vec::with_capacity(64);
|
||||
|
||||
assert_eq!(buf.remaining_mut(), usize::MAX);
|
||||
|
||||
unsafe {
|
||||
assert!(buf.bytes_mut().len() >= 64);
|
||||
}
|
||||
|
||||
buf.copy_from(&b"zomg"[..]);
|
||||
|
||||
assert_eq!(&buf, b"zomg");
|
||||
|
||||
assert_eq!(buf.remaining_mut(), usize::MAX - 4);
|
||||
assert_eq!(buf.capacity(), 64);
|
||||
|
||||
for _ in 0..16 {
|
||||
buf.copy_from(&b"zomg"[..]);
|
||||
}
|
||||
|
||||
assert_eq!(buf.len(), 68);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_put_u8() {
|
||||
let mut buf = Vec::with_capacity(8);
|
||||
buf.put_u8(33);
|
||||
assert_eq!(b"\x21", &buf[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_put_u16() {
|
||||
let mut buf = Vec::with_capacity(8);
|
||||
buf.put_u16::<byteorder::BigEndian>(8532);
|
||||
assert_eq!(b"\x21\x54", &buf[..]);
|
||||
|
||||
buf.clear();
|
||||
buf.put_u16::<byteorder::LittleEndian>(8532);
|
||||
assert_eq!(b"\x54\x21", &buf[..]);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
extern crate bytes;
|
||||
|
||||
use bytes::{Buf, BufMut, SliceBuf};
|
||||
|
||||
#[test]
|
||||
fn test_initial_buf_empty() {
|
||||
let mut mem = [0u8; 100];
|
||||
let buf = SliceBuf::new(&mut mem[..]);
|
||||
|
||||
assert!(buf.capacity() == 100);
|
||||
assert!(buf.remaining_mut() == 100);
|
||||
assert!(buf.remaining() == 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice_buf_bytes() {
|
||||
let mut mem = [0u8; 32];
|
||||
let mut buf = SliceBuf::new(&mut mem[..]);
|
||||
|
||||
buf.copy_from(&b"hello "[..]);
|
||||
assert_eq!(&b"hello "[..], buf.bytes());
|
||||
|
||||
buf.copy_from(&b"world"[..]);
|
||||
assert_eq!(&b"hello world"[..], buf.bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_buf_read_write() {
|
||||
let mut mem = [0u8; 32];
|
||||
let mut buf = SliceBuf::new(&mut mem[..]);
|
||||
|
||||
buf.copy_from(&b"hello world"[..]);
|
||||
assert_eq!(21, buf.remaining_mut());
|
||||
|
||||
buf.copy_from(&b" goodbye"[..]);
|
||||
assert_eq!(13, buf.remaining_mut());
|
||||
|
||||
let mut dst = [0; 5];
|
||||
|
||||
let pos = buf.position();
|
||||
buf.copy_to(&mut dst[..]);
|
||||
assert_eq!(b"hello", &dst);
|
||||
|
||||
buf.set_position(pos);
|
||||
buf.copy_to(&mut dst[..]);
|
||||
assert_eq!(b"hello", &dst);
|
||||
|
||||
buf.copy_to(&mut dst[..]);
|
||||
assert_eq!(b" worl", &dst);
|
||||
|
||||
let mut dst = [0; 2];
|
||||
buf.copy_to(&mut dst[..]);
|
||||
assert_eq!(b"d ", &dst);
|
||||
|
||||
let mut dst = [0; 7];
|
||||
buf.copy_to(&mut dst[..]);
|
||||
assert_eq!(b"goodbye", &dst);
|
||||
|
||||
assert_eq!(13, buf.remaining_mut());
|
||||
|
||||
buf.copy_from(&b" have fun"[..]);
|
||||
assert_eq!(4, buf.remaining_mut());
|
||||
|
||||
assert_eq!(buf.bytes(), b" have fun");
|
||||
|
||||
buf.set_position(0);
|
||||
assert_eq!(buf.bytes(), b"hello world goodbye have fun");
|
||||
|
||||
buf.clear();
|
||||
assert_eq!(buf.bytes(), b"");
|
||||
}
|
||||
Reference in New Issue
Block a user