net: expose TcpStream::poll_peek (#1864)

This used to be exposed in 0.1, but was switched to private during the
upgrade. The `async fn` is sufficient for many, but not all cases.

Closes #1556
This commit is contained in:
Carl Lerche 2019-11-30 09:36:03 -08:00 committed by GitHub
parent 417460cf86
commit b559a0cd9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -186,6 +186,7 @@ impl TcpStream {
}
/// Returns the remote address that this stream is connected to.
///
/// # Examples
///
/// ```no_run
@ -202,7 +203,43 @@ impl TcpStream {
self.io.get_ref().peer_addr()
}
fn poll_peek(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::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.
///
/// # Return value
///
/// The function returns:
///
/// * `Poll::Pending` if data is not yet available.
/// * `Poll::Ready(Ok(n))` if data is available. `n` is the number of bytes peeked.
/// * `Poll::Ready(Err(e))` if an error is encountered.
///
/// # Errors
///
/// This function may encounter any standard I/O error except `WouldBlock`.
///
/// # Examples
///
/// ```no_run
/// use tokio::io;
/// use tokio::net::TcpStream;
///
/// use futures::future::poll_fn;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut stream = TcpStream::connect("127.0.0.1:8000").await?;
/// let mut buf = [0; 10];
///
/// poll_fn(|cx| {
/// stream.poll_peek(cx, &mut buf)
/// }).await?;
///
/// Ok(())
/// }
/// ```
pub fn poll_peek(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
ready!(self.io.poll_read_ready(cx, mio::Ready::readable()))?;
match self.io.get_ref().peek(buf) {