|
|
|
@ -11,10 +11,10 @@ use read_to_string::ReadToStringFuture;
|
|
|
|
|
use read_vectored::ReadVectoredFuture;
|
|
|
|
|
|
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
|
|
use cfg_if::cfg_if;
|
|
|
|
|
|
|
|
|
|
use crate::io::IoSliceMut;
|
|
|
|
|
use crate::utils::extension_trait;
|
|
|
|
|
|
|
|
|
|
cfg_if! {
|
|
|
|
|
if #[cfg(feature = "docs")] {
|
|
|
|
@ -23,10 +23,10 @@ cfg_if! {
|
|
|
|
|
|
|
|
|
|
use crate::io;
|
|
|
|
|
use crate::task::{Context, Poll};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
pub struct ImplFuture<'a, T>(std::marker::PhantomData<&'a T>);
|
|
|
|
|
|
|
|
|
|
extension_trait! {
|
|
|
|
|
/// Allows reading from a byte stream.
|
|
|
|
|
///
|
|
|
|
|
/// This trait is a re-export of [`futures::io::AsyncRead`] and is an async version of
|
|
|
|
@ -45,7 +45,7 @@ cfg_if! {
|
|
|
|
|
/// https://docs.rs/futures-preview/0.3.0-alpha.17/futures/io/trait.AsyncRead.html
|
|
|
|
|
/// [`poll_read`]: #tymethod.poll_read
|
|
|
|
|
/// [`poll_read_vectored`]: #method.poll_read_vectored
|
|
|
|
|
pub trait Read {
|
|
|
|
|
pub trait Read [ReadExt: futures_io::AsyncRead] {
|
|
|
|
|
/// Attempt to read from the `AsyncRead` into `buf`.
|
|
|
|
|
fn poll_read(
|
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
@ -91,11 +91,11 @@ cfg_if! {
|
|
|
|
|
/// #
|
|
|
|
|
/// # Ok(()) }) }
|
|
|
|
|
/// ```
|
|
|
|
|
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<'a, io::Result<usize>>
|
|
|
|
|
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> impl Future<Output = io::Result<usize>> + 'a [ReadFuture<'a, Self>]
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin
|
|
|
|
|
{
|
|
|
|
|
unreachable!()
|
|
|
|
|
ReadFuture { reader: self, buf }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Like [`read`], except that it reads into a slice of buffers.
|
|
|
|
@ -111,11 +111,11 @@ cfg_if! {
|
|
|
|
|
fn read_vectored<'a>(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
bufs: &'a mut [IoSliceMut<'a>],
|
|
|
|
|
) -> ImplFuture<'a, io::Result<usize>>
|
|
|
|
|
) -> impl Future<Output = io::Result<usize>> + 'a [ReadVectoredFuture<'a, Self>]
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
unreachable!()
|
|
|
|
|
ReadVectoredFuture { reader: self, bufs }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Reads all bytes from the byte stream.
|
|
|
|
@ -146,11 +146,16 @@ cfg_if! {
|
|
|
|
|
fn read_to_end<'a>(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
buf: &'a mut Vec<u8>,
|
|
|
|
|
) -> ImplFuture<'a, io::Result<usize>>
|
|
|
|
|
) -> impl Future<Output = io::Result<usize>> + 'a [ReadToEndFuture<'a, Self>]
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
unreachable!()
|
|
|
|
|
let start_len = buf.len();
|
|
|
|
|
ReadToEndFuture {
|
|
|
|
|
reader: self,
|
|
|
|
|
buf,
|
|
|
|
|
start_len,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Reads all bytes from the byte stream and appends them into a string.
|
|
|
|
@ -178,11 +183,17 @@ cfg_if! {
|
|
|
|
|
fn read_to_string<'a>(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
buf: &'a mut String,
|
|
|
|
|
) -> ImplFuture<'a, io::Result<usize>>
|
|
|
|
|
) -> impl Future<Output = io::Result<usize>> + 'a [ReadToStringFuture<'a, Self>]
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
unreachable!()
|
|
|
|
|
let start_len = buf.len();
|
|
|
|
|
ReadToStringFuture {
|
|
|
|
|
reader: self,
|
|
|
|
|
bytes: unsafe { mem::replace(buf.as_mut_vec(), Vec::new()) },
|
|
|
|
|
buf,
|
|
|
|
|
start_len,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Reads the exact number of bytes required to fill `buf`.
|
|
|
|
@ -222,11 +233,11 @@ cfg_if! {
|
|
|
|
|
/// #
|
|
|
|
|
/// # Ok(()) }) }
|
|
|
|
|
/// ```
|
|
|
|
|
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<'a, io::Result<()>>
|
|
|
|
|
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> impl Future<Output = io::Result<()>> + 'a [ReadExactFuture<'a, Self>]
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
unreachable!()
|
|
|
|
|
ReadExactFuture { reader: self, buf }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -273,61 +284,4 @@ cfg_if! {
|
|
|
|
|
unreachable!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
pub use futures_io::AsyncRead as Read;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
pub trait ReadExt: futures_io::AsyncRead {
|
|
|
|
|
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
ReadFuture { reader: self, buf }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_vectored<'a>(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
bufs: &'a mut [IoSliceMut<'a>],
|
|
|
|
|
) -> ReadVectoredFuture<'a, Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
ReadVectoredFuture { reader: self, bufs }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEndFuture<'a, Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
let start_len = buf.len();
|
|
|
|
|
ReadToEndFuture {
|
|
|
|
|
reader: self,
|
|
|
|
|
buf,
|
|
|
|
|
start_len,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_to_string<'a>(&'a mut self, buf: &'a mut String) -> ReadToStringFuture<'a, Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
let start_len = buf.len();
|
|
|
|
|
ReadToStringFuture {
|
|
|
|
|
reader: self,
|
|
|
|
|
bytes: unsafe { mem::replace(buf.as_mut_vec(), Vec::new()) },
|
|
|
|
|
buf,
|
|
|
|
|
start_len,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Unpin,
|
|
|
|
|
{
|
|
|
|
|
ReadExactFuture { reader: self, buf }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: futures_io::AsyncRead + ?Sized> ReadExt for T {}
|
|
|
|
|