我对生命周期问题感到困惑,我认为这涉及到FnMut的关闭和Context的生命周期。尽管我不确定如何赋予&Context静态寿命,因为它是由self.scene.update()推断的。
我认为错误中的&context :: Context生存期引用是指使用self隐式捕获的上下文。
pub struct DisplayWrapper(glium::Display);
pub struct Context
{
pub event_loop: glutin::event_loop::EventLoop<()>,
pub display: DisplayWrapper,
pub program: Program,
pub scene: Box<Scene>,
}
impl Context {
pub fn new(scene: Box<Scene>) -> Self {
let event_loop = glutin::event_loop::EventLoop::new();
let window_builder = glutin::window::WindowBuilder::new();
let context_builder = glutin::ContextBuilder::new();
let display = DisplayWrapper(
glium::Display::new(window_builder, context_builder, &event_loop).unwrap(),
);
let vertex_shader = Shader::new(Path::new("resources/shaders/vertex.glsl"));
let fragment_shader = Shader::new(Path::new("resources/shaders/fragment.glsl"));
let program = Program::new(&display.0, vertex_shader, fragment_shader);
Context {
event_loop,
display,
program,
scene,
}
}
pub fn run(&self) {
self.event_loop.run(move |event, _, control_flow| {
let next_frame_time =
std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667);
*control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);
match event {
glutin::event::Event::WindowEvent { event, .. } => match event {
glutin::event::WindowEvent::CloseRequested => {
*control_flow = glutin::event_loop::ControlFlow::Exit;
return;
}
_ => return,
},
glutin::event::Event::NewEvents(cause) => match cause {
glutin::event::StartCause::ResumeTimeReached { .. } => (),
glutin::event::StartCause::Init => (),
_ => return,
},
_ => return,
}
self.scene.update();
let mut target = self.display.0.draw();
target.clear_color(0.0, 0.0, 0.0, 1.0);
self.scene.render();
target.finish().unwrap();
});
}
}
我收到此错误
|
42 | self.event_loop.run(move |event, _, control_flow| {
| _____________________________^
43 | | let next_frame_time =
44 | | std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667);
45 | | *control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);
... |
68 | | target.finish().unwrap();
69 | | });
| |_________^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 41:5...
--> honeycrisp\src\context.rs:41:5
|
41 | / pub fn run(&self) {
42 | | self.event_loop.run(move |event, _, control_flow| {
43 | | let next_frame_time =
44 | | std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667);
... |
69 | | });
70 | | }
| |_____^
= note: ...so that the types are compatible:
expected &context::Context
found &context::Context
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@honeycrisp\src\context.rs:42:29: 69:10 self:&context::Context]` will meet its required lifetime bounds
--> honeycrisp\src\context.rs:42:25
|
42 | self.event_loop.run(move |event, _, control_flow| {