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
+32 -15
View File
@@ -12,7 +12,7 @@ use Buf;
/// use bytes::{Buf, IntoBuf, Bytes};
///
/// let buf = Bytes::from(&b"abc"[..]).into_buf();
/// let mut iter = buf.iter();
/// let mut iter = buf.into_iter();
///
/// assert_eq!(iter.next(), Some(b'a'));
/// assert_eq!(iter.next(), Some(b'b'));
@@ -23,12 +23,31 @@ use Buf;
/// [`iter`]: trait.Buf.html#method.iter
/// [`Buf`]: trait.Buf.html
#[derive(Debug)]
pub struct Iter<T> {
pub struct IntoIter<T> {
inner: T,
}
impl<T> Iter<T> {
/// Consumes this `Iter`, returning the underlying value.
impl<T> IntoIter<T> {
/// Creates an iterator over the bytes contained by the buffer.
///
/// # Examples
///
/// ```
/// use bytes::{Buf, Bytes};
/// use bytes::buf::IntoIter;
///
/// let buf = Bytes::from_static(b"abc");
/// let mut iter = IntoIter::new(buf);
///
/// 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);
/// ```
pub fn new(inner: T) -> IntoIter<T> {
IntoIter { inner: inner }
}
/// Consumes this `IntoIter`, returning the underlying value.
///
/// # Examples
///
@@ -36,7 +55,7 @@ impl<T> Iter<T> {
/// use bytes::{Buf, IntoBuf, Bytes};
///
/// let buf = Bytes::from(&b"abc"[..]).into_buf();
/// let mut iter = buf.iter();
/// let mut iter = buf.into_iter();
///
/// assert_eq!(iter.next(), Some(b'a'));
///
@@ -57,7 +76,7 @@ impl<T> Iter<T> {
/// use bytes::{Buf, IntoBuf, Bytes};
///
/// let buf = Bytes::from(&b"abc"[..]).into_buf();
/// let mut iter = buf.iter();
/// let mut iter = buf.into_iter();
///
/// assert_eq!(iter.next(), Some(b'a'));
///
@@ -76,25 +95,22 @@ impl<T> Iter<T> {
/// ```rust
/// use bytes::{Buf, IntoBuf, BytesMut};
///
/// let buf = BytesMut::from(&b"abc"[..]).into_buf();
/// let mut iter = buf.iter();
/// let buf = BytesMut::from(&b"abc"[..]);
/// let mut iter = buf.into_iter();
///
/// assert_eq!(iter.next(), Some(b'a'));
///
/// iter.get_mut().set_position(0);
/// iter.get_mut().advance(1);
///
/// assert_eq!(iter.next(), Some(b'a'));
/// assert_eq!(iter.next(), Some(b'c'));
/// ```
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
}
}
pub fn new<T>(inner: T) -> Iter<T> {
Iter { inner: inner }
}
impl<T: Buf> Iterator for Iter<T> {
impl<T: Buf> Iterator for IntoIter<T> {
type Item = u8;
fn next(&mut self) -> Option<u8> {
@@ -104,6 +120,7 @@ impl<T: Buf> Iterator for Iter<T> {
let b = self.inner.bytes()[0];
self.inner.advance(1);
Some(b)
}
@@ -113,4 +130,4 @@ impl<T: Buf> Iterator for Iter<T> {
}
}
impl<T: Buf> ExactSizeIterator for Iter<T> { }
impl<T: Buf> ExactSizeIterator for IntoIter<T> { }