pub struct OwnedReadHalf { /* private fields */ }
Expand description
Owned read half of a TcpStream
, created by into_split
.
Reading from an OwnedReadHalf
is usually done using the convenience methods found
on the AsyncReadExt
trait.
Implementations
sourceimpl OwnedReadHalf
impl OwnedReadHalf
sourcepub fn reunite(self, other: OwnedWriteHalf) -> Result<TcpStream, ReuniteError>
pub fn reunite(self, other: OwnedWriteHalf) -> Result<TcpStream, ReuniteError>
Attempts to put the two halves of a TcpStream
back together and
recover the original socket. Succeeds only if the two halves
originated from the same call to into_split
.
sourcepub fn poll_peek(
&mut self,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>
) -> Poll<Result<usize>>
pub fn poll_peek(
&mut self,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>
) -> Poll<Result<usize>>
Attempt to receive data on the socket, without removing that data from the queue, registering the current task for wakeup if data is not yet available.
Note that on multiple calls to poll_peek
or poll_read
, only the
Waker
from the Context
passed to the most recent call is scheduled
to receive a wakeup.
See the TcpStream::poll_peek
level documentation for more details.
Examples
use tokio::io::{self, ReadBuf};
use tokio::net::TcpStream;
use futures::future::poll_fn;
#[tokio::main]
async fn main() -> io::Result<()> {
let stream = TcpStream::connect("127.0.0.1:8000").await?;
let (mut read_half, _) = stream.into_split();
let mut buf = [0; 10];
let mut buf = ReadBuf::new(&mut buf);
poll_fn(|cx| {
read_half.poll_peek(cx, &mut buf)
}).await?;
Ok(())
}
sourcepub async fn peek(&mut self, buf: &mut [u8]) -> Result<usize>
pub async fn peek(&mut self, buf: &mut [u8]) -> Result<usize>
Receives data on the socket from the remote address to which it is connected, without removing that data from the queue. On success, returns the number of bytes peeked.
See the TcpStream::peek
level documentation for more details.
Examples
use tokio::net::TcpStream;
use tokio::io::AsyncReadExt;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Connect to a peer
let stream = TcpStream::connect("127.0.0.1:8080").await?;
let (mut read_half, _) = stream.into_split();
let mut b1 = [0; 10];
let mut b2 = [0; 10];
// Peek at the data
let n = read_half.peek(&mut b1).await?;
// Read the data
assert_eq!(n, read_half.read(&mut b2[..n]).await?);
assert_eq!(&b1[..n], &b2[..n]);
Ok(())
}
The read
method is defined on the AsyncReadExt
trait.
sourcepub async fn ready(&self, interest: Interest) -> Result<Ready>
pub async fn ready(&self, interest: Interest) -> Result<Ready>
Waits for any of the requested ready states.
This function is usually paired with try_read()
or try_write()
. It
can be used to concurrently read / write to the same socket on a single
task without splitting the socket.
This function is equivalent to TcpStream::ready
.
Cancel safety
This method is cancel safe. Once a readiness event occurs, the method
will continue to return immediately until the readiness event is
consumed by an attempt to read or write that fails with WouldBlock
or
Poll::Pending
.
sourcepub async fn readable(&self) -> Result<()>
pub async fn readable(&self) -> Result<()>
Waits for the socket to become readable.
This function is equivalent to ready(Interest::READABLE)
and is usually
paired with try_read()
.
This function is also equivalent to TcpStream::ready
.
Cancel safety
This method is cancel safe. Once a readiness event occurs, the method
will continue to return immediately until the readiness event is
consumed by an attempt to read that fails with WouldBlock
or
Poll::Pending
.
sourcepub fn try_read(&self, buf: &mut [u8]) -> Result<usize>
pub fn try_read(&self, buf: &mut [u8]) -> Result<usize>
Tries to read data from the stream into the provided buffer, returning how many bytes were read.
Receives any pending data from the socket but does not wait for new data
to arrive. On success, returns the number of bytes read. Because
try_read()
is non-blocking, the buffer does not have to be stored by
the async task and can exist entirely on the stack.
Usually, readable()
or ready()
is used with this function.
Return
If data is successfully read, Ok(n)
is returned, where n
is the
number of bytes read. Ok(0)
indicates the stream’s read half is closed
and will no longer yield data. If the stream is not ready to read data
Err(io::ErrorKind::WouldBlock)
is returned.
sourcepub fn try_read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
pub fn try_read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
Tries to read data from the stream into the provided buffers, returning how many bytes were read.
Data is copied to fill each buffer in order, with the final buffer
written to possibly being only partially filled. This method behaves
equivalently to a single call to try_read()
with concatenated
buffers.
Receives any pending data from the socket but does not wait for new data
to arrive. On success, returns the number of bytes read. Because
try_read_vectored()
is non-blocking, the buffer does not have to be
stored by the async task and can exist entirely on the stack.
Usually, readable()
or ready()
is used with this function.
Return
If data is successfully read, Ok(n)
is returned, where n
is the
number of bytes read. Ok(0)
indicates the stream’s read half is closed
and will no longer yield data. If the stream is not ready to read data
Err(io::ErrorKind::WouldBlock)
is returned.
sourcepub fn try_read_buf<B: BufMut>(&self, buf: &mut B) -> Result<usize>
pub fn try_read_buf<B: BufMut>(&self, buf: &mut B) -> Result<usize>
Tries to read data from the stream into the provided buffer, advancing the buffer’s internal cursor, returning how many bytes were read.
Receives any pending data from the socket but does not wait for new data
to arrive. On success, returns the number of bytes read. Because
try_read_buf()
is non-blocking, the buffer does not have to be stored by
the async task and can exist entirely on the stack.
Usually, readable()
or ready()
is used with this function.
Return
If data is successfully read, Ok(n)
is returned, where n
is the
number of bytes read. Ok(0)
indicates the stream’s read half is closed
and will no longer yield data. If the stream is not ready to read data
Err(io::ErrorKind::WouldBlock)
is returned.
sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
Returns the remote address that this stream is connected to.
sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local address that this stream is bound to.
Trait Implementations
sourceimpl AsRef<TcpStream> for OwnedReadHalf
impl AsRef<TcpStream> for OwnedReadHalf
sourceimpl AsyncRead for OwnedReadHalf
impl AsyncRead for OwnedReadHalf
Auto Trait Implementations
impl RefUnwindSafe for OwnedReadHalf
impl Send for OwnedReadHalf
impl Sync for OwnedReadHalf
impl Unpin for OwnedReadHalf
impl UnwindSafe for OwnedReadHalf
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more