3
votes

I'm trying to iterate over logs from a docker container by using the bollard crate.

Here's my code:

use std::default::Default;

use bollard::container::LogsOptions;
use bollard::Docker;

fn main() {
    let docker = Docker::connect_with_http_defaults().unwrap();

    let options = Some(LogsOptions::<String>{
        stdout: true,
        ..Default::default()
    });

    let data = docker.logs("2f6c52410d", options);
    // ...
}

docker.logs() returns impl Stream<Item = Result<LogOutput, Error>>. I'd like to iterate over the results, but I have no idea how to do that. I've managed to find an example that uses try_collect::<Vec<LogOutput>>() from the future_utils crate, but I'd like to iterate over the results in a while loop instead of collecting the results in a vector. I know that I can iterate over a vector, but performing tasks in a loop will be better for my use case.

I've tried to call poll_next() method for the stream, but it requires a mysterious Context object which I don't understand. The poll_next() method was unavailable until I've used pin_mut!() macro on the stream.

How do I iterate over stream? What should I read to understand what's going on here? I know that the streams are related to Futures, but calling await or next() doesn't work here.

1

1 Answers

3
votes

You typically bring in your library of choice's StreamExt trait, and then do something like

while let Some(foo) = stream.next().await {
    // ...
}