mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-09 00:00:08 +02:00
net: add try_*, readable, writable, ready, and peer_addr methods to split halves (#4120)
This commit is contained in:
+211
-2
@@ -9,14 +9,18 @@
|
|||||||
//! level.
|
//! level.
|
||||||
|
|
||||||
use crate::future::poll_fn;
|
use crate::future::poll_fn;
|
||||||
use crate::io::{AsyncRead, AsyncWrite, ReadBuf};
|
use crate::io::{AsyncRead, AsyncWrite, Interest, ReadBuf, Ready};
|
||||||
use crate::net::TcpStream;
|
use crate::net::TcpStream;
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::net::Shutdown;
|
use std::net::{Shutdown, SocketAddr};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
cfg_io_util! {
|
||||||
|
use bytes::BufMut;
|
||||||
|
}
|
||||||
|
|
||||||
/// Borrowed read half of a [`TcpStream`], created by [`split`].
|
/// Borrowed read half of a [`TcpStream`], created by [`split`].
|
||||||
///
|
///
|
||||||
/// Reading from a `ReadHalf` is usually done using the convenience methods found on the
|
/// Reading from a `ReadHalf` is usually done using the convenience methods found on the
|
||||||
@@ -134,6 +138,211 @@ impl ReadHalf<'_> {
|
|||||||
let mut buf = ReadBuf::new(buf);
|
let mut buf = ReadBuf::new(buf);
|
||||||
poll_fn(|cx| self.poll_peek(cx, &mut buf)).await
|
poll_fn(|cx| self.poll_peek(cx, &mut buf)).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wait 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`.
|
||||||
|
pub async fn ready(&self, interest: Interest) -> io::Result<Ready> {
|
||||||
|
self.0.ready(interest).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait 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`.
|
||||||
|
pub async fn readable(&self) -> io::Result<()> {
|
||||||
|
self.0.readable().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
self.0.try_read(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`try_read()`]: Self::try_read()
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read_vectored(&self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
|
||||||
|
self.0.try_read_vectored(bufs)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg_io_util! {
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read_buf<B: BufMut>(&self, buf: &mut B) -> io::Result<usize> {
|
||||||
|
self.0.try_read_buf(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the remote address that this stream is connected to.
|
||||||
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.0.peer_addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the local address that this stream is bound to.
|
||||||
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.0.local_addr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WriteHalf<'_> {
|
||||||
|
/// Wait 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`.
|
||||||
|
pub async fn ready(&self, interest: Interest) -> io::Result<Ready> {
|
||||||
|
self.0.ready(interest).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait for the socket to become writable.
|
||||||
|
///
|
||||||
|
/// This function is equivalent to `ready(Interest::WRITABLE)` and is usually
|
||||||
|
/// paired with `try_write()`.
|
||||||
|
///
|
||||||
|
/// # 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 write that fails with `WouldBlock` or
|
||||||
|
/// `Poll::Pending`.
|
||||||
|
pub async fn writable(&self) -> io::Result<()> {
|
||||||
|
self.0.writable().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to write a buffer to the stream, returning how many bytes were
|
||||||
|
/// written.
|
||||||
|
///
|
||||||
|
/// The function will attempt to write the entire contents of `buf`, but
|
||||||
|
/// only part of the buffer may be written.
|
||||||
|
///
|
||||||
|
/// This function is usually paired with `writable()`.
|
||||||
|
///
|
||||||
|
/// # Return
|
||||||
|
///
|
||||||
|
/// If data is successfully written, `Ok(n)` is returned, where `n` is the
|
||||||
|
/// number of bytes written. If the stream is not ready to write data,
|
||||||
|
/// `Err(io::ErrorKind::WouldBlock)` is returned.
|
||||||
|
pub fn try_write(&self, buf: &[u8]) -> io::Result<usize> {
|
||||||
|
self.0.try_write(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to write several buffers to the stream, returning how many bytes
|
||||||
|
/// were written.
|
||||||
|
///
|
||||||
|
/// Data is written from each buffer in order, with the final buffer read
|
||||||
|
/// from possible being only partially consumed. This method behaves
|
||||||
|
/// equivalently to a single call to [`try_write()`] with concatenated
|
||||||
|
/// buffers.
|
||||||
|
///
|
||||||
|
/// This function is usually paired with `writable()`.
|
||||||
|
///
|
||||||
|
/// [`try_write()`]: Self::try_write()
|
||||||
|
///
|
||||||
|
/// # Return
|
||||||
|
///
|
||||||
|
/// If data is successfully written, `Ok(n)` is returned, where `n` is the
|
||||||
|
/// number of bytes written. If the stream is not ready to write data,
|
||||||
|
/// `Err(io::ErrorKind::WouldBlock)` is returned.
|
||||||
|
pub fn try_write_vectored(&self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
|
||||||
|
self.0.try_write_vectored(bufs)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the remote address that this stream is connected to.
|
||||||
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.0.peer_addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the local address that this stream is bound to.
|
||||||
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.0.local_addr()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsyncRead for ReadHalf<'_> {
|
impl AsyncRead for ReadHalf<'_> {
|
||||||
|
|||||||
@@ -9,16 +9,20 @@
|
|||||||
//! level.
|
//! level.
|
||||||
|
|
||||||
use crate::future::poll_fn;
|
use crate::future::poll_fn;
|
||||||
use crate::io::{AsyncRead, AsyncWrite, ReadBuf};
|
use crate::io::{AsyncRead, AsyncWrite, Interest, ReadBuf, Ready};
|
||||||
use crate::net::TcpStream;
|
use crate::net::TcpStream;
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::net::Shutdown;
|
use std::net::{Shutdown, SocketAddr};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use std::{fmt, io};
|
use std::{fmt, io};
|
||||||
|
|
||||||
|
cfg_io_util! {
|
||||||
|
use bytes::BufMut;
|
||||||
|
}
|
||||||
|
|
||||||
/// Owned read half of a [`TcpStream`], created by [`into_split`].
|
/// Owned read half of a [`TcpStream`], created by [`into_split`].
|
||||||
///
|
///
|
||||||
/// Reading from an `OwnedReadHalf` is usually done using the convenience methods found
|
/// Reading from an `OwnedReadHalf` is usually done using the convenience methods found
|
||||||
@@ -189,6 +193,128 @@ impl OwnedReadHalf {
|
|||||||
let mut buf = ReadBuf::new(buf);
|
let mut buf = ReadBuf::new(buf);
|
||||||
poll_fn(|cx| self.poll_peek(cx, &mut buf)).await
|
poll_fn(|cx| self.poll_peek(cx, &mut buf)).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wait 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`.
|
||||||
|
pub async fn ready(&self, interest: Interest) -> io::Result<Ready> {
|
||||||
|
self.inner.ready(interest).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait 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`.
|
||||||
|
pub async fn readable(&self) -> io::Result<()> {
|
||||||
|
self.inner.readable().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
self.inner.try_read(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`try_read()`]: Self::try_read()
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read_vectored(&self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
|
||||||
|
self.inner.try_read_vectored(bufs)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg_io_util! {
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read_buf<B: BufMut>(&self, buf: &mut B) -> io::Result<usize> {
|
||||||
|
self.inner.try_read_buf(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the remote address that this stream is connected to.
|
||||||
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.inner.peer_addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the local address that this stream is bound to.
|
||||||
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.inner.local_addr()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsyncRead for OwnedReadHalf {
|
impl AsyncRead for OwnedReadHalf {
|
||||||
@@ -218,6 +344,87 @@ impl OwnedWriteHalf {
|
|||||||
self.shutdown_on_drop = false;
|
self.shutdown_on_drop = false;
|
||||||
drop(self);
|
drop(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wait 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`.
|
||||||
|
pub async fn ready(&self, interest: Interest) -> io::Result<Ready> {
|
||||||
|
self.inner.ready(interest).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait for the socket to become writable.
|
||||||
|
///
|
||||||
|
/// This function is equivalent to `ready(Interest::WRITABLE)` and is usually
|
||||||
|
/// paired with `try_write()`.
|
||||||
|
///
|
||||||
|
/// # 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 write that fails with `WouldBlock` or
|
||||||
|
/// `Poll::Pending`.
|
||||||
|
pub async fn writable(&self) -> io::Result<()> {
|
||||||
|
self.inner.writable().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to write a buffer to the stream, returning how many bytes were
|
||||||
|
/// written.
|
||||||
|
///
|
||||||
|
/// The function will attempt to write the entire contents of `buf`, but
|
||||||
|
/// only part of the buffer may be written.
|
||||||
|
///
|
||||||
|
/// This function is usually paired with `writable()`.
|
||||||
|
///
|
||||||
|
/// # Return
|
||||||
|
///
|
||||||
|
/// If data is successfully written, `Ok(n)` is returned, where `n` is the
|
||||||
|
/// number of bytes written. If the stream is not ready to write data,
|
||||||
|
/// `Err(io::ErrorKind::WouldBlock)` is returned.
|
||||||
|
pub fn try_write(&self, buf: &[u8]) -> io::Result<usize> {
|
||||||
|
self.inner.try_write(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to write several buffers to the stream, returning how many bytes
|
||||||
|
/// were written.
|
||||||
|
///
|
||||||
|
/// Data is written from each buffer in order, with the final buffer read
|
||||||
|
/// from possible being only partially consumed. This method behaves
|
||||||
|
/// equivalently to a single call to [`try_write()`] with concatenated
|
||||||
|
/// buffers.
|
||||||
|
///
|
||||||
|
/// This function is usually paired with `writable()`.
|
||||||
|
///
|
||||||
|
/// [`try_write()`]: Self::try_write()
|
||||||
|
///
|
||||||
|
/// # Return
|
||||||
|
///
|
||||||
|
/// If data is successfully written, `Ok(n)` is returned, where `n` is the
|
||||||
|
/// number of bytes written. If the stream is not ready to write data,
|
||||||
|
/// `Err(io::ErrorKind::WouldBlock)` is returned.
|
||||||
|
pub fn try_write_vectored(&self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
|
||||||
|
self.inner.try_write_vectored(bufs)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the remote address that this stream is connected to.
|
||||||
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.inner.peer_addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the local address that this stream is bound to.
|
||||||
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.inner.local_addr()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for OwnedWriteHalf {
|
impl Drop for OwnedWriteHalf {
|
||||||
|
|||||||
+206
-1
@@ -8,14 +8,19 @@
|
|||||||
//! split has no associated overhead and enforces all invariants at the type
|
//! split has no associated overhead and enforces all invariants at the type
|
||||||
//! level.
|
//! level.
|
||||||
|
|
||||||
use crate::io::{AsyncRead, AsyncWrite, ReadBuf};
|
use crate::io::{AsyncRead, AsyncWrite, Interest, ReadBuf, Ready};
|
||||||
use crate::net::UnixStream;
|
use crate::net::UnixStream;
|
||||||
|
|
||||||
|
use crate::net::unix::SocketAddr;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::net::Shutdown;
|
use std::net::Shutdown;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
cfg_io_util! {
|
||||||
|
use bytes::BufMut;
|
||||||
|
}
|
||||||
|
|
||||||
/// Borrowed read half of a [`UnixStream`], created by [`split`].
|
/// Borrowed read half of a [`UnixStream`], created by [`split`].
|
||||||
///
|
///
|
||||||
/// Reading from a `ReadHalf` is usually done using the convenience methods found on the
|
/// Reading from a `ReadHalf` is usually done using the convenience methods found on the
|
||||||
@@ -47,6 +52,206 @@ pub(crate) fn split(stream: &mut UnixStream) -> (ReadHalf<'_>, WriteHalf<'_>) {
|
|||||||
(ReadHalf(stream), WriteHalf(stream))
|
(ReadHalf(stream), WriteHalf(stream))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ReadHalf<'_> {
|
||||||
|
/// Wait 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.
|
||||||
|
///
|
||||||
|
/// # 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`.
|
||||||
|
pub async fn ready(&self, interest: Interest) -> io::Result<Ready> {
|
||||||
|
self.0.ready(interest).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait for the socket to become readable.
|
||||||
|
///
|
||||||
|
/// This function is equivalent to `ready(Interest::READABLE)` and is usually
|
||||||
|
/// paired with `try_read()`.
|
||||||
|
///
|
||||||
|
/// # 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`.
|
||||||
|
pub async fn readable(&self) -> io::Result<()> {
|
||||||
|
self.0.readable().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
self.0.try_read(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg_io_util! {
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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
|
||||||
|
pub fn try_read_buf<B: BufMut>(&self, buf: &mut B) -> io::Result<usize> {
|
||||||
|
self.0.try_read_buf(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`try_read()`]: Self::try_read()
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read_vectored(&self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
|
||||||
|
self.0.try_read_vectored(bufs)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the socket address of the remote half of this connection.
|
||||||
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.0.peer_addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the socket address of the local half of this connection.
|
||||||
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.0.local_addr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WriteHalf<'_> {
|
||||||
|
/// Wait 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.
|
||||||
|
///
|
||||||
|
/// # 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`.
|
||||||
|
pub async fn ready(&self, interest: Interest) -> io::Result<Ready> {
|
||||||
|
self.0.ready(interest).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait for the socket to become writable.
|
||||||
|
///
|
||||||
|
/// This function is equivalent to `ready(Interest::WRITABLE)` and is usually
|
||||||
|
/// paired with `try_write()`.
|
||||||
|
///
|
||||||
|
/// # 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 write that fails with `WouldBlock` or
|
||||||
|
/// `Poll::Pending`.
|
||||||
|
pub async fn writable(&self) -> io::Result<()> {
|
||||||
|
self.0.writable().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to write a buffer to the stream, returning how many bytes were
|
||||||
|
/// written.
|
||||||
|
///
|
||||||
|
/// The function will attempt to write the entire contents of `buf`, but
|
||||||
|
/// only part of the buffer may be written.
|
||||||
|
///
|
||||||
|
/// This function is usually paired with `writable()`.
|
||||||
|
///
|
||||||
|
/// # Return
|
||||||
|
///
|
||||||
|
/// If data is successfully written, `Ok(n)` is returned, where `n` is the
|
||||||
|
/// number of bytes written. If the stream is not ready to write data,
|
||||||
|
/// `Err(io::ErrorKind::WouldBlock)` is returned.
|
||||||
|
pub fn try_write(&self, buf: &[u8]) -> io::Result<usize> {
|
||||||
|
self.0.try_write(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to write several buffers to the stream, returning how many bytes
|
||||||
|
/// were written.
|
||||||
|
///
|
||||||
|
/// Data is written from each buffer in order, with the final buffer read
|
||||||
|
/// from possible being only partially consumed. This method behaves
|
||||||
|
/// equivalently to a single call to [`try_write()`] with concatenated
|
||||||
|
/// buffers.
|
||||||
|
///
|
||||||
|
/// This function is usually paired with `writable()`.
|
||||||
|
///
|
||||||
|
/// [`try_write()`]: Self::try_write()
|
||||||
|
///
|
||||||
|
/// # Return
|
||||||
|
///
|
||||||
|
/// If data is successfully written, `Ok(n)` is returned, where `n` is the
|
||||||
|
/// number of bytes written. If the stream is not ready to write data,
|
||||||
|
/// `Err(io::ErrorKind::WouldBlock)` is returned.
|
||||||
|
pub fn try_write_vectored(&self, buf: &[io::IoSlice<'_>]) -> io::Result<usize> {
|
||||||
|
self.0.try_write_vectored(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the socket address of the remote half of this connection.
|
||||||
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.0.peer_addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the socket address of the local half of this connection.
|
||||||
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.0.local_addr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AsyncRead for ReadHalf<'_> {
|
impl AsyncRead for ReadHalf<'_> {
|
||||||
fn poll_read(
|
fn poll_read(
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
|
|||||||
@@ -8,9 +8,10 @@
|
|||||||
//! split has no associated overhead and enforces all invariants at the type
|
//! split has no associated overhead and enforces all invariants at the type
|
||||||
//! level.
|
//! level.
|
||||||
|
|
||||||
use crate::io::{AsyncRead, AsyncWrite, ReadBuf};
|
use crate::io::{AsyncRead, AsyncWrite, Interest, ReadBuf, Ready};
|
||||||
use crate::net::UnixStream;
|
use crate::net::UnixStream;
|
||||||
|
|
||||||
|
use crate::net::unix::SocketAddr;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::net::Shutdown;
|
use std::net::Shutdown;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
@@ -18,6 +19,10 @@ use std::sync::Arc;
|
|||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use std::{fmt, io};
|
use std::{fmt, io};
|
||||||
|
|
||||||
|
cfg_io_util! {
|
||||||
|
use bytes::BufMut;
|
||||||
|
}
|
||||||
|
|
||||||
/// Owned read half of a [`UnixStream`], created by [`into_split`].
|
/// Owned read half of a [`UnixStream`], created by [`into_split`].
|
||||||
///
|
///
|
||||||
/// Reading from an `OwnedReadHalf` is usually done using the convenience methods found
|
/// Reading from an `OwnedReadHalf` is usually done using the convenience methods found
|
||||||
@@ -102,6 +107,124 @@ impl OwnedReadHalf {
|
|||||||
pub fn reunite(self, other: OwnedWriteHalf) -> Result<UnixStream, ReuniteError> {
|
pub fn reunite(self, other: OwnedWriteHalf) -> Result<UnixStream, ReuniteError> {
|
||||||
reunite(self, other)
|
reunite(self, other)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wait 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.
|
||||||
|
///
|
||||||
|
/// # 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`.
|
||||||
|
pub async fn ready(&self, interest: Interest) -> io::Result<Ready> {
|
||||||
|
self.inner.ready(interest).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait for the socket to become readable.
|
||||||
|
///
|
||||||
|
/// This function is equivalent to `ready(Interest::READABLE)` and is usually
|
||||||
|
/// paired with `try_read()`.
|
||||||
|
///
|
||||||
|
/// # 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`.
|
||||||
|
pub async fn readable(&self) -> io::Result<()> {
|
||||||
|
self.inner.readable().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
self.inner.try_read(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg_io_util! {
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read_buf<B: BufMut>(&self, buf: &mut B) -> io::Result<usize> {
|
||||||
|
self.inner.try_read_buf(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try 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.
|
||||||
|
///
|
||||||
|
/// [`try_read()`]: Self::try_read()
|
||||||
|
/// [`readable()`]: Self::readable()
|
||||||
|
/// [`ready()`]: Self::ready()
|
||||||
|
///
|
||||||
|
/// # 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.
|
||||||
|
pub fn try_read_vectored(&self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
|
||||||
|
self.inner.try_read_vectored(bufs)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the socket address of the remote half of this connection.
|
||||||
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.inner.peer_addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the socket address of the local half of this connection.
|
||||||
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.inner.local_addr()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsyncRead for OwnedReadHalf {
|
impl AsyncRead for OwnedReadHalf {
|
||||||
@@ -131,6 +254,85 @@ impl OwnedWriteHalf {
|
|||||||
self.shutdown_on_drop = false;
|
self.shutdown_on_drop = false;
|
||||||
drop(self);
|
drop(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wait 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.
|
||||||
|
///
|
||||||
|
/// # 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`.
|
||||||
|
pub async fn ready(&self, interest: Interest) -> io::Result<Ready> {
|
||||||
|
self.inner.ready(interest).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait for the socket to become writable.
|
||||||
|
///
|
||||||
|
/// This function is equivalent to `ready(Interest::WRITABLE)` and is usually
|
||||||
|
/// paired with `try_write()`.
|
||||||
|
///
|
||||||
|
/// # 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 write that fails with `WouldBlock` or
|
||||||
|
/// `Poll::Pending`.
|
||||||
|
pub async fn writable(&self) -> io::Result<()> {
|
||||||
|
self.inner.writable().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to write a buffer to the stream, returning how many bytes were
|
||||||
|
/// written.
|
||||||
|
///
|
||||||
|
/// The function will attempt to write the entire contents of `buf`, but
|
||||||
|
/// only part of the buffer may be written.
|
||||||
|
///
|
||||||
|
/// This function is usually paired with `writable()`.
|
||||||
|
///
|
||||||
|
/// # Return
|
||||||
|
///
|
||||||
|
/// If data is successfully written, `Ok(n)` is returned, where `n` is the
|
||||||
|
/// number of bytes written. If the stream is not ready to write data,
|
||||||
|
/// `Err(io::ErrorKind::WouldBlock)` is returned.
|
||||||
|
pub fn try_write(&self, buf: &[u8]) -> io::Result<usize> {
|
||||||
|
self.inner.try_write(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to write several buffers to the stream, returning how many bytes
|
||||||
|
/// were written.
|
||||||
|
///
|
||||||
|
/// Data is written from each buffer in order, with the final buffer read
|
||||||
|
/// from possible being only partially consumed. This method behaves
|
||||||
|
/// equivalently to a single call to [`try_write()`] with concatenated
|
||||||
|
/// buffers.
|
||||||
|
///
|
||||||
|
/// This function is usually paired with `writable()`.
|
||||||
|
///
|
||||||
|
/// [`try_write()`]: Self::try_write()
|
||||||
|
///
|
||||||
|
/// # Return
|
||||||
|
///
|
||||||
|
/// If data is successfully written, `Ok(n)` is returned, where `n` is the
|
||||||
|
/// number of bytes written. If the stream is not ready to write data,
|
||||||
|
/// `Err(io::ErrorKind::WouldBlock)` is returned.
|
||||||
|
pub fn try_write_vectored(&self, buf: &[io::IoSlice<'_>]) -> io::Result<usize> {
|
||||||
|
self.inner.try_write_vectored(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the socket address of the remote half of this connection.
|
||||||
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.inner.peer_addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the socket address of the local half of this connection.
|
||||||
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||||
|
self.inner.local_addr()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for OwnedWriteHalf {
|
impl Drop for OwnedWriteHalf {
|
||||||
|
|||||||
@@ -773,11 +773,41 @@ impl UnixStream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the socket address of the local half of this connection.
|
/// Returns the socket address of the local half of this connection.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// use tokio::net::UnixStream;
|
||||||
|
///
|
||||||
|
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// let dir = tempfile::tempdir().unwrap();
|
||||||
|
/// let bind_path = dir.path().join("bind_path");
|
||||||
|
/// let stream = UnixStream::connect(bind_path).await?;
|
||||||
|
///
|
||||||
|
/// println!("{:?}", stream.local_addr()?);
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||||
self.io.local_addr().map(SocketAddr)
|
self.io.local_addr().map(SocketAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the socket address of the remote half of this connection.
|
/// Returns the socket address of the remote half of this connection.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// use tokio::net::UnixStream;
|
||||||
|
///
|
||||||
|
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
/// let dir = tempfile::tempdir().unwrap();
|
||||||
|
/// let bind_path = dir.path().join("bind_path");
|
||||||
|
/// let stream = UnixStream::connect(bind_path).await?;
|
||||||
|
///
|
||||||
|
/// println!("{:?}", stream.peer_addr()?);
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||||
self.io.peer_addr().map(SocketAddr)
|
self.io.peer_addr().map(SocketAddr)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user