0

I'm trying to code a wrapper struct around the creation of an event_loop and a graphical context:

struct GraphicalContext {
    pub el: crow::glutin::event_loop::EventLoop<()>,
    pub ctx: crow::Context
}

impl GraphicalContext {
    pub fn new(wb: WindowBuilder) -> Result<GraphicalContext, Box<dyn Error>> {
        let el = EventLoop::new();
        let ctx = Context::new(wb, &el)?;
        Ok( GraphicalContext { el, ctx } )
    }
}

fn main() {
    let window_bld = crow::glutin::window::WindowBuilder::new();
    let mut gc = GraphicalContext::new(window_bld).unwrap();
    let mut screen_texture: Option<crow::Texture> = None;
    gc.el.run(move |event, _, control_flow| {
        match event {
            ...
            Event::RedrawRequested(..) => {
                Some(t) => {
                    let mut surface = gc.ctx.surface();
                    gc.ctx.draw(&mut surface, &t, (0, 0), &DrawConfig::default());
                    gc.ctx.present(surface).unwrap(); // swap back-buffer
                },
                None => ()
            }
        }
    }

}

Problem is that the compiler complains that gc has been partially moved when gc.el.run() is called. It tells me it's because run() takes ownership of self but in this case, self is just gc.el, but I can't use gc.ctx inside the closure. At first I thought it was because the closure used move, however, removing it didn't fix the problem and gave me another error. Is there any way I can go around this?

SandWood Jones
  • 104
  • 1
  • 9

1 Answers1

1

What you want is to destructure your structure to get its parts, so that you can move only one of them to the closure:

let GraphicalContext { el, ctx } = gc;
el.run(move |event, _, control_flow| {
    // use ctx
});
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • @SandWoodJones You can learn more about this use of pattern matching here: https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html#destructuring-to-break-apart-values – Denys Séguret May 13 '21 at 05:53
  • 2
    The original code might be accepted by the compiler in [edition 2021](https://blog.rust-lang.org/2021/05/11/edition-2021.html#disjoint-capture-in-closures). – user4815162342 May 13 '21 at 07:21
  • @user4815162342 Your comment might be a new answer for future viewers – Denys Séguret May 13 '21 at 08:10