pub struct Collector<S: Sink> { /* private fields */ }Expand description
A collector for stream data, inspecting it chunk by chunk but also providing mutable access to a sink in which the data can be stored.
See the collect_* functions on BroadcastOutputStream and SingleOutputStream.
For proper cleanup, call
wait(), which waits for the collection task to complete.cancel(), which sends a termination signal and then waits for the collection task to complete.
If not cleaned up, the termination signal will be sent when dropping this collector, but the task will be aborted (forceful, not waiting for its regular completion).
Implementations§
Source§impl<S: Sink> Collector<S>
impl<S: Sink> Collector<S>
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Checks if this task has finished.
Sourcepub async fn wait(self) -> Result<S, CollectorError>
pub async fn wait(self) -> Result<S, CollectorError>
Wait for the collector to terminate naturally.
A collector will automatically terminate when either:
- The underlying write-side of the stream is dropped.
- The underlying stream is closed (by receiving an EOF / final read of 0 bytes).
- The first
Next::Breakis observed.
If none of these may occur in your case, this could/will hang forever!
The stdout/stderr streams naturally close when the process is terminated, so waiting
on a collector after termination is fine:
let mut process = Process::new(cmd).spawn_broadcast().unwrap();
let collector = process.stdout().collect_lines_into_vec(LineParsingOptions::default());
process.terminate(Duration::from_secs(1), Duration::from_secs(1)).await.unwrap();
let collected = collector.wait().await.unwrap(); // This will return immediately.Sourcepub async fn cancel(self) -> Result<S, CollectorError>
pub async fn cancel(self) -> Result<S, CollectorError>
Sends a cancellation event to the collector, letting it shut down.