oct 0.28.2

Octonary transcodings.
Documentation
// 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 [`Empty`] type and [`empty`] function.

use crate::io::{ErrorKind, Read, Result, Write};

/// A type that ignores reads and writes.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default)]
pub struct Empty;

/// Constructs a new [`Empty`] object.
#[inline(always)]
#[must_use]
pub const fn empty() -> Empty {
	Empty
}

impl Read for Empty {
	#[inline]
	fn read(&mut self, _buf: &mut [u8]) -> Result<usize> {
		// NOTE: We don't actually need to read anything.
		// :P
		Ok(0)
	}

	#[inline]
	fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
		if buf.is_empty() {
  			Ok(())
  		} else {
			// NOTE: We, sadly, cannot honour the read.
  			Err(ErrorKind::UnexpectedEof.into())
  		}
	}
}

impl Write for Empty {
	#[inline]
	fn write(&mut self, buf: &[u8]) -> Result<usize> {
		// NOTE: Yeah, it's written, alright.
		Ok(buf.len())
	}

	#[inline(always)]
	fn write_all(&mut self, _buf: &[u8]) -> Result<()> {
		Ok(())
	}

	#[inline(always)]
	fn flush(&mut self) -> Result<()> {
		Ok(())
	}
}

impl Write for &Empty {
	#[inline]
	fn write(&mut self, buf: &[u8]) -> Result<usize> {
		Ok(buf.len())
	}

	#[inline(always)]
	fn write_all(&mut self, _buf: &[u8]) -> Result<()> {
		Ok(())
	}

	#[inline(always)]
	fn flush(&mut self) -> Result<()> {
		Ok(())
	}
}