use std::io::{Read, Write};
use async_channel::Sender;
use tracing::warn;
use crate::stdin_watch::FeedCounter;
pub fn read_chunks<R>(mut reader: R, mut emit: impl FnMut(Option<Vec<u8>>) + Send + 'static)
where
R: Read + Send + 'static,
{
std::thread::spawn(move || {
let mut buffer = vec![0_u8; 8192];
loop {
match reader.read(&mut buffer) {
Ok(0) => break,
Ok(count) => emit(Some(buffer[..count].to_vec())),
Err(error) => {
warn!(%error, "a process pipe failed to read");
break;
}
}
}
emit(None);
});
}
pub fn write_chunks<W>(mut writer: W, feed: FeedCounter) -> Sender<Vec<u8>>
where
W: Write + Send + 'static,
{
let (tx, rx) = async_channel::unbounded::<Vec<u8>>();
std::thread::spawn(move || {
while let Ok(bytes) = rx.recv_blocking() {
if bytes.is_empty() {
continue;
}
feed.record();
if let Err(error) = writer.write_all(&bytes).and_then(|()| writer.flush()) {
warn!(%error, "a process pipe failed to write");
break;
}
}
});
tx
}