When I don't include the + 'static in this code, rustc complains. Why?
// adapted from rust-analyzer https://github.com/rust-analyzer/rust-analyzer
trait Upcast<T: ?Sized> {
fn upcast(&self) -> &T;
}
trait DefDatabase {}
trait HirDatabase: Upcast<dyn DefDatabase> {}
struct RootDatabase{}
impl Upcast<dyn DefDatabase> for RootDatabase {
fn upcast(&self) -> &(dyn DefDatabase + 'static) {
&*self
}
}
`impl` item signature doesn't match `trait` item signature
expected `fn(&RootDatabase) -> &(dyn DefDatabase + 'static)`
found `fn(&RootDatabase) -> &dyn DefDatabase`
the lifetime requirements from the `impl` do not correspond to the
requirements in the `trait`
verify the lifetime relationships in the `trait` and `impl` between the
`self` argument, the other inputs and its output
Why is 'static lifetime required?
It seems like the dyn is essential to the example. rustc does not complain about this impl, which doesn't mention 'static:
impl Upcast<RootDatabase> for RootDatabase {
fn upcast(&self) -> &Self {
self
}
}