Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: implement AsyncWrite for Empty #6235

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions tokio/src/io/util/empty.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::io::{AsyncBufRead, AsyncRead, ReadBuf};
use crate::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};

use std::fmt;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};

cfg_io_util! {
/// An async reader which is always at EOF.
/// `Empty` ignores any data written via [`AsyncWrite`], and will always be empty
/// (returning zero bytes) when read via [`AsyncRead`].
///
/// This struct is generally created by calling [`empty`]. Please see
/// the documentation of [`empty()`][`empty`] for more details.
Expand All @@ -19,9 +20,12 @@ cfg_io_util! {
_p: (),
}

/// Creates a new empty async reader.
/// Creates a value that is always at EOF for reads, and ignores all data written.
///
/// All reads from the returned reader will return `Poll::Ready(Ok(0))`.
/// All writes on the returned instance will return `Poll::Ready(Ok(buf.len()))`
/// and the contents of the buffer will not be inspected.
///
/// All reads from the returned instance will return `Poll::Ready(Ok(0))`.
///
/// This is an asynchronous version of [`std::io::empty`][std].
///
Expand All @@ -41,6 +45,19 @@ cfg_io_util! {
/// assert!(buffer.is_empty());
/// }
/// ```
///
/// A convoluted way of getting the length of a buffer:
///
/// ```
/// use tokio::io::{self, AsyncWriteExt};
///
/// #[tokio::main]
/// async fn main() {
/// let buffer = vec![1, 2, 3, 5, 8];
/// let num_bytes = io::empty().write(&buffer).await.unwrap();
/// assert_eq!(num_bytes, 5);
/// }
/// ```
pub fn empty() -> Empty {
Empty { _p: () }
}
Expand Down Expand Up @@ -71,6 +88,27 @@ impl AsyncBufRead for Empty {
fn consume(self: Pin<&mut Self>, _: usize) {}
}

impl AsyncWrite for Empty {
#[inline]
mcastorina marked this conversation as resolved.
Show resolved Hide resolved
fn poll_write(
self: Pin<&mut Self>,
_: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(buf.len()))
}

#[inline]
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Poll::Ready(Ok(()))
}

#[inline]
fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Poll::Ready(Ok(()))
}
}

impl fmt::Debug for Empty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad("Empty { .. }")
Expand Down
Loading