mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-16 00:00:15 +02:00
feat: remove impl IntoBuf for Cursor<Self>, impl Buf for Bytes, BytesMut, refactor iterators
This commit is contained in:
committed by
Sean McArthur
parent
654a11c84c
commit
d8134903de
+64
-112
@@ -1,4 +1,4 @@
|
||||
use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
|
||||
use super::{IntoBuf, Take, Reader, FromBuf, Chain};
|
||||
use byteorder::{BigEndian, ByteOrder, LittleEndian};
|
||||
use iovec::IoVec;
|
||||
|
||||
@@ -47,10 +47,10 @@ macro_rules! buf_get_impl {
|
||||
/// The simplest `Buf` is a `Cursor` wrapping a `[u8]`.
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
/// use std::io::Cursor;
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"hello world");
|
||||
/// let mut buf = Bytes::from_static(b"hello world");
|
||||
///
|
||||
/// assert_eq!(b'h', buf.get_u8());
|
||||
/// assert_eq!(b'e', buf.get_u8());
|
||||
@@ -71,10 +71,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"hello world");
|
||||
/// let mut buf = Bytes::from_static(b"hello world");
|
||||
///
|
||||
/// assert_eq!(buf.remaining(), 11);
|
||||
///
|
||||
@@ -100,10 +99,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"hello world");
|
||||
/// let mut buf = Bytes::from_static(b"hello world");
|
||||
///
|
||||
/// assert_eq!(buf.bytes(), b"hello world");
|
||||
///
|
||||
@@ -167,10 +165,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"hello world");
|
||||
/// let mut buf = Bytes::from_static(b"hello world");
|
||||
///
|
||||
/// assert_eq!(buf.bytes(), b"hello world");
|
||||
///
|
||||
@@ -199,10 +196,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"a");
|
||||
/// let mut buf = Bytes::from_static(b"a");
|
||||
///
|
||||
/// assert!(buf.has_remaining());
|
||||
///
|
||||
@@ -222,10 +218,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"hello world");
|
||||
/// let mut buf = Bytes::from_static(b"hello world");
|
||||
/// let mut dst = [0; 5];
|
||||
///
|
||||
/// buf.copy_to_slice(&mut dst);
|
||||
@@ -265,10 +260,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x08 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x08 hello");
|
||||
/// assert_eq!(8, buf.get_u8());
|
||||
/// ```
|
||||
///
|
||||
@@ -289,10 +283,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x08 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x08 hello");
|
||||
/// assert_eq!(8, buf.get_i8());
|
||||
/// ```
|
||||
///
|
||||
@@ -313,10 +306,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x08\x09 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x08\x09 hello");
|
||||
/// assert_eq!(0x0809, buf.get_u16());
|
||||
/// ```
|
||||
///
|
||||
@@ -334,10 +326,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x09\x08 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x09\x08 hello");
|
||||
/// assert_eq!(0x0809, buf.get_u16_le());
|
||||
/// ```
|
||||
///
|
||||
@@ -355,10 +346,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x08\x09 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x08\x09 hello");
|
||||
/// assert_eq!(0x0809, buf.get_i16());
|
||||
/// ```
|
||||
///
|
||||
@@ -376,10 +366,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x09\x08 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x09\x08 hello");
|
||||
/// assert_eq!(0x0809, buf.get_i16_le());
|
||||
/// ```
|
||||
///
|
||||
@@ -397,10 +386,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x08\x09\xA0\xA1 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x08\x09\xA0\xA1 hello");
|
||||
/// assert_eq!(0x0809A0A1, buf.get_u32());
|
||||
/// ```
|
||||
///
|
||||
@@ -418,10 +406,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\xA1\xA0\x09\x08 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\xA1\xA0\x09\x08 hello");
|
||||
/// assert_eq!(0x0809A0A1, buf.get_u32_le());
|
||||
/// ```
|
||||
///
|
||||
@@ -439,10 +426,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x08\x09\xA0\xA1 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x08\x09\xA0\xA1 hello");
|
||||
/// assert_eq!(0x0809A0A1, buf.get_i32());
|
||||
/// ```
|
||||
///
|
||||
@@ -460,10 +446,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\xA1\xA0\x09\x08 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\xA1\xA0\x09\x08 hello");
|
||||
/// assert_eq!(0x0809A0A1, buf.get_i32_le());
|
||||
/// ```
|
||||
///
|
||||
@@ -481,10 +466,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello");
|
||||
/// assert_eq!(0x0102030405060708, buf.get_u64());
|
||||
/// ```
|
||||
///
|
||||
@@ -502,10 +486,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello");
|
||||
/// assert_eq!(0x0102030405060708, buf.get_u64_le());
|
||||
/// ```
|
||||
///
|
||||
@@ -523,10 +506,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello");
|
||||
/// assert_eq!(0x0102030405060708, buf.get_i64());
|
||||
/// ```
|
||||
///
|
||||
@@ -544,10 +526,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello");
|
||||
/// assert_eq!(0x0102030405060708, buf.get_i64_le());
|
||||
/// ```
|
||||
///
|
||||
@@ -657,10 +638,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x01\x02\x03 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x01\x02\x03 hello");
|
||||
/// assert_eq!(0x010203, buf.get_uint(3));
|
||||
/// ```
|
||||
///
|
||||
@@ -678,10 +658,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x03\x02\x01 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x03\x02\x01 hello");
|
||||
/// assert_eq!(0x010203, buf.get_uint_le(3));
|
||||
/// ```
|
||||
///
|
||||
@@ -699,10 +678,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x01\x02\x03 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x01\x02\x03 hello");
|
||||
/// assert_eq!(0x010203, buf.get_int(3));
|
||||
/// ```
|
||||
///
|
||||
@@ -720,10 +698,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x03\x02\x01 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x03\x02\x01 hello");
|
||||
/// assert_eq!(0x010203, buf.get_int_le(3));
|
||||
/// ```
|
||||
///
|
||||
@@ -742,10 +719,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x3F\x99\x99\x9A hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x3F\x99\x99\x9A hello");
|
||||
/// assert_eq!(1.2f32, buf.get_f32());
|
||||
/// ```
|
||||
///
|
||||
@@ -764,10 +740,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x9A\x99\x99\x3F hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x9A\x99\x99\x3F hello");
|
||||
/// assert_eq!(1.2f32, buf.get_f32_le());
|
||||
/// ```
|
||||
///
|
||||
@@ -786,10 +761,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello");
|
||||
/// assert_eq!(1.2f64, buf.get_f64());
|
||||
/// ```
|
||||
///
|
||||
@@ -808,10 +782,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Buf;
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new(b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello");
|
||||
/// let mut buf = Bytes::from_static(b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello");
|
||||
/// assert_eq!(1.2f64, buf.get_f64_le());
|
||||
/// ```
|
||||
///
|
||||
@@ -854,10 +827,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::{Buf, BufMut};
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, BufMut, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new("hello world").take(5);
|
||||
/// let mut buf = Bytes::from_static(b"hello world").take(5);
|
||||
/// let mut dst = vec![];
|
||||
///
|
||||
/// dst.put(&mut buf);
|
||||
@@ -882,13 +854,13 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::{Bytes, Buf, IntoBuf};
|
||||
/// use bytes::{Bytes, Buf};
|
||||
/// use bytes::buf::Chain;
|
||||
///
|
||||
/// let buf = Bytes::from(&b"hello "[..]).into_buf()
|
||||
/// let chain = Bytes::from_static(b"hello ")
|
||||
/// .chain(Bytes::from(&b"world"[..]));
|
||||
///
|
||||
/// let full: Bytes = buf.collect();
|
||||
/// let full: Bytes = chain.collect();
|
||||
/// assert_eq!(full[..], b"hello world"[..]);
|
||||
/// ```
|
||||
fn chain<U>(self, next: U) -> Chain<Self, U::Buf>
|
||||
@@ -905,10 +877,9 @@ pub trait Buf {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::{Buf, BufMut};
|
||||
/// use std::io::Cursor;
|
||||
/// use bytes::{Buf, BufMut, Bytes};
|
||||
///
|
||||
/// let mut buf = Cursor::new("hello world");
|
||||
/// let mut buf = Bytes::from_static(b"hello world");
|
||||
/// let mut dst = vec![];
|
||||
///
|
||||
/// {
|
||||
@@ -951,25 +922,6 @@ pub trait Buf {
|
||||
fn reader(self) -> Reader<Self> where Self: Sized {
|
||||
super::reader::new(self)
|
||||
}
|
||||
|
||||
/// Returns an iterator over the bytes contained by the buffer.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::{Buf, IntoBuf, Bytes};
|
||||
///
|
||||
/// let buf = Bytes::from(&b"abc"[..]).into_buf();
|
||||
/// let mut iter = buf.iter();
|
||||
///
|
||||
/// assert_eq!(iter.next(), Some(b'a'));
|
||||
/// assert_eq!(iter.next(), Some(b'b'));
|
||||
/// assert_eq!(iter.next(), Some(b'c'));
|
||||
/// assert_eq!(iter.next(), None);
|
||||
/// ```
|
||||
fn iter(self) -> Iter<Self> where Self: Sized {
|
||||
super::iter::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
|
||||
|
||||
Reference in New Issue
Block a user