Function tokio::io::empty

source ·
pub fn empty() -> Empty
Expand description

Creates a value that is always at EOF for reads, and ignores all data written.

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.

§Examples

A slightly sad example of not reading anything into a buffer:

use tokio::io::{self, AsyncReadExt};

#[tokio::main]
async fn main() {
    let mut buffer = String::new();
    io::empty().read_to_string(&mut buffer).await.unwrap();
    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);
}