我有下面的代码,我尝试在winit板条箱上创建一个翘曲。我想包装winit,因为我希望将来能够用最少的工作替换它。
代码无法编译,但我想不出一种方法来使它工作。如果有人对如何保持我的设计有一些建议,但同时包装winit请让我知道。
谢谢!
main.rs
use crate::events::WindowResizeEvent;
use crate::window::Window;
use crate::window::WindowProps;
mod events;
mod window;
fn main() {
let props = WindowProps {
title: "Foo".to_string(),
width: 1024,
height: (1024.0 * 16.0 / 9.0) as usize,
};
let mut main_window = Window::new(props);
main_window.set_on_window_resize(|event: WindowResizeEvent| {
println!("{}", event);
});
main_window.run();
}events.rs
use std::fmt::{Display, Formatter};
pub struct WindowResizeEvent {
width: u32,
height: u32,
}
impl Display for WindowResizeEvent {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
fmt.write_fmt(format_args!(
"WindowResizeEvent[width = {}, height = {}]",
self.width, self.height
))
}
}
pub struct WindowCloseEvent;
impl Display for WindowCloseEvent {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
fmt.write_str("WindowCloseEvent")
}
}最后是window.rs
use winit::{
dpi::LogicalSize,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use crate::events::{WindowCloseEvent, WindowResizeEvent};
pub struct WindowProps {
pub title: String,
pub width: usize,
pub height: usize,
}
pub struct Window {
event_loop: EventLoop<()>,
window: winit::window::Window,
on_window_resize: Option<fn(WindowResizeEvent)>,
on_window_close: Option<fn(WindowCloseEvent)>,
}
impl Window {
pub fn new(props: WindowProps) -> Self {
let event_loop = EventLoop::with_user_event();
let window = WindowBuilder::new()
.with_visible(false)
.with_title(&props.title)
.with_inner_size(LogicalSize::new(props.width as f32, props.height as f32))
.build(&event_loop)
.expect("Unable to create window");
Self {
event_loop,
window,
on_window_resize: None,
on_window_close: None,
}
}
pub fn set_on_window_resize(&mut self, f: fn(WindowResizeEvent)) {
self.on_window_resize = Some(f);
}
pub fn run(&self) {
self.event_loop
.run(move |event, _, control_flow| match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::Resized(physical_size) => {
if let Some(on_window_resize) = self.on_window_resize.clone() {
on_window_resize(WindowResizeEvent {
width: physical_size.width,
height: physical_size.height,
});
}
}
WindowEvent::CloseRequested => {
if let Some(on_window_close) = self.on_window_close.clone() {
on_window_close(WindowCloseEvent);
}
*control_flow = ControlFlow::Exit;
}
_ => (),
},
_ => (),
});
}
}我想要winit上的抽象层,因为我更喜欢它,也许在某个时候我想用其他东西替换winit,我不想改变所有可能使用窗口的地方。
现在,当我试图编译它时,我得到了这个错误,并且我找不到一种方法来使它工作:
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> ghost-sandbox/src/window.rs:49:18
|
47 | pub fn run(&self) {
| ----- this data with an anonymous lifetime `'_`...
48 | self.event_loop
49 | .run(move |event, _, control_flow| match event {
| __________________^
50 | | Event::WindowEvent { event, .. } => match event {
51 | | WindowEvent::Resized(physical_size) => {
52 | | if let Some(on_window_resize) = self.on_window_resize.clone() {
... |
69 | | _ => (),
70 | | });
| |_____________^ ...is captured here...
|
note: ...and is required to live as long as `'static` here
--> ghost-sandbox/src/window.rs:49:14
|
49 | .run(move |event, _, control_flow| match event {
| ^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0759`.
error: could not compile `ghost-sandbox`https://stackoverflow.com/questions/67286017
复制相似问题