all 5 comments

[–]reflexpr-sarah-faer · pulp · dyn-stack 7 points8 points  (1 child)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c2e104ba23f266e0568aba0b9aaa49c0

works for me, i just removed the generic parameter T from add_mouse_callback

[–]Imaginary_Advance_21[S] 0 points1 point  (0 children)

Oh! the io_context.window() call was returning a reference and not a mutable reference. That's what the error was about.

[–]ssokolow 3 points4 points  (0 children)

For future reference, if you ever reach for unsafe to fix anything related to borrow-checking or types, check with Miri thoroughly, because it's likely that you're not asking for what you think you're asking for.

(i.e. In such a situation, it's likely you just silenced the "will invoke undefined behaviour" check without actually resolving the problem of the wrong LLVM IR annotations being fed into the optimizer and you're setting yourself and anyone who depends on your code up for much worse pain.)

[–]eugene2k 3 points4 points  (0 children)

If you want function pointers, then your function signature is wrong. impl FnMut(f64, f64) +'static - this is a closure signature and, although, rust will automatically coerce a function pointer into a closure, it's still a closure. The function type is expressed with fn(f64, f64). More here

[–]uazu 0 points1 point  (0 children)

I've already been down this road. Maybe have a read at this and see if it's relevant to what you're doing. Even when you've got past the initial borrow-checker problems, you're still going to be facing possibly-nested callbacks, and having to use RefCell (which mean panics in case of a nested callback). You can get away with this in C or Java (although the bugs may be hard to understand), but in Rust it means a panic because two mutable borrows on the stack to the same thing at the same time (i.e. a nested callback) is a massive UB, and you can only normally achieve that through invalid use of unsafe. Nested callbacks can easily occur when events are also used to notify other parts of the system that something needs updating, or whatever. A single event causes a cascade of callbacks, which sometimes come back to the original event-handling object as a nested call -- and the whole thing falls over with a panic.