Skip to main content

Module stream

Module stream 

Source
Expand description

Streaming and async iteration support

This module provides async streaming capabilities similar to JavaScript’s async iterators and stream handling in $.stream-utils.mjs.

§Usage

use command_stream::{StreamingRunner, OutputChunk};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let runner = StreamingRunner::new("yes hello");

    // Stream output as it arrives
    let mut stream = runner.stream();
    let mut count = 0;
    while let Some(chunk) = stream.next().await {
        match chunk {
            OutputChunk::Stdout(data) => {
                print!("{}", String::from_utf8_lossy(&data));
                count += 1;
                if count >= 5 {
                    break;
                }
            }
            OutputChunk::Stderr(data) => {
                eprint!("{}", String::from_utf8_lossy(&data));
            }
            OutputChunk::Exit(code) => {
                println!("Process exited with code: {}", code);
                break;
            }
        }
    }

    Ok(())
}

Structs§

OutputStream
Stream of output chunks from a process
StreamingRunner
A streaming process runner that allows async iteration over output

Enums§

OutputChunk
A chunk of output from a streaming process

Traits§

AsyncIterator
Async iterator trait for output streams
IntoStream
Extension trait to convert ProcessRunner into a stream