Please help me understand the problem and how to resolve it.
I have a main function that creates the application and calls the run function.
fn main() {
let application: Application = Application::new();
application.run();
}
The compiler does not like that because the self has a limited lifespan.
I want application to run for the lifetime of the application though.
The error I am getting is this.
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> src\.\core\application.rs:22:29
|
21 | pub fn run(&self) {
| ----- this data with an anonymous lifetime `'_`...
22 | self.event_loop.run(move |event, _, control_flow| match event {
| _____________________________^
23 | |
24 | | Event::WindowEvent {
25 | | ref event,
... |
41 | | _ => {}
42 | | });
| |_________^ ...is captured here...
|
note: ...and is required to live as long as `'static` here
--> src\.\core\application.rs:22:25
|
22 | self.event_loop.run(move |event, _, control_flow| match event {
| ^^^
I changed the parameter to look like this
pub fn run(&'static self) {
aparently there is a scope problem when trying to run it though
error[E0597]: `application` does not live long enough
--> src\main.rs:11:5
|
11 | application.run();
| ^^^^^^^^^^^------
| |
| borrowed value does not live long enough
| argument requires that `application` is borrowed for `'static`
12 | }
| - `application` dropped here while still borrowed
Just spitballing because there is somewhat limited information here, but as this looks like some sort of gui program, and I've run into this dilemma with gtk-rs, try moving run out of the impl block and calling it as a freestanding function.
When the compiler says it wants 'static, it's very poorly trying to say that all temporary references are forbidden (@ekuber any chance of removing misleading 'static from errors?)
No amount of lifetime annotations can solve this problem. You can't take a temporarily borrowed argument of a function and pass it to a thread that may live for as long as it wants (which event_loop.run most likely wants to do).
If you can, you need to change the temporary scope-bound &self to an owned self that can be moved to the event loop. Alternatively, if you need to use it in multiple places, try Arc<Self> or reorganize the code in a way that the even loop doesn't use self.
Generally, when compiler demands 'static, ignore it, and keep wrapping stuff in Arc or Arc<Mutex> until it compiles. Don't use references.
If you have only one instance of Application, then the last-resort hack is to use Box::leak to make a leaked reference, which actually is 'static like the compiler wanted.
This is probably the easiest way I've found to do it:
Pass self by value, destructure it and claim the necessary mutability on each field, and use event_loop.run_return(...) instead of event_loop.run(...).
I've thought about removing 'static messages altogether, and we've removed a bunch of suggestions and tweaked some errors, but this would be a whole project to actually cover every diagnostic people might get. I'm in favor of doing that, but in the meantime it'd be useful to file targeted tickets so that anyone with time to spare can tackle them piecemeal.