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 5b6fa9cea2
commit e3de5089d6
15 changed files with 191 additions and 254 deletions
+1 -2
View File
@@ -5,7 +5,6 @@ extern crate test;
use test::Bencher;
use bytes::Buf;
use std::io::Cursor;
/// Dummy Buf implementation
struct TestBuf {
@@ -120,7 +119,7 @@ macro_rules! bench {
#[bench]
fn $fname(b: &mut Bencher) {
// buf must be long enough for one read of 8 bytes starting at pos 7
let mut buf = Cursor::new(vec![1u8; 8+7]);
let mut buf = [1u8; 8+7];
b.iter(|| {
for i in 0..8 {
buf.set_position(i);
+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..];
}
}
+17 -28
View File
@@ -2,7 +2,7 @@ use super::{IntoBuf, Writer};
use byteorder::{LittleEndian, ByteOrder, BigEndian};
use iovec::IoVecMut;
use std::{cmp, io, ptr, usize};
use std::{cmp, ptr, usize};
/// A trait for values that provide sequential write access to bytes.
///
@@ -34,10 +34,10 @@ pub trait BufMut {
/// # Examples
///
/// ```
/// use bytes::{BufMut, BytesMut};
/// use bytes::BufMut;
///
/// let mut src = [0u8; 10];
/// let mut buf = BytesMut::from(&src[..]);
/// let mut dst = [0; 10];
/// let mut buf = &mut dst[..];
///
/// let original_remaining = buf.remaining_mut();
/// buf.put("hello");
@@ -104,10 +104,9 @@ pub trait BufMut {
///
/// ```
/// use bytes::BufMut;
/// use std::io::Cursor;
///
/// let mut dst = [0; 5];
/// let mut buf = Cursor::new(&mut dst);
/// let mut buf = &mut dst[..];
///
/// assert!(buf.has_remaining_mut());
///
@@ -254,12 +253,11 @@ pub trait BufMut {
///
/// ```
/// use bytes::BufMut;
/// use std::io::Cursor;
///
/// let mut dst = [0; 6];
///
/// {
/// let mut buf = Cursor::new(&mut dst);
/// let mut buf = &mut dst[..];
/// buf.put_slice(b"hello");
///
/// assert_eq!(1, buf.remaining_mut());
@@ -1018,31 +1016,22 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
}
}
impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
impl<'a> BufMut for &'a mut [u8] {
#[inline]
fn remaining_mut(&self) -> usize {
use Buf;
self.remaining()
self.len()
}
/// Advance the internal cursor of the BufMut
unsafe fn advance_mut(&mut self, cnt: usize) {
use Buf;
self.advance(cnt);
}
/// Returns a mutable slice starting at the current BufMut position and of
/// length between 0 and `BufMut::remaining()`.
///
/// The returned byte slice may represent uninitialized memory.
#[inline]
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
let len = self.get_ref().as_ref().len();
let pos = self.position() as usize;
self
}
if pos >= len {
return Default::default();
}
&mut (self.get_mut().as_mut())[pos..]
#[inline]
unsafe fn advance_mut(&mut self, cnt: usize) {
// Lifetime dance taken from `impl Write for &mut [u8]`.
let (_, b) = ::std::mem::replace(self, &mut []).split_at_mut(cnt);
*self = b;
}
}
+12 -29
View File
@@ -1,6 +1,5 @@
use super::{Buf};
use std::io;
use ::BytesMut;
/// Conversion into a `Buf`
///
@@ -55,58 +54,42 @@ impl<T: Buf> IntoBuf for T {
}
}
impl<'a> IntoBuf for &'a [u8] {
type Buf = io::Cursor<&'a [u8]>;
fn into_buf(self) -> Self::Buf {
io::Cursor::new(self)
}
}
impl<'a> IntoBuf for &'a mut [u8] {
type Buf = io::Cursor<&'a mut [u8]>;
fn into_buf(self) -> Self::Buf {
io::Cursor::new(self)
}
}
impl<'a> IntoBuf for &'a str {
type Buf = io::Cursor<&'a [u8]>;
type Buf = &'a [u8];
fn into_buf(self) -> Self::Buf {
self.as_bytes().into_buf()
self.as_bytes()
}
}
impl IntoBuf for Vec<u8> {
type Buf = io::Cursor<Vec<u8>>;
type Buf = BytesMut;
fn into_buf(self) -> Self::Buf {
io::Cursor::new(self)
self.into()
}
}
impl<'a> IntoBuf for &'a Vec<u8> {
type Buf = io::Cursor<&'a [u8]>;
type Buf = &'a [u8];
fn into_buf(self) -> Self::Buf {
io::Cursor::new(&self[..])
self.as_slice()
}
}
// Kind of annoying... but this impl is required to allow passing `&'static
// [u8]` where for<'a> &'a T: IntoBuf is required.
impl<'a> IntoBuf for &'a &'static [u8] {
type Buf = io::Cursor<&'static [u8]>;
type Buf = &'static [u8];
fn into_buf(self) -> Self::Buf {
io::Cursor::new(self)
*self
}
}
impl<'a> IntoBuf for &'a &'static str {
type Buf = io::Cursor<&'static [u8]>;
type Buf = &'static [u8];
fn into_buf(self) -> Self::Buf {
self.as_bytes().into_buf()
@@ -114,7 +97,7 @@ impl<'a> IntoBuf for &'a &'static str {
}
impl IntoBuf for String {
type Buf = io::Cursor<Vec<u8>>;
type Buf = BytesMut;
fn into_buf(self) -> Self::Buf {
self.into_bytes().into_buf()
@@ -122,7 +105,7 @@ impl IntoBuf for String {
}
impl<'a> IntoBuf for &'a String {
type Buf = io::Cursor<&'a [u8]>;
type Buf = &'a [u8];
fn into_buf(self) -> Self::Buf {
self.as_bytes().into_buf()
+4 -20
View File
@@ -25,11 +25,10 @@ impl<B: Buf> Reader<B> {
///
/// ```rust
/// use bytes::Buf;
/// use std::io::{self, Cursor};
///
/// let mut buf = Cursor::new(b"hello world").reader();
/// let mut buf = b"hello world".reader();
///
/// assert_eq!(0, buf.get_ref().position());
/// assert_eq!(b"hello world", buf.get_ref());
/// ```
pub fn get_ref(&self) -> &B {
&self.buf
@@ -38,21 +37,6 @@ impl<B: Buf> Reader<B> {
/// Gets a mutable reference to the underlying `Buf`.
///
/// It is inadvisable to directly read from the underlying `Buf`.
///
/// # Examples
///
/// ```rust
/// use bytes::Buf;
/// use std::io::{self, Cursor};
///
/// let mut buf = Cursor::new(b"hello world").reader();
/// let mut dst = vec![];
///
/// buf.get_mut().set_position(2);
/// io::copy(&mut buf, &mut dst).unwrap();
///
/// assert_eq!(*dst, b"llo world"[..]);
/// ```
pub fn get_mut(&mut self) -> &mut B {
&mut self.buf
}
@@ -63,9 +47,9 @@ impl<B: Buf> Reader<B> {
///
/// ```rust
/// use bytes::Buf;
/// use std::io::{self, Cursor};
/// use std::io;
///
/// let mut buf = Cursor::new(b"hello world").reader();
/// let mut buf = b"hello world".reader();
/// let mut dst = vec![];
///
/// io::copy(&mut buf, &mut dst).unwrap();
+10 -10
View File
@@ -25,9 +25,9 @@ impl<T> Take<T> {
/// # Examples
///
/// ```rust
/// use bytes::{Buf, BufMut, Bytes};
/// use bytes::{Buf, BufMut};
///
/// let mut buf = Bytes::from_static(b"hello world").take(2);
/// let mut buf = b"hello world".take(2);
/// let mut dst = vec![];
///
/// dst.put(&mut buf);
@@ -50,9 +50,9 @@ impl<T> Take<T> {
/// # Examples
///
/// ```rust
/// use bytes::{Buf, BufMut, Bytes};
/// use bytes::{Buf, BufMut};
///
/// let mut buf = Bytes::from_static(b"hello world").take(2);
/// let mut buf = b"hello world".take(2);
///
/// assert_eq!(11, buf.get_ref().remaining());
/// ```
@@ -67,9 +67,9 @@ impl<T> Take<T> {
/// # Examples
///
/// ```rust
/// use bytes::{Buf, BufMut, Bytes};
/// use bytes::{Buf, BufMut};
///
/// let mut buf = Bytes::from_static(b"hello world").take(2);
/// let mut buf = b"hello world".take(2);
/// let mut dst = vec![];
///
/// buf.get_mut().advance(2);
@@ -91,9 +91,9 @@ impl<T> Take<T> {
/// # Examples
///
/// ```rust
/// use bytes::{Buf, Bytes};
/// use bytes::Buf;
///
/// let mut buf = Bytes::from_static(b"hello world").take(2);
/// let mut buf = b"hello world".take(2);
///
/// assert_eq!(2, buf.limit());
/// assert_eq!(b'h', buf.get_u8());
@@ -113,9 +113,9 @@ impl<T> Take<T> {
/// # Examples
///
/// ```rust
/// use bytes::{Buf, BufMut, Bytes};
/// use bytes::{Buf, BufMut};
///
/// let mut buf = Bytes::from_static(b"hello world").take(2);
/// let mut buf = b"hello world".take(2);
/// let mut dst = vec![];
///
/// dst.put(&mut buf);
+2 -2
View File
@@ -59,10 +59,10 @@ impl<B: BufMut> Writer<B> {
///
/// ```rust
/// use bytes::BufMut;
/// use std::io::{self, Cursor};
/// use std::io;
///
/// let mut buf = vec![].writer();
/// let mut src = Cursor::new(b"hello world");
/// let mut src = &b"hello world"[..];
///
/// io::copy(&mut src, &mut buf).unwrap();
///
+17 -9
View File
@@ -1,4 +1,4 @@
use {Buf, BufMut};
use {Buf, BufMut, IntoBuf};
use buf::IntoIter;
use debug;
@@ -1058,12 +1058,12 @@ impl IntoIterator for Bytes {
}
}
impl<'a> IntoIterator for &'a mut Bytes {
type Item = u8;
type IntoIter = IntoIter<&'a mut Bytes>;
impl<'a> IntoIterator for &'a Bytes {
type Item = &'a u8;
type IntoIter = ::std::slice::Iter<'a, u8>;
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self)
self.as_ref().into_iter()
}
}
@@ -1636,6 +1636,14 @@ impl BufMut for BytesMut {
}
}
impl<'a> IntoBuf for &'a BytesMut {
type Buf = &'a [u8];
fn into_buf(self) -> Self::Buf {
self.as_ref()
}
}
impl AsRef<[u8]> for BytesMut {
#[inline]
fn as_ref(&self) -> &[u8] {
@@ -1807,12 +1815,12 @@ impl IntoIterator for BytesMut {
}
}
impl<'a> IntoIterator for &'a mut BytesMut {
type Item = u8;
type IntoIter = IntoIter<&'a mut BytesMut>;
impl<'a> IntoIterator for &'a BytesMut {
type Item = &'a u8;
type IntoIter = ::std::slice::Iter<'a, u8>;
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self)
self.as_ref().into_iter()
}
}
+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());
}