Tricky WGPU ownership problem: move occurs because `self.state.event_loop` has type `winit::event_loop::EventLoop<()>`, which does not implement the `Copy` trait

error[E0507]: cannot move out of `self.state.event_loop` which is behind a mutable reference
   --> /home/home/Projects/wgpu_test/src/lib.rs:63:9
    |
63  |           self.state.event_loop.run(move |event, _, control_flow| {
    |  _________^^^^^^^^^^^^^^^^^^^^^_-
    | |         |
    | |         move occurs because `self.state.event_loop` has type `winit::event_loop::EventLoop<()>`, which does not implement the `Copy` trait
64  | |
65  | |         })
    | |__________- `self.state.event_loop` moved due to this method call

Seems like something like this can't be done? :thinking:
Edit:
I forgot to include my code somehow:

    pub fn update(&mut self) {
        self.clear_screen();

        self.state.event_loop.run(move |event, _, control_flow| {

        })
    }

In general, such ownership problems can sometimes be solved with Option and its .take() method. However, though I’m very much not familiar with either winit::event_loop::EventLoop::run nor the update method you have here, I have a suspicion that .run is only supposed to be called exactly once and then keeps running a … well … event loop (i.e. a loop), I suppose (though correct me if I’m wrong) – whereas the name of update(&mut self) suggests a method that’s supposed to be called multiple times. If both of these observations/guesses are correct, then that’s contradictory, i.e. calling a method that’s supposed to be called once from a method that will be called multiple times won’t do any good, no matter what techniques you employ to get Rust’s ownership and borrowing system to compile your code somehow.

The EventLoop should not be stored in your application data structures. Despite the name, it is really more like an event loop builder and is used only once to start the event loop running; you have to give up ownership of it when you call run(), and that's okay because you won't need it.

1 Like

You're right, it should only be called in the start, thank you both