I want to create a trait that will contain a set of functions. But when I compile this code:
trait FnSet<T> {
const FUNCTIONS: &'static [fn(&T)];
}
The compiler prints an error:
error[E0310]: the parameter type `T` may not live long enough
--> src/main.rs:3:5
|
2 | trait FnSet<T> {
| - help: consider adding an explicit lifetime bound...: `T: 'static`
3 | const FUNCTIONS: &'static [fn(&T)];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'static [for<'r> fn(&'r T)]` does not outlive the data it points at
Usually, everything that can be defined as a constant I make a constant. But I see no reason why I couldn't create a constant array of function pointers.
As I understand, the lifetime annotation allows compiler to check a data in memory is alive. But why is this necessary when I have no data? I just have function pointers that own nothing.
I could add T: 'static
bound, but I want this trait to be able to be implemented for structs with references.