Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

captured variable in a Fn closure cannot assign #95

Open
yaojiu19 opened this issue Jan 12, 2023 · 3 comments
Open

captured variable in a Fn closure cannot assign #95

yaojiu19 opened this issue Jan 12, 2023 · 3 comments

Comments

@yaojiu19
Copy link

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();
@Narsil
Copy link
Owner

Narsil commented Jan 13, 2023

What is actually happening ?

@yaojiu19
Copy link
Author

yaojiu19 commented Jan 30, 2023

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 + 'static
modify to
T: FnMut(Event) -> Option + 'static

@Narsil
Copy link
Owner

Narsil commented Jan 30, 2023

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>>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants