I’m getting this rust lifetime error, but I don’t understand why it happens:
error: lifetime may not live long enough
--> src/console.rs:454:39
|
422 | conn: &mut dyn ConnectionTrait<TlsStream<TcpStream>>,
| ---- - let's call the lifetime of this reference `'1`
| |
| has type `&mut dyn ConnectionTrait<'2, TlsStream<TcpStream>>`
...
454 | let logger = ConsoleLogger::new(conn);
| ^^^^ cast requires that `'1` must outlive `'2`
The (reduced) code looks like this:
pub trait ConnectionTrait<'a, S>: Read + Write + BufRead + Send + Sync {
}
pub struct ConsoleLogger<'a> {
conn: &'a mut dyn ConnectionTrait<'a, TlsStream<TcpStream>>,
}
impl<'a> ConsoleLogger<'a> {
pub fn new(conn: &'a mut dyn ConnectionTrait<'a, TlsStream<TcpStream>>) -> Self {
ConsoleLogger { conn }
}
}
impl log::Log for ConsoleLogger<'_> {
// ...
}
pub fn test123(logger: &dyn log::Log) -> Result<()> {
Ok(())
}
fn cmd_test(&self, conn: &mut dyn ConnectionTrait<TlsStream<TcpStream>>) -> Result<Option<String>> {
// some code...
{
let logger = ConsoleLogger::new(conn);
if let Err(err) = test123(&logger) {
bail!("failed: {}", err);
}
}
Ok(Some(format!("ok")))
}
To me clearly conn
outlives logger
, so I don’t get why this error happens. How can it be fixed, in the most simple way?