use crate::io::util::chain::{chain, Chain};
use crate::io::util::read::{read, Read};
use crate::io::util::read_buf::{read_buf, ReadBuf};
use crate::io::util::read_exact::{read_exact, ReadExact};
use crate::io::util::read_int::{
ReadI128, ReadI128Le, ReadI16, ReadI16Le, ReadI32, ReadI32Le, ReadI64, ReadI64Le, ReadI8,
};
use crate::io::util::read_int::{
ReadU128, ReadU128Le, ReadU16, ReadU16Le, ReadU32, ReadU32Le, ReadU64, ReadU64Le, ReadU8,
};
use crate::io::util::read_to_end::{read_to_end, ReadToEnd};
use crate::io::util::read_to_string::{read_to_string, ReadToString};
use crate::io::util::take::{take, Take};
use crate::io::AsyncRead;
use bytes::BufMut;
cfg_io_util! {
/// Defines numeric reader
macro_rules! read_impl {
(
$(
$(#[$outer:meta])*
fn $name:ident(&mut self) -> $($fut:ident)*;
)*
) => {
$(
$(#[$outer])*
fn $name<'a>(&'a mut self) -> $($fut)*<&'a mut Self> where Self: Unpin {
$($fut)*::new(self)
}
)*
}
}
/// Reads bytes from a source.
///
/// Implemented as an extention trait, adding utility methods to all
/// [`AsyncRead`] types. Callers will tend to import this trait instead of
/// [`AsyncRead`].
///
/// As a convenience, this trait may be imported using the [`prelude`]:
///
/// ```no_run
/// use tokio::fs::File;
/// use tokio::prelude::*;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut f = File::open("foo.txt").await?;
/// let mut buffer = [0; 10];
///
/// // The `read` method is defined by this trait.
/// let n = f.read(&mut buffer[..]).await?;
///
/// Ok(())
/// }
/// ```
///
/// See [module][crate::io] documentation for more details.
///
/// [`AsyncRead`]: AsyncRead
/// [`prelude`]: crate::prelude
pub trait AsyncReadExt: AsyncRead {
/// Creates a new `AsyncRead` instance that chains this stream with
/// `next`.
///
/// The returned `AsyncRead` instance will first read all bytes from this object
/// until EOF is encountered. Afterwards the output is equivalent to the
/// output of `next`.
///
/// # Examples
///
/// [`File`][crate::fs::File]s implement `AsyncRead`:
///
/// ```no_run
/// use tokio::fs::File;
/// use tokio::io::{self, AsyncReadExt};
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let f1 = File::open("foo.txt").await?;
/// let f2 = File::open("bar.txt").await?;
///
/// let mut handle = f1.chain(f2);
/// let mut buffer = String::new();
///
/// // read the value into a String. We could use any AsyncRead
/// // method here, this is just one example.
/// handle.read_to_string(&mut buffer).await?;
/// Ok(())
/// }
/// ```
fn chain<R>(self, next: R) -> Chain<Self, R>
where
Self: Sized,
R: AsyncRead,
{
chain(self, next)
}
/// Pulls some bytes from this source into the specified buffer,
/// returning how many bytes were read.
///
/// Equivalent to:
///
/// ```ignore
/// async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>;
/// ```
///
/// This function does not provide any guarantees about whether it
/// completes immediately or asynchronously
///
/// If the return value of this method is `Ok(n)`, then it must be
/// guaranteed that `0 <= n <= buf.len()`. A nonzero `n` value indicates
/// that the buffer `buf` has been filled in with `n` bytes of data from
/// this source. If `n` is `0`, then it can indicate one of two
/// scenarios:
///
/// 1. This reader has reached its "end of file" and will likely no longer
/// be able to produce bytes. Note that this does not mean that the
/// reader will *always* no longer be able to produce bytes.
/// 2. The buffer specified was 0 bytes in length.
///
/// No guarantees are provided about the contents of `buf` when this
/// function is called, implementations cannot rely on any property of the
/// contents of `buf` being `true`. It is recommended that *implementations*
/// only write data to `buf` instead of reading its contents.
///
/// Correspondingly, however, *callers* of this method may not assume
/// any guarantees about how the implementation uses `buf`. It is
/// possible that the code that's supposed to write to the buffer might
/// also read from it. It is your responsibility to make sure that `buf`
/// is initialized before calling `read`.
///
/// # Errors
///
/// If this function encounters any form of I/O or other error, an error
/// variant will be returned. If an error is returned then it must be
/// guaranteed that no bytes were read.
///
/// # Examples
///
/// [`File`][crate::fs::File]s implement `Read`:
///
/// ```no_run
/// use tokio::fs::File;
/// use tokio::io::{self, AsyncReadExt};
///
/// #[tokio::main]<