use core::cmp;
#[cfg(not(feature = "std"))]
use core::fmt;
#[cfg(feature = "std")]
use std::io;
pub trait Read {
type Error;
fn unexpected_eof() -> Self::Error;
fn read_all(&mut self, buf: &mut[u8]) -> Result<usize, Self::Error>;
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
if buf.len() != self.read_all(buf)? {
Err(Self::unexpected_eof())
}
else {
Ok(())
}
}
fn take(self, limit: u64) -> Take<Self>
where Self: Sized
{
Take { inner: self, limit }
}
#[inline]
fn by_ref(&mut self) -> &mut Self {
self
}
}
pub(crate) fn discard_to_end<R: Read, const BUF: usize>(rd: &mut R) -> Result<(), R::Error> {
use core::mem::MaybeUninit;
assert!(BUF != 0);
let mut data = MaybeUninit::<[u8; BUF]>::uninit();
let buf: &mut [u8; BUF] = unsafe {
data.assume_init_mut()
};
while 0 != rd.read_all(buf)? {}
Ok(())
}
#[derive(Debug)]
pub struct Take<R> {
limit: u64,
inner: R,
}
impl<R> Take<R> {
#[inline]
pub fn limit(&self) -> u64 {
self.limit
}
pub fn into_inner(self) -> R {
self.inner
}
pub fn get_ref(&self) -> &R {
&self.inner
}
pub fn get_mut(&mut self) -> &mut R {
&mut self.inner
}
}
impl<R: Read> Read for Take<R> {
type Error = R::Error;
#[inline]
fn unexpected_eof() -> Self::Error {
R::unexpected_eof()
}
#[inline]
fn read_all(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
if self.limit == 0 {
return Ok(0);
}
let max = cmp::min(buf.len() as u64, self.limit) as usize;
let n = self.inner.read_all(&mut buf[..max])?;
self.limit = self.limit.checked_sub(n as u64).expect("number of read bytes exceeds limit");
Ok(n)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
let len = buf.len();
if len as u64 > self.limit {
return Err(Self::unexpected_eof());
}
self.inner.read_exact(buf)?;
self.limit -= len as u64;
Ok(())
}
}
#[cfg(feature = "std")]
impl<R: io::Read> Read for R {
type Error = io::Error;
fn unexpected_eof() -> Self::Error {
io::Error::new(io::ErrorKind::UnexpectedEof, "failed to fill whole buffer")
}
fn read_all(&mut self, mut buf: &mut[u8]) -> Result<usize, Self::Error> {
let orig_len = buf.len();
while !buf.is_empty() {
match self.read(buf) {
Ok(0) => break,
Ok(n) => buf = &mut buf[n..],
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e)
}
}
Ok(orig_len - buf.len())
}
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
io::Read::read_exact(self, buf)
}
}
#[cfg(not(feature = "std"))]
#[derive(Debug)]
pub struct UnexpectedEofError;
#[cfg(not(feature = "std"))]
impl<R: Read + ?Sized> Read for &mut R {
type Error = R::Error;
fn unexpected_eof() -> Self::Error {
R::unexpected_eof()
}
fn read_all(&mut self, buf: &mut[u8]) -> Result<usize, Self::Error> {
R::read_all(*self, buf)
}
fn read_exact(&mut self, buf: &mut[u8]) -> Result<(), Self::Error> {
R::read_exact(*self, buf)
}
}
#[cfg(not(feature = "std"))]
impl<R: Read + ?Sized> Read for alloc::boxed::Box<R> {
type Error = R::Error;
fn unexpected_eof() -> Self::Error {
R::unexpected_eof()
}
fn read_all(&mut self, buf: &mut[u8]) -> Result<usize, Self::Error> {
R::read_all(self, buf)
}
fn read_exact(&mut self, buf: &mut[u8]) -> Result<(), Self::Error> {
R::read_exact(self, buf)
}
}
#[cfg(not(feature = "std"))]
impl fmt::Display for UnexpectedEofError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("failed to fill whole buffer")
}
}
#[cfg(not(feature = "std"))]
impl Read for &'_[u8] {
type Error = UnexpectedEofError;
#[inline]
fn unexpected_eof() -> Self::Error {
UnexpectedEofError
}
#[inline]
fn read_all(&mut self, buf: &mut[u8]) -> Result<usize, Self::Error> {
let amt = 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)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
if buf.len() > self.len() {
return Err(UnexpectedEofError);
}
self.read_all(buf)?;
Ok(())
}
}