pub trait Write {
type Error;
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>;
fn flush(&mut self) -> Result<(), Self::Error>;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>;
}
#[cfg(feature = "std")]
impl<T> Write for T
where
T: std::io::Write,
{
type Error = std::io::Error;
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
std::io::Write::write(self, buf)
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
std::io::Write::flush(self)
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
std::io::Write::write_all(self, buf)
}
}
pub trait Read {
type Error;
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>;
}
#[cfg(feature = "std")]
impl<T> Read for T
where
T: std::io::Read,
{
type Error = std::io::Error;
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
self.read(buf)
}
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
self.read_exact(buf)
}
}
#[cfg(not(feature = "std"))]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum IoError {
UnexpectedEof,
WriteZero,
}
#[cfg(not(feature = "std"))]
impl core::fmt::Display for IoError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
IoError::UnexpectedEof => f.write_str("Failed to fill whole buffer"),
IoError::WriteZero => f.write_str("Failed to write whole buffer"),
}
}
}
#[cfg(not(feature = "std"))]
impl Read for &[u8] {
type Error = IoError;
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let amt = core::cmp::min(buf.len(), self.len());
let (a, b) = self.split_at(amt);
if amt == 1 {
buf[0] = a[0];
} else {
buf[..amt].copy_from_slice(a);
}
*self = b;
Ok(amt)
}
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
if buf.len() > self.len() {
return Err(IoError::UnexpectedEof);
}
let (a, b) = self.split_at(buf.len());
if buf.len() == 1 {
buf[0] = a[0];
} else {
buf.copy_from_slice(a);
}
*self = b;
Ok(())
}
}
#[cfg(not(feature = "std"))]
impl Write for &mut [u8] {
type Error = IoError;
#[inline]
fn write(&mut self, data: &[u8]) -> Result<usize, Self::Error> {
let amt = core::cmp::min(data.len(), self.len());
let (a, b) = core::mem::replace(self, &mut []).split_at_mut(amt);
a.copy_from_slice(&data[..amt]);
*self = b;
Ok(amt)
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
#[inline]
fn write_all(&mut self, data: &[u8]) -> Result<(), Self::Error> {
if self.write(data)? == data.len() {
Ok(())
} else {
Err(IoError::WriteZero)
}
}
}
#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl Write for alloc::vec::Vec<u8> {
type Error = core::convert::Infallible;
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.extend_from_slice(buf);
Ok(buf.len())
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.extend_from_slice(buf);
Ok(())
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(not(feature = "std"))]
impl<R: Read + ?Sized> Read for &mut R {
type Error = R::Error;
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
(**self).read(buf)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
(**self).read_exact(buf)
}
}
#[cfg(not(feature = "std"))]
impl<W: Write + ?Sized> Write for &mut W {
type Error = W::Error;
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
(**self).write(buf)
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
(**self).flush()
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
(**self).write_all(buf)
}
}
#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl<R: Read + ?Sized> Read for alloc::boxed::Box<R> {
type Error = R::Error;
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
(**self).read(buf)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
(**self).read_exact(buf)
}
}
#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl<W: Write + ?Sized> Write for alloc::boxed::Box<W> {
type Error = W::Error;
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
(**self).write(buf)
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
(**self).flush()
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
(**self).write_all(buf)
}
}