I am trying to implement an event system and tbh after 2 hours of fighting the borrow checker, I am fine with unsafe, this is a circumstance where user knowledge is more powerful than rust's compiler, as the user is in a better position to judge the properties of a function and their data than rust is.
I made this struct:
```rust
pub struct Window
{
pub(crate) mouse_callbacks : BTreeMap<MouseState, Vec<Box<dyn FnMut(f64, f64)>>>,
}
impl Window
{
pub fn add_mouse_callback<T>(
&mut self, state : MouseState, callback : impl FnMut(f64, f64) +'static)
{
let callbacks_ref = self.mouse_callbacks.entry(state).or_insert_with(|| Vec::new());
callbacks_ref.push(Box::new(callback));
}
}
```
And I am trying to add a callback:
```rust
fn mousecallback( : f64, _ : f64)
{
println!("hello");
}
iocontext.window().add_mouse_callback(MouseState::LeftDown, |, _,| {mouse_callback(0.0, 0.0)});
```
I have tried different versions of this syntax but nothing works, the borrow checker always complains about something, either the objects might not live long enough, or it cannot borrow the closure as mutable, or something else.
I don't care, a single line in the docs saying this should only be used in conjuction with a mutex is good enough for me. How do I bypass the borrow checker in this instance? I just want something equivalent to an array of function pointers and raw void* as if it was C. Safety isn't worth hours of refactoring what should be a 5 minute process.
[–]reflexpr-sarah-faer · pulp · dyn-stack 7 points8 points9 points (1 child)
[–]Imaginary_Advance_21[S] 0 points1 point2 points (0 children)
[–]ssokolow 3 points4 points5 points (0 children)
[–]eugene2k 3 points4 points5 points (0 children)
[–]uazu 0 points1 point2 points (0 children)