[−][src]Trait async_std::io::Read
Allows reading from a byte stream.
This trait is a re-export of futures::io::AsyncRead
and is an async version of
std::io::Read
.
Methods other than poll_read
and poll_read_vectored
do not really exist in the
trait itself, but they become available when the prelude is imported:
use async_std::prelude::*;
Required methods
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
Attempt to read from the AsyncRead
into buf
.
Provided methods
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
Attempt to read from the AsyncRead
into bufs
using vectored IO operations.
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
Self: Unpin,
Reads some bytes from the byte stream.
Returns the number of bytes read from the start of the buffer.
If the return value is Ok(n)
, then it must be guaranteed that
0 <= n <= buf.len()
. A nonzero n
value indicates that the buffer has been
filled in with n
bytes of data. If n
is 0
, then it can indicate one of two
scenarios:
- 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.
- The buffer specified was 0 bytes in length.
Examples
use async_std::fs::File; use async_std::prelude::*; let mut file = File::open("a.txt").await?; let mut buf = vec![0; 1024]; let n = file.read(&mut buf).await?;
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
Like read
, except that it reads into a slice of buffers.
Data is copied to fill each buffer in order, with the final buffer written to
possibly being only partially filled. This method must behave as a single call to
read
with the buffers concatenated would.
The default implementation calls read
with either the first nonempty buffer
provided, or an empty one if none exists.
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>
) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
&'a mut self,
buf: &'a mut Vec<u8>
) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
Reads all bytes from the byte stream.
All bytes read from this stream will be appended to the specified buffer buf
.
This function will continuously call read
to append more data to buf
until
read
returns either Ok(0)
or an error.
If successful, this function will return the total number of bytes read.
Examples
use async_std::fs::File; use async_std::prelude::*; let mut file = File::open("a.txt").await?; let mut buf = Vec::new(); file.read_to_end(&mut buf).await?;
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
&'a mut self,
buf: &'a mut String
) -> ImplFuture<'a, Result<usize>> where
Self: Unpin,
Reads all bytes from the byte stream and appends them into a string.
If successful, this function will return the number of bytes read.
If the data in this stream is not valid UTF-8 then an error will be returned and
buf
will be left unmodified.
Examples
use async_std::fs::File; use async_std::prelude::*; let mut file = File::open("a.txt").await?; let mut buf = String::new(); file.read_to_string(&mut buf).await?;
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<'a, Result<()>> where
Self: Unpin,
Self: Unpin,
Reads the exact number of bytes required to fill buf
.
This function reads as many bytes as necessary to completely fill the specified
buffer buf
.
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.
If this function encounters an "end of file" before completely filling the buffer,
it returns an error of the kind ErrorKind::UnexpectedEof
. The contents of
buf
are unspecified in this case.
If any other read error is encountered then this function immediately returns. The
contents of buf
are unspecified in this case.
If this function returns an error, it is unspecified how many bytes it has read, but it will never read more than would be necessary to completely fill the buffer.
Examples
use async_std::fs::File; use async_std::prelude::*; let mut file = File::open("a.txt").await?; let mut buf = vec![0; 10]; file.read_exact(&mut buf).await?;
Implementations on Foreign Types
impl<T: Read + Unpin + ?Sized> Read for Box<T>
[src]
impl<'_, T: Read + Unpin + ?Sized> Read for &'_ mut T
[src]
impl<'_> Read for &'_ [u8]
[src]
Loading content...
Implementors
impl Read for File
[src]
impl Read for Empty
[src]
impl Read for Repeat
[src]
impl Read for Stdin
[src]
impl Read for TcpStream
[src]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
impl Read for UnixStream
[src]
impl<'_> Read for &'_ File
[src]
impl<'_> Read for &'_ TcpStream
[src]
impl<'_> Read for &'_ UnixStream
[src]
impl<P> Read for Pin<P> where
P: DerefMut + Unpin,
<P as Deref>::Target: Read,
[src]
P: DerefMut + Unpin,
<P as Deref>::Target: Read,
impl<R: Read> Read for BufReader<R>
[src]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
impl<T> Read for Cursor<T> where
T: AsRef<[u8]> + Unpin,
[src]
T: AsRef<[u8]> + Unpin,