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