Error with OpenGL events loop

I want my function to be the event loop so I've make that:

pub fn windowEvent(self) -> (std::ops::FnMut(glutin::Event) -> ()){
    return |event| {
        match event {
            glutin::Event::WindowEvent{ event, .. } => match event {
                glutin::WindowEvent::CloseRequested => process::exit(0x0100),
                glutin::WindowEvent::Resized(logical_size) => {
                    let dpi_factor = self.gl_window.get_hidpi_factor();
                    self.gl_window.resize(logical_size.to_physical(dpi_factor));
                },
                _ => ()
            },
            _ => ()
        }
    };
}

And in my loop

frame.events_loop.poll_events(frame.windowEvent());

But I have two errors:

error[E0277]: the trait bound `std::ops::FnMut(glutin::Event) + 'static: std::marker::Sized` is not satisfied
  --> src/window.rs:48:34
   |
48 |     pub fn windowEvent(self) -> (std::ops::FnMut(glutin::Event) -> ()){
   |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `std::ops::FnMut(glutin::Event) + 'static` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `std::ops::FnMut(glutin::Event) + 'static`
   = note: the return type of a function must have a statically known size

error[E0308]: mismatched types
  --> src/window.rs:49:16
   |
49 |           return |event| {
   |  ________________^
50 | |             match event {
51 | |                 glutin::Event::WindowEvent{ event, .. } => match event {
52 | |                     glutin::WindowEvent::CloseRequested => process::exit(0x0100),
...  |
60 | |             }
61 | |         };
   | |_________^ expected trait std::ops::FnMut, found closure
   |
   = note: expected type `std::ops::FnMut(glutin::Event) + 'static`
              found type `[closure@src/window.rs:49:16: 61:10 self:_]`

If you can help me.
Sorry for my English because I'm french.

I'm just a beginner, but let me try to explain how I understand it.

FnMut is a trait and traits do not have a size that is known as compile time. You need to either create a trait-object by returning Box<FnMut...> or if your version allows it use impl FnMut....

You can see something similar in action here on the playground

3 Likes