#Odd E0594 compiler error

5 messages · Page 1 of 1 (latest)

zenith zinc
#

Odd for someone that worked with C++ for a while at least.

use winsafe::co::BS;
use winsafe::{self as w, gui, msg, prelude::*};

fn main() {
    let main_window = Window::new();
    if let Err(e) = main_window.wnd.run_main(None) {
        eprintln!("main_window.run_main() error: {}", e);
    }
}

#[derive(Clone)]
pub struct Window {
    wnd: gui::WindowMain,
    btn_install: gui::Button,
}

struct Common {
    left: i32,
    top: i32,
    center_width: u32,
    center_height: u32,
}

impl Window {
    pub fn new() -> Self {
        let mut common = Common {
            left: 0,
            top: 0,
            center_width: 0,
            center_height: 0,
        };

        let wnd = gui::WindowMain::new(gui::WindowMainOpts {
            title: "Example".to_owned(),
            size: (800, 600),
            ..Default::default()
        });

        wnd.on().wm_create(move |p: msg::wm::Create| -> w::AnyResult<i32> {
            println!("Created window");
            common.left = p.createstruct.x;
            common.top = p.createstruct.y;

            common.center_width = (common.left / 2) as u32;
            common.center_height = (common.top / 2) as u32;

            println!("Left: {} Top: {}", common.left, common.top);
            Ok(0)
        });

        let btn_install = gui::Button::new(&wnd, gui::ButtonOpts {
            text: "&Install".to_owned(),
            position: (common.left, common.top),
            button_style: BS::FLAT,
            width: common.center_width,
            height: common.center_height,
            ..Default::default()
        });

        let window_self = Self { wnd, btn_install };
        window_self.events();
        window_self
    }

    fn events(&self) {
        let wnd = self.wnd.clone();
        self.btn_install.on().bn_clicked(move || {
            wnd.hwnd().SetWindowText("It registered a click!")?;
            Ok(())
        });
    }
}
#
error[E0594]: cannot assign to `common.left`, as it is a captured variable in a `Fn` closure
  --> src/main.rs:41:13
   |
41 |             common.left = p.createstruct.x;
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot assign

Repeats for the rest of the Common elements

rare stream
#

wm_create takes a Fn closure, not an FnMut. Such a closure cannot mutate the variables it captures.

#

I don't know why the API chooses to take an Fn, but it does, so you can't do this.

zenith zinc
#

thank you, the website wasn't clear on what a closure meant