I have a struct with a lifetime and some methods:
struct Ctrl<'a> {
x: &'a i32,
}
impl<'a> Ctrl<'a> {
fn foo(&self) -> i32 {
*self.x + 1
}
fn bar(&self) -> i32 {
*self.x + 2
}
}
Now I want to store pointers to the methods in a slice, kind of like a look-up table:
const LIST: &[_] = &[Ctrl::foo, Ctrl::bar];
Rust demands to know the slice element type and suggests for<'r> fn(&'r Ctrl) -> i32, but this results in an error:
error[E0308]: mismatched types
--> main.rs:16:48
|
16 | const LIST: &[for<'r> fn(&'r Ctrl) -> i32] = &[Ctrl::foo, Ctrl::bar];
| ^^^^^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'s, 'r> fn(&'r Ctrl<'s>) -> _`
found fn pointer `for<'r> fn(&'r Ctrl<'_>) -> _`
Is there a way to specify the correct type?