Skip to content

lifetime problem for log stream #65

@Siddius

Description

@Siddius

Hi,

I need to abstract logs, but run into some lifetime / borrow issues. Is this just something generally not possible in this specific api due to rust or am i doing something wrong ?

use docker_api::opts::LogsOpts;
use docker_api::*;
use futures::{Stream, TryStreamExt,StreamExt};
use std::result::Result;

struct SomeWrapper {
    runtime: Docker,
}

impl SomeWrapper {
    fn logs(
        &self,
        container_id: &str,
    ) -> impl Stream<Item = Result<String, String>> + Send + Unpin {
        let logs_opts = LogsOpts::builder()
            //.follow(true)
            .stdout(true)
            .stderr(true)
            .timestamps(true)
            .build();

        let container = self.runtime.containers().get(container_id);
        let log_lines = container
            .logs(&logs_opts)
            .map_ok(|message| message.escape_ascii().to_string())
            .map_err(|e| e.to_string());
        log_lines
    }
}

#[tokio::main]
async fn main()  {
    let w = SomeWrapper { runtime: Docker::new("/var/run/docker.socks").unwrap() };
    let mut log_stream= w.logs("<someID>");
    match log_stream.next().await {
        Some(event) => {
            dbg!(event);
        }
        None => {
            println!("Channel Closed")
        }
    }
}

which leads to

error[E0597]: `container` does not live long enough
  --> src\main.rs:23:25
   |
22 |           let container = self.runtime.containers().get(container_id);
   |               --------- binding `container` declared here
23 |           let log_lines = container
   |                           -^^^^^^^^
   |                           |
   |  _________________________borrowed value does not live long enough
   | |
24 | |             .logs(&logs_opts)
   | |_____________________________- argument requires that `container` is borrowed for `'static`
...
28 |       }
   |       - `container` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.

I tried lifetime annotations, Box , Rc but honestly i am totally lost and don't see how to solve this. With 'static its still a error[E0515]: cannot return value referencing local variable container`

error[E0515]: cannot return value referencing local variable `container`
  --> src\main.rs:27:9
   |
23 |         let log_lines = container
   |                         --------- `container` is borrowed here
...
27 |         log_lines
   |         ^^^^^^^^^ returns a value referencing data owned by the current function

For more information about this error, try `rustc --explain E0515`.
error: could not compile `docker-logs` (bin "docker-logs") due to previous error

For events that apporach is working fine, but i don't get it working for logs .

        self
            .runtime
            .events(&opts)
            .map_ok(|message| message.escape_ascii().to_string())
            .map_err(|e| e.to_string());

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions