I have a trait that returns a borrow attached to its own lifetime:
trait SomeTrait {
fn do<'a>(&'a self, other: &AnonymousLifetime) -> &'a Output;
}
How can this same restriction be expressed in a where clause for a closure, so that SomeTrait
can impl From<Closure>
?
Example
A minimal, reproducible example for the scenario (playground):
// The traits
trait Context {
fn give(&self) -> usize;
}
trait ContextDecider {
fn decide<'a>(&'a self, context: &dyn Context) -> &'a str;
}
// A concrete implementation example
// As expected, works OK
struct SomeDecider(Vec<String>);
impl ContextDecider for SomeDecider {
fn decide<'a>(&'a self, context: &dyn Context) -> &'a str {
let some_context = context.give();
if some_context > self.0.len() {
panic!("Oh no!");
}
&self.0[some_context]
}
}
// An implemetation for a closure
// Help here!!
impl<'a, F> ContextDecider for F
where
F: 'a + Fn(&dyn Context) -> &'a str,
{
fn decide<'b>(&'b self, giver: &dyn Context) -> &'b str {
self(giver)
}
}
Fails to compile:
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src/lib.rs:30:9
|
30 | self(giver)
| ^^^^^^^^^^^
|
note: ...the reference is valid for the lifetime `'b` as defined on the method body at 29:15...
--> src/lib.rs:29:15
|
29 | fn decide<'b>(&'b self, giver: &dyn Context) -> &'b str {
| ^^
note: ...but the borrowed content is only valid for the lifetime `'a` as defined on the impl at 25:6
--> src/lib.rs:25:6
|
25 | impl<'a, F> ContextDecider for F
| ^^
In the example, I am failing to express in the closure bounds the restriction that the trait imposes and the compiler is not happy.
The compiler is not helping me with what syntax I should use that will allow me to lock in the two lifetimes together.