The key error seems to be this one:
error[E0597]: `body` does not live long enough
--> src/lib.rs:85:15
|
85 | resp: body.lock().unwrap().clone(),
| ^^^^ borrowed value does not live long enough
...
89 | }
| - `body` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
The same for the headers object.
I was able to get a simplified reproducer of this by stubbing out a lot of your code:
use std::sync::{Arc, Mutex};
pub struct Response {
resp: String,
headers: Vec<String>,
}
pub fn get(addr: &str) -> std::io::Result<Box<Response>> {
let headers: Vec<String> = Vec::with_capacity(10);
let headers = Arc::new(Mutex::new(headers));
let body = Arc::new(Mutex::new(String::with_capacity(1024)));
Ok(Box::new(Response {
resp: body.lock().unwrap().clone(),
headers: headers.lock().unwrap().clone(),
}))
}
I think this has to do with the lifetimes of the temporary variables constructed in the final Ok(Box::new(...))
return values.
I was able to get it to compile by pulling the lock/unwrap outside.
let body = body.lock().unwrap();
let headers = headers.lock().unwrap();
Ok(Box::new(Response {
resp: body.clone(),
headers: headers.clone(),
}))
From the fuller explaination given in Why do I get "does not live long enough" in a return value? I've found that you can write this as
return Ok(Box::new(Response {
resp: body.lock().unwrap().clone(),
headers: headers.lock().unwrap().clone(),
}));
i.e. adding an explicit return
and a trailing semicolon. Though I have a feeling clippy might say that its bad style.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…