/rust/registry/src/index.crates.io-6f17d22bba15001f/bytes-1.6.1/src/buf/iter.rs
Line | Count | Source (jump to first uncovered line) |
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 | 0 | pub fn new(inner: T) -> IntoIter<T> { |
42 | 0 | IntoIter { inner } |
43 | 0 | } Unexecuted instantiation: <bytes::buf::iter::IntoIter<bytes::bytes::Bytes>>::new Unexecuted instantiation: <bytes::buf::iter::IntoIter<bytes::bytes_mut::BytesMut>>::new |
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 | 0 | pub fn into_inner(self) -> T { |
61 | 0 | self.inner |
62 | 0 | } |
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 | 0 | pub fn get_ref(&self) -> &T { |
81 | 0 | &self.inner |
82 | 0 | } |
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 | 0 | pub fn get_mut(&mut self) -> &mut T { |
103 | 0 | &mut self.inner |
104 | 0 | } |
105 | | } |
106 | | |
107 | | impl<T: Buf> Iterator for IntoIter<T> { |
108 | | type Item = u8; |
109 | | |
110 | 0 | fn next(&mut self) -> Option<u8> { |
111 | 0 | if !self.inner.has_remaining() { |
112 | 0 | return None; |
113 | 0 | } |
114 | 0 |
|
115 | 0 | let b = self.inner.chunk()[0]; |
116 | 0 | self.inner.advance(1); |
117 | 0 |
|
118 | 0 | Some(b) |
119 | 0 | } |
120 | | |
121 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
122 | 0 | let rem = self.inner.remaining(); |
123 | 0 | (rem, Some(rem)) |
124 | 0 | } |
125 | | } |
126 | | |
127 | | impl<T: Buf> ExactSizeIterator for IntoIter<T> {} |