async_flow/tokio/
stdin.rs

1// This is free and unencumbered software released into the public domain.
2
3use super::Outputs;
4use crate::io::{Error, Result};
5use core::str::FromStr;
6
7pub async fn stdin<T: FromStr>(outputs: Outputs<T>) -> Result {
8    use std::io::ErrorKind;
9    use tokio::io::{AsyncBufReadExt, BufReader};
10
11    let input = tokio::io::stdin();
12    let reader = BufReader::new(input);
13    let mut lines = reader.lines();
14
15    while let Some(line) = lines.next_line().await? {
16        let output = line
17            .parse()
18            .map_err(|_| Error::Stdio(ErrorKind::InvalidInput.into()))?;
19        outputs.send(output).await?;
20    }
21
22    Ok(())
23}