use_prelude!();
use ::std::io::Read;
pub unsafe trait ReadIntoUninit: Read
{
fn read_into_uninit<'buf>(
self: &'_ mut Self,
buf: Out<'buf, [u8]>,
) -> io::Result<&'buf mut [u8]>;
fn read_into_uninit_exact<'buf>(
self: &'_ mut Self,
mut buf: Out<'buf, [u8]>,
) -> io::Result<&'buf mut [u8]> {
{
let mut buf = buf.reborrow();
while buf.is_empty().not() {
match self.read_into_uninit(buf.r()).map(|it| it.len()) {
Ok(0) => {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
));
}
Ok(n) => {
buf = buf.get_out(n..).unwrap();
}
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => {
return Err(e);
}
}
}
}
Ok(unsafe {
buf.assume_init()
})
}
#[cfg(feature = "chain")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "chain")))]
fn chain<R: ReadIntoUninit>(self: Self, next: R) -> chain::Chain<Self, R>
where
Self: Sized,
{
chain::Chain {
first: self,
second: next,
first_done: false,
}
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! _private_auto_impl {(
#[derived_from(ReadIntoUninit)]
impl $( [$($generics:tt)*] )? Read for $T:ty
$(
where
$($where_clause:tt)*
)?
) => (
impl$(<$($generics)*>)? $crate::std::io::Read for $T
where
$( $($where_clause)* )?
{
#[inline]
fn read (self: &'_ mut Self, buf: &'_ mut [u8])
-> $crate::std::io::Result<usize>
{
<Self as $crate::read::ReadIntoUninit>::read_into_uninit(
self,
buf.as_out(),
).map(|x| x.len())
}
#[inline]
fn read_exact (self: &'_ mut Self, buf: &'_ mut [u8])
-> $crate::std::io::Result<()>
{
<Self as $crate::read::ReadIntoUninit>::read_into_uninit_exact(
self,
buf.as_out(),
).map(drop)
}
}
)}
#[doc(inline)]
pub use _private_auto_impl as auto_impl;
pub use crate::extension_traits::VecExtendFromReader;
mod impls;
#[cfg(feature = "chain")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "chain")))]
pub mod chain {
#![allow(missing_docs)]
use super::*;
#[cfg_attr(feature = "nightly", doc(cfg(feature = "chain")))]
#[derive(Debug)]
pub struct Chain<R1, R2>
where
R1: ReadIntoUninit,
R2: ReadIntoUninit,
{
pub(super) first: R1,
pub(super) second: R2,
pub(super) first_done: bool,
}
impl<R1, R2> Chain<R1, R2>
where
R1: ReadIntoUninit,
R2: ReadIntoUninit,
{
pub fn into_inner(self: Self) -> (R1, R2) {
let Self { first, second, .. } = self;
(first, second)
}
pub fn get_ref(self: &'_ Self) -> (&'_ R1, &'_ R2) {
let Self { first, second, .. } = self;
(first, second)
}
}
unsafe impl<R1, R2> ReadIntoUninit for Chain<R1, R2>
where
R1: ReadIntoUninit,
R2: ReadIntoUninit,
{
fn read_into_uninit<'buf>(
self: &'_ mut Self,
mut buf: Out<'buf, [u8]>,
) -> io::Result<&'buf mut [u8]> {
let len = buf.len();
if len == 0 {
return Ok(buf.copy_from_slice(&[]));
}
if self.first_done.not() {
let buf_ = self.first.read_into_uninit(buf.r())?;
if buf_.is_empty() {
self.first_done = true;
} else {
return unsafe {
let len = buf_.len();
let buf = buf.get_out(..len).unwrap();
Ok(buf.assume_init())
};
}
}
self.second.read_into_uninit(buf)
}
}
super::auto_impl! {
#[derived_from(ReadIntoUninit)]
impl[R1, R2] Read for Chain<R1, R2>
where
R1 : ReadIntoUninit,
R2 : ReadIntoUninit,
}
}