oct 0.26.0

Octonary transcodings.
Documentation
// Copyright 2024-2025 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)),
			}
		}
	}
}