0

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.

10
  • "Im not sure even why rust doesn't let me use it as I move it into the closure." -- You moved self into the closure, which means it can no longer be used anywhere except in the closure. But then you call run on it.
    – Peter Hall
    Commented Oct 7, 2023 at 17:56
  • What prompted you to use the move keyword here?
    – Peter Hall
    Commented Oct 7, 2023 at 17:57
  • Assuming you are using 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/…
    – Peter Hall
    Commented Oct 7, 2023 at 18:00
  • 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
    – Thomas
    Commented Oct 7, 2023 at 18:01
  • Why did you make event_loop a field of your application struct? Ask yourself if it's necessary. And look at the winit examples for ideas.
    – Peter Hall
    Commented Oct 7, 2023 at 18:03

1 Answer 1

2

When you write self.eventLoop.run in Application.run, then EventLoop.run has ownership of self.eventLoop (partial ownership of self). As such the closure cannot call any method on self, because such a method might use self.eventLoop.

There are a couple of ways of fixing this:

  1. Make event_loop a local variable in the run function
  2. Make event_loop a parameter of the run function
  3. Put the variables needed by event_loop into a separate struct like so:
pub struct ApplicationState {
  name: String,
  windows: Vec<Window>,
  // put anything that needs to be accessible by the run closure here
}
impl ApplicationState {
 // implement functions that do not use event_loop here
 // these can be called by the run closure
 fn send_event_to_window(window_id, event) {}
}
pub struct Application {
  state: ApplicationState,
  event_loop: EventLoop<()>,
}
impl Application {
      pub fn run(mut self) {
        self.event_loop.run(move |event, _, control_flow| match event {
            Event::WindowEvent {
                window_id,
                ..
            } => {
                &self.state.send_event_to_window(window_id, event);
            },
            Event::RedrawRequested(window_id) => {
                &self.state.send_event_to_window(window_id, event);
            }
            Event::MainEventsCleared => {
                // RedrawRequested will only trigger once, unless we manually
                // request it.
                // state.window().request_redraw();
            }
            _ => {},
        });
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.