use oct::serdes::Result;
#[cfg(feature = "std")]
use std::io;
#[cfg(not(feature = "std"))]
use oct::serdes::Error;
#[cfg_attr(not(feature = "std"), doc = "[`std::io::Read`]: <https://doc.rust-lang.org/nightly/std/io/trait.Read.html>")]
pub trait Read {
#[cfg_attr(not(feature = "std"), doc = "[`std::io::Read::read`]: <https://doc.rust-lang.org/nightly/std/io/trait.Read.html#tymethod.read>")]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
#[cfg_attr(not(feature = "std"), doc = "[`std::io::Read::reaD_exact`]: <https://doc.rust-lang.org/nightly/std/io/trait.Read.html#tymethod.read_exact>")]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>;
}
#[cfg(feature = "std")]
impl<T: ?Sized + io::Read> Read for T {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
io::Read::read(self, buf).map_err(Into::into)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
io::Read::read_exact(self, buf).map_err(Into::into)
}
}
#[cfg(not(feature = "std"))]
impl<T: ?Sized + Read> Read for &mut T {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
T::read(*self, buf)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
T::read_exact(*self, buf)
}
}
#[cfg(not(feature = "std"))]
impl Read for &[u8] {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let count = buf.len().min(self.len());
let (slot, remaining) = self.split_at(count);
buf.copy_from_slice(slot);
*self = remaining;
Ok(count)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
if self.read(buf)? == buf.len() {
Ok(())
} else {
Err(Error::unexpected_eof())
}
}
}