Remove io::Cursor, and implement Buf/BufMut for slices instead (#261)

This commit is contained in:
Sean McArthur
2019-06-07 12:31:10 -07:00
committed by Carl Lerche
parent d8134903de
commit 55aa530dc1
15 changed files with 191 additions and 254 deletions
+8 -8
View File
@@ -4,11 +4,10 @@ extern crate iovec;
use bytes::Buf;
use iovec::IoVec;
use std::io::Cursor;
#[test]
fn test_fresh_cursor_vec() {
let mut buf = Cursor::new(b"hello".to_vec());
let mut buf = &b"hello"[..];
assert_eq!(buf.remaining(), 5);
assert_eq!(buf.bytes(), b"hello");
@@ -26,27 +25,28 @@ fn test_fresh_cursor_vec() {
#[test]
fn test_get_u8() {
let mut buf = Cursor::new(b"\x21zomg");
let mut buf = &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());
assert_eq!(0x5421, Cursor::new(buf).get_u16_le());
let mut buf = &b"\x21\x54zomg"[..];
assert_eq!(0x2154, buf.get_u16());
let mut buf = &b"\x21\x54zomg"[..];
assert_eq!(0x5421, buf.get_u16_le());
}
#[test]
#[should_panic]
fn test_get_u16_buffer_underflow() {
let mut buf = Cursor::new(b"\x21");
let mut buf = &b"\x21"[..];
buf.get_u16();
}
#[test]
fn test_bufs_vec() {
let buf = Cursor::new(b"hello world");
let buf = &b"hello world"[..];
let b1: &[u8] = &mut [0];
let b2: &[u8] = &mut [0];
+7
View File
@@ -81,3 +81,10 @@ fn test_bufs_vec_mut() {
assert_eq!(1, buf.bytes_vec_mut(&mut dst[..]));
}
}
#[test]
fn test_mut_slice() {
let mut v = vec![0, 0, 0, 0];
let mut s = &mut v[..];
s.put_u32(42);
}
+8 -15
View File
@@ -1,6 +1,6 @@
extern crate bytes;
use bytes::{Bytes, BytesMut, Buf, BufMut, IntoBuf};
use bytes::{Bytes, BytesMut, Buf, BufMut};
const LONG: &'static [u8] = b"mary had a little lamb, little lamb, little lamb";
const SHORT: &'static [u8] = b"hello world";
@@ -181,11 +181,11 @@ fn split_off_to_loop() {
}
{
let mut bytes = BytesMut::from(&s[..]);
let mut off = bytes.split_off(i);
let off = bytes.split_off(i);
assert_eq!(i, bytes.len());
let mut sum = Vec::new();
sum.extend(&mut bytes);
sum.extend(&mut off);
sum.extend(&bytes);
sum.extend(&off);
assert_eq!(&s[..], &sum[..]);
}
{
@@ -199,11 +199,11 @@ fn split_off_to_loop() {
}
{
let mut bytes = BytesMut::from(&s[..]);
let mut off = bytes.split_to(i);
let off = bytes.split_to(i);
assert_eq!(i, off.len());
let mut sum = Vec::new();
sum.extend(&mut off);
sum.extend(&mut bytes);
sum.extend(&off);
sum.extend(&bytes);
assert_eq!(&s[..], &sum[..]);
}
}
@@ -294,17 +294,10 @@ fn fns_defined_for_bytes_mut() {
bytes.as_mut_ptr();
// Iterator
let v: Vec<u8> = bytes.iter().map(|b| *b).collect();
let v: Vec<u8> = bytes.as_ref().iter().cloned().collect();
assert_eq!(&v[..], bytes);
}
#[test]
fn mut_into_buf() {
let mut v = vec![0, 0, 0, 0];
let s = &mut v[..];
s.into_buf().put_u32_le(42);
}
#[test]
fn reserve_convert() {
// Inline -> Vec
+4 -5
View File
@@ -4,12 +4,11 @@ extern crate iovec;
use bytes::{Buf, BufMut, Bytes, BytesMut};
use bytes::buf::Chain;
use iovec::IoVec;
use std::io::Cursor;
#[test]
fn collect_two_bufs() {
let a = Cursor::new(Bytes::from(&b"hello"[..]));
let b = Cursor::new(Bytes::from(&b"world"[..]));
let a = Bytes::from(&b"hello"[..]);
let b = Bytes::from(&b"world"[..]);
let res: Vec<u8> = a.chain(b).collect();
assert_eq!(res, &b"helloworld"[..]);
@@ -49,8 +48,8 @@ fn iterating_two_bufs() {
#[test]
fn vectored_read() {
let a = Cursor::new(Bytes::from(&b"hello"[..]));
let b = Cursor::new(Bytes::from(&b"world"[..]));
let a = Bytes::from(&b"hello"[..]);
let b = Bytes::from(&b"world"[..]);
let mut buf = a.chain(b);
+6 -7
View File
@@ -1,34 +1,33 @@
extern crate bytes;
use bytes::{Buf, Bytes, BytesMut};
use std::io::Cursor;
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> = Cursor::new(SHORT).collect();
let buf: Vec<u8> = SHORT.collect();
assert_eq!(buf, SHORT);
let buf: Vec<u8> = Cursor::new(LONG).collect();
let buf: Vec<u8> = LONG.collect();
assert_eq!(buf, LONG);
}
#[test]
fn collect_to_bytes() {
let buf: Bytes = Cursor::new(SHORT).collect();
let buf: Bytes = SHORT.collect();
assert_eq!(buf, SHORT);
let buf: Bytes = Cursor::new(LONG).collect();
let buf: Bytes = LONG.collect();
assert_eq!(buf, LONG);
}
#[test]
fn collect_to_bytes_mut() {
let buf: BytesMut = Cursor::new(SHORT).collect();
let buf: BytesMut = SHORT.collect();
assert_eq!(buf, SHORT);
let buf: BytesMut = Cursor::new(LONG).collect();
let buf: BytesMut = LONG.collect();
assert_eq!(buf, LONG);
}
+5 -5
View File
@@ -1,13 +1,13 @@
extern crate bytes;
use std::io::{BufRead, Cursor, Read};
use std::io::{BufRead, Read};
use bytes::Buf;
#[test]
fn read() {
let buf1 = Cursor::new(b"hello ");
let buf2 = Cursor::new(b"world");
let buf1 = &b"hello "[..];
let buf2 = &b"world"[..];
let buf = Buf::chain(buf1, buf2); // Disambiguate with Read::chain
let mut buffer = Vec::new();
buf.reader().read_to_end(&mut buffer).unwrap();
@@ -16,8 +16,8 @@ fn read() {
#[test]
fn buf_read() {
let buf1 = Cursor::new(b"hell");
let buf2 = Cursor::new(b"o\nworld");
let buf1 = &b"hell"[..];
let buf2 = &b"o\nworld"[..];
let mut reader = Buf::chain(buf1, buf2).reader();
let mut line = String::new();
reader.read_line(&mut line).unwrap();
+1 -2
View File
@@ -1,13 +1,12 @@
extern crate bytes;
use bytes::Buf;
use std::io::Cursor;
#[test]
fn long_take() {
// Tests that get a take with a size greater than the buffer length will not
// overrun the buffer. Regression test for #138.
let buf = Cursor::new(b"hello world").take(100);
let buf = b"hello world".take(100);
assert_eq!(11, buf.remaining());
assert_eq!(b"hello world", buf.bytes());
}