feat: remove impl IntoBuf for Cursor<Self>, impl Buf for Bytes, BytesMut, refactor iterators

This commit is contained in:
YetAnotherMinion
2019-06-06 16:59:44 -07:00
committed by Sean McArthur
parent 654a11c84c
commit d8134903de
12 changed files with 246 additions and 273 deletions
+83 -80
View File
@@ -1,10 +1,9 @@
use {IntoBuf, Buf, BufMut};
use buf::Iter;
use {Buf, BufMut};
use buf::IntoIter;
use debug;
use std::{cmp, fmt, mem, hash, ops, slice, ptr, usize};
use std::borrow::{Borrow, BorrowMut};
use std::io::Cursor;
use std::sync::atomic::{self, AtomicUsize, AtomicPtr};
use std::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
use std::iter::{FromIterator, Iterator};
@@ -738,22 +737,6 @@ impl Bytes {
self.inner.truncate(len);
}
/// Shortens the buffer, dropping the first `cnt` bytes and keeping the
/// rest.
///
/// This is the same function as `Buf::advance`, and in the next breaking
/// release of `bytes`, this implementation will be removed in favor of
/// having `Bytes` implement `Buf`.
///
/// # Panics
///
/// This function panics if `cnt` is greater than `self.len()`
#[inline]
pub fn advance(&mut self, cnt: usize) {
assert!(cnt <= self.len(), "cannot advance past `remaining`");
unsafe { self.inner.set_start(cnt); }
}
/// Clears the buffer, removing all data.
///
/// # Examples
@@ -887,21 +870,42 @@ impl Bytes {
self.extend_from_slice(other_inner.as_ref());
}
}
}
impl IntoBuf for Bytes {
type Buf = Cursor<Self>;
fn into_buf(self) -> Self::Buf {
Cursor::new(self)
/// Returns an iterator over the bytes contained by the buffer.
///
/// # Examples
///
/// ```
/// use bytes::{Buf, IntoBuf, Bytes};
///
/// let buf = Bytes::from(&b"abc"[..]);
/// let mut iter = buf.iter();
///
/// assert_eq!(iter.next().map(|b| *b), Some(b'a'));
/// assert_eq!(iter.next().map(|b| *b), Some(b'b'));
/// assert_eq!(iter.next().map(|b| *b), Some(b'c'));
/// assert_eq!(iter.next(), None);
/// ```
pub fn iter<'a>(&'a self) -> ::std::slice::Iter<'a, u8> {
self.bytes().iter()
}
}
impl<'a> IntoBuf for &'a Bytes {
type Buf = Cursor<Self>;
impl Buf for Bytes {
#[inline]
fn remaining(&self) -> usize {
self.len()
}
fn into_buf(self) -> Self::Buf {
Cursor::new(self)
#[inline]
fn bytes(&self) -> &[u8] {
&(self.inner.as_ref())
}
#[inline]
fn advance(&mut self, cnt: usize) {
assert!(cnt <= self.inner.as_ref().len(), "cannot advance past `remaining`");
unsafe { self.inner.set_start(cnt); }
}
}
@@ -1047,19 +1051,19 @@ impl Borrow<[u8]> for Bytes {
impl IntoIterator for Bytes {
type Item = u8;
type IntoIter = Iter<Cursor<Bytes>>;
type IntoIter = IntoIter<Bytes>;
fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
IntoIter::new(self)
}
}
impl<'a> IntoIterator for &'a Bytes {
impl<'a> IntoIterator for &'a mut Bytes {
type Item = u8;
type IntoIter = Iter<Cursor<&'a Bytes>>;
type IntoIter = IntoIter<&'a mut Bytes>;
fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
IntoIter::new(self)
}
}
@@ -1294,24 +1298,18 @@ impl BytesMut {
/// let mut buf = BytesMut::with_capacity(1024);
/// buf.put(&b"hello world"[..]);
///
/// let other = buf.take();
/// let other = buf.split();
///
/// assert!(buf.is_empty());
/// assert_eq!(1013, buf.capacity());
///
/// assert_eq!(other, b"hello world"[..]);
/// ```
pub fn take(&mut self) -> BytesMut {
pub fn split(&mut self) -> BytesMut {
let len = self.len();
self.split_to(len)
}
#[deprecated(since = "0.4.1", note = "use take instead")]
#[doc(hidden)]
pub fn drain(&mut self) -> BytesMut {
self.take()
}
/// Splits the buffer into two at the given index.
///
/// Afterwards `self` contains elements `[at, len)`, and the returned `BytesMut`
@@ -1376,22 +1374,6 @@ impl BytesMut {
self.inner.truncate(len);
}
/// Shortens the buffer, dropping the first `cnt` bytes and keeping the
/// rest.
///
/// This is the same function as `Buf::advance`, and in the next breaking
/// release of `bytes`, this implementation will be removed in favor of
/// having `BytesMut` implement `Buf`.
///
/// # Panics
///
/// This function panics if `cnt` is greater than `self.len()`
#[inline]
pub fn advance(&mut self, cnt: usize) {
assert!(cnt <= self.len(), "cannot advance past `remaining`");
unsafe { self.inner.set_start(cnt); }
}
/// Clears the buffer, removing all data.
///
/// # Examples
@@ -1501,7 +1483,7 @@ impl BytesMut {
/// buf.put(&[0; 64][..]);
///
/// let ptr = buf.as_ptr();
/// let other = buf.take();
/// let other = buf.split();
///
/// assert!(buf.is_empty());
/// assert_eq!(buf.capacity(), 64);
@@ -1570,6 +1552,43 @@ impl BytesMut {
self.extend_from_slice(other_inner.as_ref());
}
}
/// Returns an iterator over the bytes contained by the buffer.
///
/// # Examples
///
/// ```
/// use bytes::{Buf, BytesMut};
///
/// let buf = BytesMut::from(&b"abc"[..]);
/// let mut iter = buf.iter();
///
/// assert_eq!(iter.next().map(|b| *b), Some(b'a'));
/// assert_eq!(iter.next().map(|b| *b), Some(b'b'));
/// assert_eq!(iter.next().map(|b| *b), Some(b'c'));
/// assert_eq!(iter.next(), None);
/// ```
pub fn iter<'a>(&'a self) -> ::std::slice::Iter<'a, u8> {
self.bytes().iter()
}
}
impl Buf for BytesMut {
#[inline]
fn remaining(&self) -> usize {
self.len()
}
#[inline]
fn bytes(&self) -> &[u8] {
&(self.inner.as_ref())
}
#[inline]
fn advance(&mut self, cnt: usize) {
assert!(cnt <= self.inner.as_ref().len(), "cannot advance past `remaining`");
unsafe { self.inner.set_start(cnt); }
}
}
impl BufMut for BytesMut {
@@ -1617,22 +1636,6 @@ impl BufMut for BytesMut {
}
}
impl IntoBuf for BytesMut {
type Buf = Cursor<Self>;
fn into_buf(self) -> Self::Buf {
Cursor::new(self)
}
}
impl<'a> IntoBuf for &'a BytesMut {
type Buf = Cursor<&'a BytesMut>;
fn into_buf(self) -> Self::Buf {
Cursor::new(self)
}
}
impl AsRef<[u8]> for BytesMut {
#[inline]
fn as_ref(&self) -> &[u8] {
@@ -1797,19 +1800,19 @@ impl Clone for BytesMut {
impl IntoIterator for BytesMut {
type Item = u8;
type IntoIter = Iter<Cursor<BytesMut>>;
type IntoIter = IntoIter<BytesMut>;
fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
IntoIter::new(self)
}
}
impl<'a> IntoIterator for &'a BytesMut {
impl<'a> IntoIterator for &'a mut BytesMut {
type Item = u8;
type IntoIter = Iter<Cursor<&'a BytesMut>>;
type IntoIter = IntoIter<&'a mut BytesMut>;
fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
IntoIter::new(self)
}
}