mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-08 00:00:26 +02:00
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
72 lines
1.6 KiB
Rust
72 lines
1.6 KiB
Rust
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"");
|
|
}
|