I am updating a project to the newest WGPU version which adds a lifetime parameter to wgpu::Surface<'window>
that is causing me problems with the trait winit exposes for the window initialization winit::application::ApplicationHandler
.
A smaller version of the problem is as follows:
use wgpu::{Dx12Compiler, Gles3MinorVersion, Instance, InstanceDescriptor, InstanceFlags, Surface};
use winit::{
application::ApplicationHandler,
event_loop::EventLoop,
window::{Window, WindowAttributes},
};
struct Application<'a> {
event_loop: EventLoop<()>,
window: Option<Window>,
surface: Option<Surface<'a>>,
}
impl<'a> ApplicationHandler for Application<'a> {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
if self.window.is_none() {
self.window = Some(
event_loop
.create_window(WindowAttributes::default())
.unwrap(),
);
let instance_descriptor = InstanceDescriptor {
backends: wgpu::Backends::VULKAN,
flags: InstanceFlags::default(),
dx12_shader_compiler: Dx12Compiler::Fxc,
gles_minor_version: Gles3MinorVersion::Automatic,
};
let instance = Instance::new(instance_descriptor);
self.surface = Some(
instance
.create_surface(self.window.as_ref().unwrap())
.unwrap(),
);
}
}
fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId,
event: winit::event::WindowEvent,
) {
}
}
The above code yields lifetime may not live long enough
referencing self.surface = Some(...)
noting that the implicit lifetime of &mut self
is not guaranteed to live as long as 'a
I cannot augment the resumed trait method with lifetime bounds like resumed<'b: 'a>(&'b mut self, ...)
so I cannot see a way to reconcile the lifetimes.
I’ve also tried structuring the types as such
struct Inner<'a> {
window: Window,
surface: Option<Surface<'a>>
}
struct Outer<'a> {
event_loop: EventLoop<()>,
inner: Option<Inner<'a>>,
}
which lets me rationalize that the lifetime of the window is guaranteed to be as long as the lifetime of the surface, but this obviously doesn’t change the compiler error, and adds a problem of circular reference.
So my question is: Given the signature of ApplicationHandler::resumed
and the lifetime requirements how do we create a wgpu surface?