[−][src]Function smol::reader
pub fn reader(
reader: impl Read + Send + 'static
) -> impl AsyncRead + Send + Unpin + 'static
Creates an async reader that runs on a thread.
This adapter converts any kind of synchronous reader into an asynchronous reader by running it on the blocking executor and sending bytes back over a pipe.
Examples
Read from a file:
use futures::prelude::*; use smol::{blocking, reader}; use std::fs::File; // Open a file for reading. let file = blocking!(File::open("foo.txt"))?; let mut file = reader(file); // Read the whole file. let mut contents = Vec::new(); file.read_to_end(&mut contents).await?;
Read output from a process:
use futures::prelude::*; use smol::reader; use std::process::{Command, Stdio}; // Spawn a child process and make an async reader for its stdout. let child = Command::new("dir").stdout(Stdio::piped()).spawn()?; let mut child_stdout = reader(child.stdout.unwrap()); // Read the entire output. let mut output = String::new(); child_stdout.read_to_string(&mut output).await?;