mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-20 00:00:10 +02:00
Remove io::Cursor, and implement Buf/BufMut for slices instead (#261)
This commit is contained in:
committed by
Carl Lerche
parent
d8134903de
commit
55aa530dc1
+89
-112
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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();
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user