mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-15 00:00:18 +02:00
Freshen up buf API
This commit is contained in:
+10
-10
@@ -20,10 +20,10 @@ pub fn test_initial_buf_empty() {
|
||||
#[test]
|
||||
pub fn test_byte_buf_bytes() {
|
||||
let mut buf = ByteBuf::mut_with_capacity(32);
|
||||
buf.write(&b"hello "[..]).unwrap();
|
||||
buf.copy_from(&b"hello "[..]).unwrap();
|
||||
assert_eq!(&b"hello "[..], buf.bytes());
|
||||
|
||||
buf.write(&b"world"[..]).unwrap();
|
||||
buf.copy_from(&b"world"[..]).unwrap();
|
||||
assert_eq!(&b"hello world"[..], buf.bytes());
|
||||
let buf = buf.flip();
|
||||
assert_eq!(&b"hello world"[..], buf.bytes());
|
||||
@@ -33,37 +33,37 @@ pub fn test_byte_buf_bytes() {
|
||||
pub fn test_byte_buf_read_write() {
|
||||
let mut buf = ByteBuf::mut_with_capacity(32);
|
||||
|
||||
buf.write(&b"hello world"[..]).unwrap();
|
||||
buf.copy_from(&b"hello world"[..]).unwrap();
|
||||
assert_eq!(21, buf.remaining());
|
||||
|
||||
buf.write(&b" goodbye"[..]).unwrap();
|
||||
buf.copy_from(&b" goodbye"[..]).unwrap();
|
||||
assert_eq!(13, buf.remaining());
|
||||
|
||||
let mut buf = buf.flip();
|
||||
let mut dst = [0; 5];
|
||||
|
||||
buf.mark();
|
||||
assert_eq!(5, buf.read(&mut dst[..]).unwrap());
|
||||
assert_eq!(5, buf.copy_to(&mut dst[..]).unwrap());
|
||||
assert_eq!(b"hello", &dst);
|
||||
buf.reset();
|
||||
assert_eq!(5, buf.read(&mut dst[..]).unwrap());
|
||||
assert_eq!(5, buf.copy_to(&mut dst[..]).unwrap());
|
||||
assert_eq!(b"hello", &dst);
|
||||
|
||||
assert_eq!(5, buf.read(&mut dst[..]).unwrap());
|
||||
assert_eq!(5, buf.copy_to(&mut dst[..]).unwrap());
|
||||
assert_eq!(b" worl", &dst);
|
||||
|
||||
let mut dst = [0; 2];
|
||||
assert_eq!(2, buf.read(&mut dst[..]).unwrap());
|
||||
assert_eq!(2, buf.copy_to(&mut dst[..]).unwrap());
|
||||
assert_eq!(b"d ", &dst);
|
||||
|
||||
let mut dst = [0; 7];
|
||||
assert_eq!(7, buf.read(&mut dst[..]).unwrap());
|
||||
assert_eq!(7, buf.copy_to(&mut dst[..]).unwrap());
|
||||
assert_eq!(b"goodbye", &dst);
|
||||
|
||||
let mut buf = buf.resume();
|
||||
assert_eq!(13, buf.remaining());
|
||||
|
||||
buf.write(&b" have fun"[..]).unwrap();
|
||||
buf.copy_from(&b" have fun"[..]).unwrap();
|
||||
assert_eq!(4, buf.remaining());
|
||||
|
||||
let buf = buf.flip();
|
||||
|
||||
Reference in New Issue
Block a user