1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use blocking::Unblock;
use futures_lite::{AsyncBufReadExt, AsyncRead, Stream, StreamExt};
use serde::Deserialize;
use std::io;
pub fn async_stdin() -> Unblock<io::Stdin> {
Unblock::new(io::stdin())
}
pub fn async_stdout() -> Unblock<io::Stdout> {
Unblock::new(io::stdout())
}
pub fn json_input_stream<I, S>(input: I) -> impl Stream<Item = serde_json::Result<S>> + Unpin + Send
where
I: AsyncRead + Unpin + Send,
S: for<'a> Deserialize<'a>,
{
futures_lite::io::BufReader::new(input)
.lines()
.take_while(Result::is_ok)
.map(Result::unwrap)
.map(|line| serde_json::from_str::<S>(&line))
}