Fltk-rs issues with accessing one Arc Mutex variable in two closures

Hey guys I am using fltk-rs.

I have this code:

fn addition_type_ctrl(frame: &Frame, is_adding: &Arc<Mutex<bool>>)
{
    let is_adding = Arc::clone(is_adding);

    let mut return_val = true;
    // with_size is button size
    let mut flex = Flex::default().with_size(50, 20).center_of(frame).below_of(frame, 0).row();

    // Buttons
    struct AdditionType
    {
        add: RadioButton,
        sub: RadioButton,
    }

    let mut addition_type = AdditionType
    {
        add: RadioButton::default().with_label("+"),
        sub: RadioButton::default().with_label("-"),
    };

    //let mut but_add = RadioButton::default().with_label("+");
    addition_type.add.clear_visible_focus();
    addition_type.sub.clear_visible_focus();

    let mut tmp = RadioButton::default().with_label("+");

    //tmp.set_callback(move |_|
    addition_type.add.set_callback(move |_|
    {
        let is_adding= is_adding.lock().unwrap();
        button_pressed(*is_adding);
    });

    addition_type.sub.set_callback(move |_|
    {
        let is_adding= is_adding.lock().unwrap();
        button_pressed(*is_adding);
    });
    //addition_type.add.toggle(*is_adding);
    

    

    //let mut but_sub = RadioButton::default().with_label("-");
    //addition_type.sub.();
    


    flex.end();

}

and I get this error:

error[E0382]: use of moved value: `is_adding`
  --> src/adding_time.rs:54:36
   |
22 |     let is_adding = Arc::clone(is_adding);
   |         --------- move occurs because `is_adding` has type `Arc<Mutex<bool>>`, which does not implement the `Copy` trait
...
48 |     addition_type.add.set_callback(move |_|
   |                                    -------- value moved into closure here
49 |     {
50 |         let is_adding= is_adding.lock().unwrap();
   |                        --------- variable moved due to use in closure
...
54 |     addition_type.sub.set_callback(move |_|
   |                                    ^^^^^^^^ value used here after move
55 |     {
56 |         let is_adding= is_adding.lock().unwrap();
   |                        --------- use occurs due to use in closure

I need both the - and the + to access the variable is_adding in the same function and not too sure what to do over here?

You need to clone is_adding for each closure it is moved into. One way to do this (I did not compile this):

        addition_type.add.set_callback({
            let is_adding = Arc::clone(is_adding);
            move |_| {
                let is_adding = is_adding.lock().unwrap();
                button_pressed(*is_adding);
            }
        });
1 Like