Hi,
while trying to follow the wgpu tutorial using the latest versions of winit and wpug, I run into some self-referentiel struct error I can't fix.
The winit sample is as follow:
struct App {
window: Option<Window>,
}
impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
self.window = Some(event_loop.create_window(Window::default_attributes()).unwrap();
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent) {
todo!()
}
}
Now the wgpu tutorial creates a render State as follow:
struct State<'a> {
surface: wgpu::Surface<'a>,
....
window: &'a Window
}
impl<'a> State<'a> {
async fn new(window: &'a Window) -> State<'a> {
...
let instance = wgpu::Instance::new(...);
let surface = instance.create_surface(window).unwrap();
...
Self {
window,
surface,
...
}
}
}
I was planning to call this State::new()
method within a pollster::block_on()
call inside the ApplicationHandler::resumed()
method, something like this:
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let window = event_loop
.create_window(Window::default_attributes())
.unwrap();
let state = pollster::block_on(State::new(&window)); // (1)
self.window = Some(window); // (2)
self.state = Some(state);
}
But this creates 2 related issues:
- In (1):
window does not live long enough
- in (2):
Cannot move out of 'window' because it is borrowed
I can "fix" (2) by first moving window
into App and then borrowing a reference to it as follow:
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
let window = event_loop
.create_window(Window::default_attributes())
.unwrap();
self.window = Some(window);
let state = pollster::block_on(State::new(self.window.as_ref().unwrap()));
self.state = Some(state); // (3)
}
But now the compiler complains in (3) that the Lifetime may not live long enough
.
Although both the window and the state are owned by the App the compiler can't seem to figure this out. I miss something. Is it just a case of lifetime annotation ? And/or self-referential struct (App -> surface -> window + App -> window) ?
Any help welcome
Thanks for reading.
Brieuc