Skip to content

Commit

Permalink
Dry up output stream handling
Browse files Browse the repository at this point in the history
  • Loading branch information
soenkehahn committed Aug 10, 2021
1 parent 9e06ef7 commit 4015e1c
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions src/collected_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,60 @@ pub(crate) struct Waiter {
}

impl Waiter {
pub(crate) fn spawn_standard_stream_relaying<Stdout, Stderr>(
context: &Context<Stdout, Stderr>,
config: &Config,
mut child_stdin: ChildStdin,
mut child_stdout: ChildStdout,
mut child_stderr: ChildStderr,
) -> Self
where
Stdout: Write + Send + Clone + 'static,
Stderr: Write + Send + Clone + 'static,
{
let config_stdin = config.stdin.clone();
let stdin_join_handle = thread::spawn(move || -> io::Result<()> {
child_stdin.write_all(&config_stdin)?;
Ok(())
});
let mut context_clone = context.clone();
let capture_stdout = config.capture_stdout;
let stdout_join_handle = thread::spawn(move || -> io::Result<Option<Vec<u8>>> {
let mut collected_stdout = if capture_stdout {
fn spawn_standard_stream_handler(
capture_stream: bool,
mut source: impl Read + Send + 'static,
mut relay_sink: impl Write + Send + 'static,
) -> JoinHandle<io::Result<Option<Vec<u8>>>> {
thread::spawn(move || -> io::Result<Option<Vec<u8>>> {
let mut collected = if capture_stream {
Some(Vec::new())
} else {
None
};
let buffer = &mut [0; 256];
loop {
let length = child_stdout.read(buffer)?;
let length = source.read(buffer)?;
if (length) == 0 {
break;
}
if let Some(collected_stdout) = &mut collected_stdout {
if let Some(collected_stdout) = &mut collected {
collected_stdout.extend(&buffer[..length]);
}
if !capture_stdout {
context_clone.stdout.write_all(&buffer[..length])?;
if !capture_stream {
relay_sink.write_all(&buffer[..length])?;
}
}
Ok(collected_stdout)
});
let mut context_clone = context.clone();
let capture_stderr = config.capture_stderr;
let stderr_join_handle = thread::spawn(move || -> io::Result<Option<Vec<u8>>> {
let mut collected_stderr = if capture_stderr {
Some(Vec::new())
} else {
None
};
let buffer = &mut [0; 256];
loop {
let length = child_stderr.read(buffer)?;
if (length) == 0 {
break;
}
if let Some(collected_stderr) = &mut collected_stderr {
collected_stderr.extend(&buffer[..length]);
}
if !capture_stderr {
context_clone.stderr.write_all(&buffer[..length])?;
}
}
Ok(collected_stderr)
Ok(collected)
})
}

pub(crate) fn spawn_standard_stream_relaying<Stdout, Stderr>(
context: &Context<Stdout, Stderr>,
config: &Config,
mut child_stdin: ChildStdin,
child_stdout: ChildStdout,
child_stderr: ChildStderr,
) -> Self
where
Stdout: Write + Send + Clone + 'static,
Stderr: Write + Send + Clone + 'static,
{
let config_stdin = config.stdin.clone();
let stdin_join_handle = thread::spawn(move || -> io::Result<()> {
child_stdin.write_all(&config_stdin)?;
Ok(())
});
let stdout_join_handle = Self::spawn_standard_stream_handler(
config.capture_stdout,
child_stdout,
context.stdout.clone(),
);
let stderr_join_handle = Self::spawn_standard_stream_handler(
config.capture_stderr,
child_stderr,
context.stderr.clone(),
);
Waiter {
stdin: stdin_join_handle,
stdout: stdout_join_handle,
Expand Down

0 comments on commit 4015e1c

Please sign in to comment.