Unused Crate Import breaks lifetimes

Hello!
In my little hobby project i want to add the cpal dependency. When i did, code closely interacting with the winit crate broke with a lifetime issue. I only added the dependency to my Cargo.toml, it isnt used anywhere in the Code. I don't understand why there would be any interaction. I linked the code below. To see the Error just uncomment the Cpal dependecy in Cargo.toml.
I use nightly-rust (need 1 feature) on apple silicon MacOs.
Hope you can help me!

edit: Here is the Error:

error[E0597]: `window` does not live long enough
   --> src/visual/event_loop.rs:33:58
    |
31  |     let window = Window::new(&event_loop).unwrap();
    |         ------ binding `window` declared here
32  |
33  |     let mut gpu_state = pollster::block_on(GPUState::new(&window));
    |                         ---------------------------------^^^^^^^--
    |                         |                                |
    |                         |                                borrowed value does not live long enough
    |                         argument requires that `window` is borrowed for `'static`
...
134 | }
    | - `window` dropped here while still borrowed

error[E0597]: `window` does not live long enough
   --> src/visual/event_loop.rs:43:18
    |
31  |       let window = Window::new(&event_loop).unwrap();
    |           ------ binding `window` declared here
...
43  |       let window = &window;
    |                    ^^^^^^^ borrowed value does not live long enough
44  |       let _ = event_loop.run(move |event, elwt| match event {
    |  _____________-
45  | |         Event::WindowEvent {
46  | |             window_id: _, // can ignore because i only use one window
47  | |             ref event,
...   |
132 | |         _ => {}
133 | |     });
    | |______- argument requires that `window` is borrowed for `'static`
134 |   }
    |   - `window` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
warning: `RustRacker` (bin "RustRacker") generated 1 warning
error: could not compile `RustRacker` (bin "RustRacker") due to 2 previous errors; 1 warning emitted

While these are old and closed they do match your description...
https://github.com/search?q=repo%3Arust-windowing%2Fwinit+cpal&type=issues

My suspicion is either a regression or you'll need to upgrade one or both crates.

1 Like

I don't think was the issue as i am using the most recent version of both crates. Also the Issues you linked (as far as i understood) are about initialization at runtime, while i can't even compile.
But your idea about upgrading made me update my rust version and now it doesn't compile at all, no matter if i have cpal in the project or not, but at least it's consistent now and i can try to understand the issue, so thank you!
cpal apparently requires a certain rust version and apparently is was below that? (while using nightly?) and the newer rust version doesnt accept my code anymore.

1 Like

The 'static bound on the event loop closure was removed in winit 0.29, so it should not fail with that error. I'm also not able to reproduce your compile failure at all. The build works fine for me on both Windows and Linux, regardless of whether cpal is included in the dependency tree.

I'm on a nightly compiler on both platforms, if it matters (doubtful). The Linux nightly is old: rustc 1.78.0-nightly (b6d2d841b 2024-03-05)

The most likely explanation so far is that you are somehow using an older version of winit.

Thank you sooo much!! That was it. I somehow had some weird local version of winit cargo built with. After deleting that it starting working again. Still don't understand how that error got triggered by me adding cpal, but at least it's fixed now.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.