1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2024-2026 Gabriel Bjørnager Jensen.
//
// This Source Code Form is subject to the terms of
// the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you
// can obtain one at:
// <https://mozilla.org/MPL/2.0/>.
//! The [`Bytes`] type.
mod test;
use crate::io::{Read, Result};
use core::slice;
// NOTE: We could require `?Sized` but `std`
// doesn't do such.
/// A byte iterator over a reader.
#[must_use]
#[derive(Debug)]
pub struct Bytes<R> {
/// The reader.
reader: R,
}
impl<R> Bytes<R> {
/// Constructs a new `Bytes` iterator.
#[inline]
pub(super) fn new(r: R) -> Self
where
R: Read,
{
Self { reader: r }
}
}
impl<R: Read> Iterator for Bytes<R> {
type Item = Result<u8>;
fn next(&mut self) -> Option<Self::Item> {
let mut byte = u8::default();
loop {
match self.reader.read(slice::from_mut(&mut byte)) {
Ok(0) => break None,
Ok(..) => break Some(Ok(byte)),
Err(ref e) if e.is_interrupted() => {}
Err(e) => break Some(Err(e)),
}
}
}
}