A thread should read content from stdout or stderr in pass the read content to via a channel. Now want to create one Thread for stdout and one for stderr, because I they have the same behaviour I dont want to pass the ChildStdout and ChildStderr structs rather than the a generic that implements the Read trait.
This is my method:
fn start_read_thread<T : Read + Send>(stdout: Option<T>, process: &mut Child) -> Result<Receiver<Result<Vec<u8>, ()>>, ()> {
let (tx_stdout, rx_stdout): (Sender<Result<Vec<u8>, ()>>, Receiver<Result<Vec<u8>, ()>>) = mpsc::channel();
std::thread::spawn(move || {
for b in stdout.unwrap().bytes() {
let b = b.unwrap();
}
});
return Ok(rx_stdout);
}
Now the compiler says:
the parameter type `T` may not live long enough
--> shell_execution\src\powershell.rs:146:17
|
142 | fn start_read_thread<'a, T : Read + Send>(stdout: Option<T>, process: &mut Child) -> Result<Receiver<Result<Vec<u8>, ()>>...
| -- help: consider adding an explicit lifetime bound...: `T: 'static +`
...
146 | std::thread::spawn(move || {
| ^^^^^^^^^^^^^^^^^^ ...so that the type `[closure@shell.rs:146:36: 148:18]` will meet its required lifetime bounds
Should T not always live long enough, because it gets moved to the thread scope it can't be dropped somewhere else?
Replacing T with ChildStdout works, without the need of adding the static lifetime.
Does someone know why this is happening, and do I build a memory leak with this static lifetime?
Thanks