The code goes something like this:
struct Model {
// This is Vec #1.
recorded_curves: Vec<Curve>,
// This is the Vec whose elements hold references to #1's.
ongoing_comparisons: Vec<Comparison>,
}
struct Comparison {
// `recording` references an element in `Model.recorded_curves`.
recording: &Curve,
// ...
}
And later, inside a function called event()
, I have this:
for recording in model.recorded_curves.iter() {
model.ongoing_comparisons.push(Comparison {
recording,
// ...
});
}
Rust demands I add lifetimes to Comparison
, but to do that, I have to (as far as I know) use the same lifetime for Model
. Afterward, the structs looked like this:
struct Comparison<'a> {
recording: &'a Curve,
// ...
}
struct Model<'a> {
recorded_curves: Vec<Curve>,
ongoing_comparisons: Vec<Comparison<'a>>,
}
fn event<'a>(_app: &App, model: &'a mut Model<'a>, event: Event) { /* ... */ }
Unfortunately, this brings its own problem. I'm using nannou, where I register event()
as a callback, and it doesn't let me use my custom lifetime ('a
):
error[E0308]: mismatched types
--> src/main.rs:17:30
|
17 | nannou::app(model).event(event).simple_window(view).run();
| ^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'r, 's> fn(&'r nannou::App, &'s mut Model<'_>, nannou::Event)`
found fn pointer `for<'a, 'r> fn(&'r nannou::App, &'a mut Model<'a>, nannou::Event)`
Is there a way to accomplish this without using lifetime annotations? Otherwise, could I somehow make the program work with the annotations?