• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::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::Bytes;
37     ///
38     /// let buf = Bytes::from_static(b"abc");
39     /// let mut iter = buf.into_iter();
40     ///
41     /// assert_eq!(iter.next(), Some(b'a'));
42     /// assert_eq!(iter.next(), Some(b'b'));
43     /// assert_eq!(iter.next(), Some(b'c'));
44     /// assert_eq!(iter.next(), None);
45     /// ```
new(inner: T) -> IntoIter<T>46     pub(crate) fn new(inner: T) -> IntoIter<T> {
47         IntoIter { inner }
48     }
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 impl<T: Buf> Iterator for IntoIter<T> {
113     type Item = u8;
114 
next(&mut self) -> Option<u8>115     fn next(&mut self) -> Option<u8> {
116         if !self.inner.has_remaining() {
117             return None;
118         }
119 
120         let b = self.inner.chunk()[0];
121         self.inner.advance(1);
122 
123         Some(b)
124     }
125 
size_hint(&self) -> (usize, Option<usize>)126     fn size_hint(&self) -> (usize, Option<usize>) {
127         let rem = self.inner.remaining();
128         (rem, Some(rem))
129     }
130 }
131 
132 impl<T: Buf> ExactSizeIterator for IntoIter<T> {}
133