Implement iterator adapter for Buf

This commit is contained in:
Carl Lerche
2017-03-01 10:03:28 -08:00
parent 4f8c565111
commit 8fec8a92ad
4 changed files with 173 additions and 2 deletions
+20 -1
View File
@@ -1,4 +1,4 @@
use super::{Take, Reader, FromBuf};
use super::{Take, Reader, Iter, FromBuf};
use byteorder::ByteOrder;
use std::{cmp, ptr};
@@ -546,4 +546,23 @@ 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)
}
}