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
+89 -112
View File
@@ -2,7 +2,7 @@ use super::{IntoBuf, Take, Reader, FromBuf, Chain};
use byteorder::{BigEndian, ByteOrder, LittleEndian};
use iovec::IoVec;
use std::{cmp, io, ptr};
use std::{cmp, ptr};
macro_rules! buf_get_impl {
($this:ident, $size:expr, $conv:path) => ({
@@ -44,13 +44,12 @@ macro_rules! buf_get_impl {
/// position. It can be thought of as an efficient `Iterator` for collections of
/// bytes.
///
/// The simplest `Buf` is a `Cursor` wrapping a `[u8]`.
/// The simplest `Buf` is a `&[u8]`.
///
/// ```
/// use bytes::{Buf, Bytes};
/// use std::io::Cursor;
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"hello world");
/// let mut buf = &b"hello world"[..];
///
/// assert_eq!(b'h', buf.get_u8());
/// assert_eq!(b'e', buf.get_u8());
@@ -59,7 +58,7 @@ macro_rules! buf_get_impl {
/// let mut rest = [0; 8];
/// buf.copy_to_slice(&mut rest);
///
/// assert_eq!(&rest[..], b"lo world");
/// assert_eq!(&rest[..], &b"lo world"[..]);
/// ```
pub trait Buf {
/// Returns the number of bytes between the current position and the end of
@@ -71,9 +70,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"hello world");
/// let mut buf = &b"hello world"[..];
///
/// assert_eq!(buf.remaining(), 11);
///
@@ -99,15 +98,15 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"hello world");
/// let mut buf = &b"hello world"[..];
///
/// assert_eq!(buf.bytes(), b"hello world");
/// assert_eq!(buf.bytes(), &b"hello world"[..]);
///
/// buf.advance(6);
///
/// assert_eq!(buf.bytes(), b"world");
/// assert_eq!(buf.bytes(), &b"world"[..]);
/// ```
///
/// # Implementer notes
@@ -165,15 +164,15 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"hello world");
/// let mut buf = &b"hello world"[..];
///
/// assert_eq!(buf.bytes(), b"hello world");
/// assert_eq!(buf.bytes(), &b"hello world"[..]);
///
/// buf.advance(6);
///
/// assert_eq!(buf.bytes(), b"world");
/// assert_eq!(buf.bytes(), &b"world"[..]);
/// ```
///
/// # Panics
@@ -196,9 +195,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"a");
/// let mut buf = &b"a"[..];
///
/// assert!(buf.has_remaining());
///
@@ -218,13 +217,13 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"hello world");
/// let mut buf = &b"hello world"[..];
/// let mut dst = [0; 5];
///
/// buf.copy_to_slice(&mut dst);
/// assert_eq!(b"hello", &dst);
/// assert_eq!(&b"hello"[..], &dst);
/// assert_eq!(6, buf.remaining());
/// ```
///
@@ -260,9 +259,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x08 hello");
/// let mut buf = &b"\x08 hello"[..];
/// assert_eq!(8, buf.get_u8());
/// ```
///
@@ -283,9 +282,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x08 hello");
/// let mut buf = &b"\x08 hello"[..];
/// assert_eq!(8, buf.get_i8());
/// ```
///
@@ -306,9 +305,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x08\x09 hello");
/// let mut buf = &b"\x08\x09 hello"[..];
/// assert_eq!(0x0809, buf.get_u16());
/// ```
///
@@ -326,9 +325,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x09\x08 hello");
/// let mut buf = &b"\x09\x08 hello"[..];
/// assert_eq!(0x0809, buf.get_u16_le());
/// ```
///
@@ -346,9 +345,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x08\x09 hello");
/// let mut buf = &b"\x08\x09 hello"[..];
/// assert_eq!(0x0809, buf.get_i16());
/// ```
///
@@ -366,9 +365,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x09\x08 hello");
/// let mut buf = &b"\x09\x08 hello"[..];
/// assert_eq!(0x0809, buf.get_i16_le());
/// ```
///
@@ -386,9 +385,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x08\x09\xA0\xA1 hello");
/// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..];
/// assert_eq!(0x0809A0A1, buf.get_u32());
/// ```
///
@@ -406,9 +405,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\xA1\xA0\x09\x08 hello");
/// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..];
/// assert_eq!(0x0809A0A1, buf.get_u32_le());
/// ```
///
@@ -426,9 +425,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x08\x09\xA0\xA1 hello");
/// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..];
/// assert_eq!(0x0809A0A1, buf.get_i32());
/// ```
///
@@ -446,9 +445,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\xA1\xA0\x09\x08 hello");
/// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..];
/// assert_eq!(0x0809A0A1, buf.get_i32_le());
/// ```
///
@@ -466,9 +465,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello");
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..];
/// assert_eq!(0x0102030405060708, buf.get_u64());
/// ```
///
@@ -486,9 +485,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello");
/// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
/// assert_eq!(0x0102030405060708, buf.get_u64_le());
/// ```
///
@@ -506,9 +505,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello");
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..];
/// assert_eq!(0x0102030405060708, buf.get_i64());
/// ```
///
@@ -526,9 +525,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello");
/// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
/// assert_eq!(0x0102030405060708, buf.get_i64_le());
/// ```
///
@@ -548,9 +547,8 @@ pub trait Buf {
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
///
/// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello");
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..];
/// assert_eq!(0x01020304050607080910111213141516, buf.get_u128());
/// ```
///
@@ -571,9 +569,8 @@ pub trait Buf {
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
///
/// let mut buf = Cursor::new(b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello");
/// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
/// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_le());
/// ```
///
@@ -594,9 +591,8 @@ pub trait Buf {
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
///
/// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello");
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..];
/// assert_eq!(0x01020304050607080910111213141516, buf.get_i128());
/// ```
///
@@ -617,9 +613,8 @@ pub trait Buf {
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
///
/// let mut buf = Cursor::new(b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello");
/// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
/// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_le());
/// ```
///
@@ -638,9 +633,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x01\x02\x03 hello");
/// let mut buf = &b"\x01\x02\x03 hello"[..];
/// assert_eq!(0x010203, buf.get_uint(3));
/// ```
///
@@ -658,9 +653,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x03\x02\x01 hello");
/// let mut buf = &b"\x03\x02\x01 hello"[..];
/// assert_eq!(0x010203, buf.get_uint_le(3));
/// ```
///
@@ -678,9 +673,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x01\x02\x03 hello");
/// let mut buf = &b"\x01\x02\x03 hello"[..];
/// assert_eq!(0x010203, buf.get_int(3));
/// ```
///
@@ -698,9 +693,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x03\x02\x01 hello");
/// let mut buf = &b"\x03\x02\x01 hello"[..];
/// assert_eq!(0x010203, buf.get_int_le(3));
/// ```
///
@@ -719,9 +714,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x3F\x99\x99\x9A hello");
/// let mut buf = &b"\x3F\x99\x99\x9A hello"[..];
/// assert_eq!(1.2f32, buf.get_f32());
/// ```
///
@@ -740,9 +735,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x9A\x99\x99\x3F hello");
/// let mut buf = &b"\x9A\x99\x99\x3F hello"[..];
/// assert_eq!(1.2f32, buf.get_f32_le());
/// ```
///
@@ -761,9 +756,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello");
/// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..];
/// assert_eq!(1.2f64, buf.get_f64());
/// ```
///
@@ -782,9 +777,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello");
/// let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..];
/// assert_eq!(1.2f64, buf.get_f64_le());
/// ```
///
@@ -805,12 +800,12 @@ pub trait Buf {
/// Collecting a buffer and loading the contents into a `Vec<u8>`.
///
/// ```
/// use bytes::{Buf, Bytes, IntoBuf};
/// use bytes::Buf;
///
/// let buf = Bytes::from(&b"hello world"[..]).into_buf();
/// let buf = &b"hello world"[..];
/// let vec: Vec<u8> = buf.collect();
///
/// assert_eq!(vec, &b"hello world"[..]);
/// assert_eq!(vec, b"hello world");
/// ```
fn collect<B>(self) -> B
where Self: Sized,
@@ -827,9 +822,9 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BufMut, Bytes};
/// use bytes::{Buf, BufMut};
///
/// let mut buf = Bytes::from_static(b"hello world").take(5);
/// let mut buf = b"hello world"[..].take(5);
/// let mut dst = vec![];
///
/// dst.put(&mut buf);
@@ -854,14 +849,12 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf};
/// use bytes::buf::Chain;
/// use bytes::Buf;
///
/// let chain = Bytes::from_static(b"hello ")
/// .chain(Bytes::from(&b"world"[..]));
/// let chain = b"hello "[..].chain(&b"world"[..]);
///
/// let full: Bytes = chain.collect();
/// assert_eq!(full[..], b"hello world"[..]);
/// let full: Vec<u8> = chain.collect();
/// assert_eq!(full, b"hello world");
/// ```
fn chain<U>(self, next: U) -> Chain<Self, U::Buf>
where U: IntoBuf,
@@ -877,20 +870,20 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BufMut, Bytes};
/// use bytes::{Buf, BufMut};
///
/// let mut buf = Bytes::from_static(b"hello world");
/// let mut buf = &b"hello world"[..];
/// let mut dst = vec![];
///
/// {
/// let mut reference = buf.by_ref();
/// dst.put(&mut reference.take(5));
/// assert_eq!(dst, b"hello");
/// assert_eq!(dst, &b"hello"[..]);
/// } // drop our &mut reference so we can use `buf` again
///
/// dst.clear();
/// dst.put(&mut buf);
/// assert_eq!(dst, b" world");
/// assert_eq!(dst, &b" world"[..]);
/// ```
fn by_ref(&mut self) -> &mut Self where Self: Sized {
self
@@ -917,7 +910,7 @@ pub trait Buf {
/// let num = reader.read(&mut dst).unwrap();
///
/// assert_eq!(11, num);
/// assert_eq!(&dst[..11], b"hello world");
/// assert_eq!(&dst[..11], &b"hello world"[..]);
/// ```
fn reader(self) -> Reader<Self> where Self: Sized {
super::reader::new(self)
@@ -960,36 +953,20 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
}
}
impl<T: AsRef<[u8]>> Buf for io::Cursor<T> {
impl<'a> Buf for &'a [u8] {
#[inline]
fn remaining(&self) -> usize {
let len = self.get_ref().as_ref().len();
let pos = self.position();
if pos >= len as u64 {
return 0;
}
len - pos as usize
self.len()
}
#[inline]
fn bytes(&self) -> &[u8] {
let len = self.get_ref().as_ref().len();
let pos = self.position() as usize;
if pos >= len {
return Default::default();
}
&(self.get_ref().as_ref())[pos..]
self
}
#[inline]
fn advance(&mut self, cnt: usize) {
let pos = (self.position() as usize)
.checked_add(cnt).expect("overflow");
assert!(pos <= self.get_ref().as_ref().len());
self.set_position(pos as u64);
*self = &self[cnt..];
}
}