| 1 |
use crate::Buf; |
| 2 |
|
| 3 |
/// Iterator over the bytes contained by the buffer. |
| 4 |
/// |
| 5 |
/// # Examples |
| 6 |
/// |
| 7 |
/// Basic usage: |
| 8 |
/// |
| 9 |
/// ``` |
| 10 |
/// use bytes::Bytes; |
| 11 |
/// |
| 12 |
/// let buf = Bytes::from(&b"abc"[..]); |
| 13 |
/// let mut iter = buf.into_iter(); |
| 14 |
/// |
| 15 |
/// assert_eq!(iter.next(), Some(b'a')); |
| 16 |
/// assert_eq!(iter.next(), Some(b'b')); |
| 17 |
/// assert_eq!(iter.next(), Some(b'c')); |
| 18 |
/// assert_eq!(iter.next(), None); |
| 19 |
/// ``` |
| 20 |
#[derive(Debug)] |
| 21 |
pub struct IntoIter<T> { |
| 22 |
inner: T, |
| 23 |
} |
| 24 |
|
| 25 |
impl<T> IntoIter<T> { |
| 26 |
/// Creates an iterator over the bytes contained by the buffer. |
| 27 |
/// |
| 28 |
/// # Examples |
| 29 |
/// |
| 30 |
/// ``` |
| 31 |
/// use bytes::Bytes; |
| 32 |
/// |
| 33 |
/// let buf = Bytes::from_static(b"abc"); |
| 34 |
/// let mut iter = buf.into_iter(); |
| 35 |
/// |
| 36 |
/// assert_eq!(iter.next(), Some(b'a')); |
| 37 |
/// assert_eq!(iter.next(), Some(b'b')); |
| 38 |
/// assert_eq!(iter.next(), Some(b'c')); |
| 39 |
/// assert_eq!(iter.next(), None); |
| 40 |
/// ``` |
| 41 |
pub fn new(inner: T) -> IntoIter<T> { |
| 42 |
IntoIter { inner } |
| 43 |
} |
| 44 |
|
| 45 |
/// Consumes this `IntoIter`, returning the underlying value. |
| 46 |
/// |
| 47 |
/// # Examples |
| 48 |
/// |
| 49 |
/// ```rust |
| 50 |
/// use bytes::{Buf, Bytes}; |
| 51 |
/// |
| 52 |
/// let buf = Bytes::from(&b"abc"[..]); |
| 53 |
/// let mut iter = buf.into_iter(); |
| 54 |
/// |
| 55 |
/// assert_eq!(iter.next(), Some(b'a')); |
| 56 |
/// |
| 57 |
/// let buf = iter.into_inner(); |
| 58 |
/// assert_eq!(2, buf.remaining()); |
| 59 |
/// ``` |
| 60 |
pub fn into_inner(self) -> T { |
| 61 |
self.inner |
| 62 |
} |
| 63 |
|
| 64 |
/// Gets a reference to the underlying `Buf`. |
| 65 |
/// |
| 66 |
/// It is inadvisable to directly read from the underlying `Buf`. |
| 67 |
/// |
| 68 |
/// # Examples |
| 69 |
/// |
| 70 |
/// ```rust |
| 71 |
/// use bytes::{Buf, Bytes}; |
| 72 |
/// |
| 73 |
/// let buf = Bytes::from(&b"abc"[..]); |
| 74 |
/// let mut iter = buf.into_iter(); |
| 75 |
/// |
| 76 |
/// assert_eq!(iter.next(), Some(b'a')); |
| 77 |
/// |
| 78 |
/// assert_eq!(2, iter.get_ref().remaining()); |
| 79 |
/// ``` |
| 80 |
pub fn get_ref(&self) -> &T { |
| 81 |
&self.inner |
| 82 |
} |
| 83 |
|
| 84 |
/// Gets a mutable reference to the underlying `Buf`. |
| 85 |
/// |
| 86 |
/// It is inadvisable to directly read from the underlying `Buf`. |
| 87 |
/// |
| 88 |
/// # Examples |
| 89 |
/// |
| 90 |
/// ```rust |
| 91 |
/// use bytes::{Buf, BytesMut}; |
| 92 |
/// |
| 93 |
/// let buf = BytesMut::from(&b"abc"[..]); |
| 94 |
/// let mut iter = buf.into_iter(); |
| 95 |
/// |
| 96 |
/// assert_eq!(iter.next(), Some(b'a')); |
| 97 |
/// |
| 98 |
/// iter.get_mut().advance(1); |
| 99 |
/// |
| 100 |
/// assert_eq!(iter.next(), Some(b'c')); |
| 101 |
/// ``` |
| 102 |
pub fn get_mut(&mut self) -> &mut T { |
| 103 |
&mut self.inner |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl<T: Buf> Iterator for IntoIter<T> { |
| 108 |
type Item = u8; |
| 109 |
|
| 110 |
fn next(&mut self) -> Option<u8> { |
| 111 |
if !self.inner.has_remaining() { |
| 112 |
return None; |
| 113 |
} |
| 114 |
|
| 115 |
let b = self.inner.chunk()[0]; |
| 116 |
self.inner.advance(1); |
| 117 |
|
| 118 |
Some(b) |
| 119 |
} |
| 120 |
|
| 121 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 122 |
let rem = self.inner.remaining(); |
| 123 |
(rem, Some(rem)) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
impl<T: Buf> ExactSizeIterator for IntoIter<T> {} |