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