use core::fmt;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ReadExactError<E> {
UnexpectedEof,
Other(E),
}
impl<E: fmt::Debug> fmt::Display for ReadExactError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
#[cfg(feature = "std")]
impl<E: fmt::Debug> std::error::Error for ReadExactError<E> {}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WriteFmtError<E> {
FmtError,
Other(E),
}
impl<E: fmt::Debug> fmt::Display for WriteFmtError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
#[cfg(feature = "std")]
impl<E: fmt::Debug> std::error::Error for WriteFmtError<E> {}
pub trait Read: crate::Io {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), ReadExactError<Self::Error>> {
while !buf.is_empty() {
match self.read(buf) {
Ok(0) => break,
Ok(n) => buf = &mut buf[n..],
Err(e) => return Err(ReadExactError::Other(e)),
}
}
if !buf.is_empty() {
Err(ReadExactError::UnexpectedEof)
} else {
Ok(())
}
}
}
pub trait BufRead: crate::Io {
fn fill_buf(&mut self) -> Result<&[u8], Self::Error>;
fn consume(&mut self, amt: usize);
}
pub trait Write: crate::Io {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>;
fn flush(&mut self) -> Result<(), Self::Error>;
fn write_all(&mut self, mut buf: &[u8]) -> Result<(), Self::Error> {
while !buf.is_empty() {
match self.write(buf) {
Ok(0) => panic!("zero-length write."),
Ok(n) => buf = &buf[n..],
Err(e) => return Err(e),
}
}
Ok(())
}
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<(), WriteFmtError<Self::Error>> {
struct Adapter<'a, T: Write + ?Sized + 'a> {
inner: &'a mut T,
error: Result<(), T::Error>,
}
impl<T: Write + ?Sized> fmt::Write for Adapter<'_, T> {
fn write_str(&mut self, s: &str) -> fmt::Result {
match self.inner.write_all(s.as_bytes()) {
Ok(()) => Ok(()),
Err(e) => {
self.error = Err(e);
Err(fmt::Error)
}
}
}
}
let mut output = Adapter {
inner: self,
error: Ok(()),
};
match fmt::write(&mut output, fmt) {
Ok(()) => Ok(()),
Err(..) => match output.error {
Err(e) => Err(WriteFmtError::Other(e)),
Ok(()) => Err(WriteFmtError::FmtError),
},
}
}
}
pub trait Seek: crate::Io {
fn seek(&mut self, pos: crate::SeekFrom) -> Result<u64, Self::Error>;
fn rewind(&mut self) -> Result<(), Self::Error> {
self.seek(crate::SeekFrom::Start(0))?;
Ok(())
}
fn stream_position(&mut self) -> Result<u64, Self::Error> {
self.seek(crate::SeekFrom::Current(0))
}
}
impl<T: ?Sized + Read> Read for &mut T {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
T::read(self, buf)
}
}
impl<T: ?Sized + BufRead> BufRead for &mut T {
fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
T::fill_buf(self)
}
fn consume(&mut self, amt: usize) {
T::consume(self, amt)
}
}
impl<T: ?Sized + Write> Write for &mut T {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
T::write(self, buf)
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
T::flush(self)
}
}
impl<T: ?Sized + Seek> Seek for &mut T {
#[inline]
fn seek(&mut self, pos: crate::SeekFrom) -> Result<u64, Self::Error> {
T::seek(self, pos)
}
}
impl Read for &[u8] {
#[inline]
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)
}
}
impl BufRead for &[u8] {
#[inline]
fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
Ok(*self)
}
#[inline]
fn consume(&mut self, amt: usize) {
*self = &self[amt..];
}
}
impl Write for &mut [u8] {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let amt = core::cmp::min(buf.len(), self.len());
let (a, b) = core::mem::replace(self, &mut []).split_at_mut(amt);
a.copy_from_slice(&buf[..amt]);
*self = b;
Ok(amt)
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + Read> Read for alloc::boxed::Box<T> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
T::read(self, buf)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + BufRead> BufRead for alloc::boxed::Box<T> {
fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
T::fill_buf(self)
}
fn consume(&mut self, amt: usize) {
T::consume(self, amt)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + Write> Write for alloc::boxed::Box<T> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
T::write(self, buf)
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
T::flush(self)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + Seek> Seek for alloc::boxed::Box<T> {
#[inline]
fn seek(&mut self, pos: crate::SeekFrom) -> Result<u64, Self::Error> {
T::seek(self, pos)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl Write for alloc::vec::Vec<u8> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.extend_from_slice(buf);
Ok(buf.len())
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}