summaryrefslogtreecommitdiffstats
path: root/third_party/rust/bytes/src/buf/iter.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /third_party/rust/bytes/src/buf/iter.rs
parentInitial commit. (diff)
downloadfirefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz
firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/bytes/src/buf/iter.rs')
-rw-r--r--third_party/rust/bytes/src/buf/iter.rs130
1 files changed, 130 insertions, 0 deletions
diff --git a/third_party/rust/bytes/src/buf/iter.rs b/third_party/rust/bytes/src/buf/iter.rs
new file mode 100644
index 0000000000..c694e3d418
--- /dev/null
+++ b/third_party/rust/bytes/src/buf/iter.rs
@@ -0,0 +1,130 @@
+use crate::Buf;
+
+/// Iterator over the bytes contained by the buffer.
+///
+/// # Examples
+///
+/// Basic usage:
+///
+/// ```
+/// use bytes::Bytes;
+///
+/// let buf = Bytes::from(&b"abc"[..]);
+/// let mut iter = buf.into_iter();
+///
+/// assert_eq!(iter.next(), Some(b'a'));
+/// assert_eq!(iter.next(), Some(b'b'));
+/// assert_eq!(iter.next(), Some(b'c'));
+/// assert_eq!(iter.next(), None);
+/// ```
+///
+/// [`iter`]: trait.Buf.html#method.iter
+/// [`Buf`]: trait.Buf.html
+#[derive(Debug)]
+pub struct IntoIter<T> {
+ inner: T,
+}
+
+impl<T> IntoIter<T> {
+ /// Creates an iterator over the bytes contained by the buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Bytes;
+ ///
+ /// let buf = Bytes::from_static(b"abc");
+ /// let mut iter = buf.into_iter();
+ ///
+ /// assert_eq!(iter.next(), Some(b'a'));
+ /// assert_eq!(iter.next(), Some(b'b'));
+ /// assert_eq!(iter.next(), Some(b'c'));
+ /// assert_eq!(iter.next(), None);
+ /// ```
+ pub fn new(inner: T) -> IntoIter<T> {
+ IntoIter { inner }
+ }
+
+ /// Consumes this `IntoIter`, returning the underlying value.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::{Buf, Bytes};
+ ///
+ /// let buf = Bytes::from(&b"abc"[..]);
+ /// let mut iter = buf.into_iter();
+ ///
+ /// assert_eq!(iter.next(), Some(b'a'));
+ ///
+ /// let buf = iter.into_inner();
+ /// assert_eq!(2, buf.remaining());
+ /// ```
+ pub fn into_inner(self) -> T {
+ self.inner
+ }
+
+ /// Gets a reference to the underlying `Buf`.
+ ///
+ /// It is inadvisable to directly read from the underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::{Buf, Bytes};
+ ///
+ /// let buf = Bytes::from(&b"abc"[..]);
+ /// let mut iter = buf.into_iter();
+ ///
+ /// assert_eq!(iter.next(), Some(b'a'));
+ ///
+ /// assert_eq!(2, iter.get_ref().remaining());
+ /// ```
+ pub fn get_ref(&self) -> &T {
+ &self.inner
+ }
+
+ /// Gets a mutable reference to the underlying `Buf`.
+ ///
+ /// It is inadvisable to directly read from the underlying `Buf`.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use bytes::{Buf, BytesMut};
+ ///
+ /// let buf = BytesMut::from(&b"abc"[..]);
+ /// let mut iter = buf.into_iter();
+ ///
+ /// assert_eq!(iter.next(), Some(b'a'));
+ ///
+ /// iter.get_mut().advance(1);
+ ///
+ /// assert_eq!(iter.next(), Some(b'c'));
+ /// ```
+ pub fn get_mut(&mut self) -> &mut T {
+ &mut self.inner
+ }
+}
+
+impl<T: Buf> Iterator for IntoIter<T> {
+ type Item = u8;
+
+ fn next(&mut self) -> Option<u8> {
+ if !self.inner.has_remaining() {
+ return None;
+ }
+
+ let b = self.inner.chunk()[0];
+ self.inner.advance(1);
+
+ Some(b)
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ let rem = self.inner.remaining();
+ (rem, Some(rem))
+ }
+}
+
+impl<T: Buf> ExactSizeIterator for IntoIter<T> {}