On the code below, I'm trying to transform a reference to owned data. The method to_owned
ensures a 'static
lifetime for the new returned type. However, it's complaining that this returned type is not really 'static
because of the impl <'a>
. I don't see what does 'a
have to do with this since all the returned objects in to_owned
are owned
and thus should be 'static
.
pub enum RRtspEncodedPacket<'a> {
Owned(Data<Body>),
Ref(&'a Data<Body>)
}
pub struct Body(Inner);
enum Inner {
Vec(Vec<u8>),
Custom(Box<dyn Custom>),
}
trait Custom: AsRef<[u8]> + Send + Sync + 'static {}
pub struct Data<Body> {
pub(crate) channel_id: u8,
pub(crate) body: Body,
}
pub trait EncodedPacket<'a, T: Send>: Send {
fn to_owned(&self) -> Box<dyn EncodedPacket<'static, T>>;
}
impl<'a> EncodedPacket<'a, u8> for RRtspEncodedPacket<'a> {
fn to_owned(&self) -> Box<dyn EncodedPacket<'static, u8>> {
match self {
Self::Owned(o) => Box::new(Self::Owned(*o.clone())),
Self::Ref(data) => Box::new(Self::Owned(*(*data).clone()))
}
}
}
Error:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/lib.rs:27:40
|
27 | Self::Owned(o) => Box::new(Self::Owned(*o.clone())),
| ^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the impl at 24:6...
--> src/lib.rs:24:6
|
24 | impl<'a> EncodedPacket<'a, u8> for RRtspEncodedPacket<'a> {
| ^^
note: ...so that the types are compatible
--> src/lib.rs:27:40
|
27 | Self::Owned(o) => Box::new(Self::Owned(*o.clone())),
| ^^^^^^^^^^^
= note: expected `RRtspEncodedPacket<'_>`
found `RRtspEncodedPacket<'a>`
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
--> src/lib.rs:27:31
|
27 | Self::Owned(o) => Box::new(Self::Owned(*o.clone())),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `Box<(dyn EncodedPacket<'static, u8> + 'static)>`
found `Box<dyn EncodedPacket<'static, u8>>`