rdev icon indicating copy to clipboard operation
rdev copied to clipboard

captured variable in a `Fn` closure cannot assign

Open yaojiu19 opened this issue 2 years ago • 3 comments

when i use grab, i cant modify environment variable;

let mut button_right_act= false;
let mut is_block = false;
rdev::grab(move |event| {
    match event.event_type {
        EventType::ButtonPress(button) => {
            match button {
                Button::Right => {
                    button_right_act = true;
                    is_block = false;
                },
                _ => {}
            }
        },
        EventType::ButtonRelease(button) => {
            match button {
                Button::Right => {
                    button_right_act = false;
                },
                _ => {}
            }
        },
        EventType::MouseMove { x, y } => {
            if button_right_act {
               if x > 100.0 {
                  is_block = true;
               } 
            }
        },
        _ => {}
    }
    if is_block {
        None
    } else {
        Some(event)
    }
}).unwrap();

yaojiu19 avatar Jan 12 '23 09:01 yaojiu19

What is actually happening ?

Narsil avatar Jan 13 '23 15:01 Narsil

I'm a beginner of rust. I want move of ownership to closure, but Fn applies to closures that don’t move captured values out of their body and that don’t mutate captured values. image T: Fn(Event) -> Option<Event> + 'static modify to T: FnMut(Event) -> Option<Event> + 'static

yaojiu19 avatar Jan 30 '23 05:01 yaojiu19

I want move of ownership to closure, but Fn applies to closures that don’t move captured values out of their body and that don’t mutate captured values.

That's because you cannot. You could use a global variable for that (that's unsafe but it works if your program is simple enough). Or an Arc<Mutex<T>>

Narsil avatar Jan 30 '23 10:01 Narsil