I have a struct in rust:
pub struct Application {
name: String,
windows: Vec<Window>,
event_loop: EventLoop<()>,
}
in here I have a method which moves self and runs an event loop which takes a move closure. I still want to reference self, as it has some methods I need to run according to events. It looks like this
pub fn run(mut self) {
self.event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent {
window_id,
..
} => {
&self.send_event_to_window(window_id, event);
},
Event::RedrawRequested(window_id) => {
&self.send_event_to_window(window_id, event);
}
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
// state.window().request_redraw();
}
_ => {},
});
}
rust has an issue with me using self.send_event_to_window and says
"use of partially moved value: self
"
How do I access self from this closure? Im not sure even why rust doesn't let me use it as I move it into the closure.
self
into the closure, which means it can no longer be used anywhere except in the closure. But then you callrun
on it.move
keyword here?winit
, take a look at the examples. None of them are structured the way that your code is. e.g. github.com/rust-windowing/winit/blob/master/examples/…You moved self into the closure, which means it can no longer be used anywhere except in the closure.
yeah exactly, but im trying to use it in the closure calling&self.send_event_to_window
event_loop
a field of your application struct? Ask yourself if it's necessary. And look at thewinit
examples for ideas.