Search code examples
rustrust-tokio

Read Childstdout without blocking


I'm trying to reproduce Shepmasters answer to this question, but getting the following compilation error.

error[E0599]: the method `for_each` exists for struct `tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>`, but its trait bounds were not satisfied
  --> src/main.rs:19:10
   |
19 |           .for_each(|s| async move { println!("> {:?}", s) })
   |            ^^^^^^^^ method cannot be called on `tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>` due to unsatisfied trait bounds
   | 
  ::: /home/.../tokio-1.7.1/src/io/util/lines.rs:10:1
   |
10 | / pin_project! {
11 | |     /// Read lines from an [`AsyncBufRead`].
12 | |     ///
13 | |     /// A `Lines` can be turned into a `Stream` with [`LinesStream`].
...  |
29 | |     }
30 | | }
   | |_- doesn't satisfy `_: Iterator`
   |
   = note: the following trait bounds were not satisfied:
           `tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>: Iterator`
           which is required by `&mut tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>: Iterator`

This is my code (rust 1.52.1 on Raspberry Pi 4)

[dependencies]
futures = "0.3.15"
tokio = { version = "1", features = ["full"] }
use futures::StreamExt; // <-- claimed to be unused
use std::process::Stdio; 
// use tokio::prelude::*;  <-- doesn't exit
use tokio::{io::AsyncBufReadExt, io::BufReader, process::Command};

#[tokio::main]
async fn main() {
    let mut child = Command::new("sudo")
        .arg("ls")
        .stdout(Stdio::piped())
        .spawn()
        .expect("Command failed");

    let mut stdout = child.stdout.take().unwrap();

    BufReader::new(stdout)
        .lines()
        .for_each(|s| async move { println!("> {:?}", s) })
        .await;
}

More generally, how I can get better at learning which traits I need to import? In other languages I'd just look at the methods on a given class, but in rust I struggle to discover the necessary traits. E.g. to map Futures it took me days to find futures::FutureExt.


Solution

  • I like this example on the Tokio documentation.

    Below is that code with a slight modification to print all stderr lines:

    use tokio::io::{BufReader, AsyncBufReadExt};
    use tokio::process::Command;
    use std::process::{Stdio};
    
        async fn run_command(shell_cmd_str: &str) -> Result<()> {
            let mut cmd = Command::new("sh");
            cmd.args(&["-c", shell_cmd_str]);
            cmd.stderr(Stdio::piped());
        
            let mut child = cmd.spawn()
                .expect("failed to spawn command");
        
            let stderr = child.stderr.take()
                .expect("child did not have a handle to stdout");
        
            let mut stderr_reader = BufReader::new(stderr).lines();
        
            tokio::spawn(async {
                let status = child.await
                    .expect("child process encountered an error");
                println!("child status was: {}", status);
            });
        
            while let Some(line) = stderr_reader.next_line().await? {
                println!("Stderr line: {}", line);
            }
        
        
            Ok(())
        }