rodrigocfd / winsafe

Windows API and GUI in safe, idiomatic Rust.
https://crates.io/crates/winsafe
MIT License
523 stars 30 forks source link

[Question] Using MutFn as WinApi callback #96

Closed Dimava closed 1 year ago

Dimava commented 1 year ago

I'm trying to do something like

// find a window with title "Untitled"
let mut hw: Option<HWND> = None;
EnumWindows(|hwnd: HWND| -> bool {
    let text = hwnd.GetWindowText().unwrap();
    if text == "" || text == "Default IME" || text == "MSCTFIME UI" {
        return true;
    }
    if text == "Untitled" {
        let h = unsafe { hwnd.raw_copy() };
        hw = Some(h)
    }
    println!("HWND: {}, {}", hwnd, text);
    true
})
.unwrap();

which errors with cannot assign to `hw`, as it is a captured variable in a `Fn` closure (I did patch winsafe for that to work)

Is it safe to use FnMut as a callback here? If yes, can EnumWindows be defined with FnMut callback? If it can, should it be?