0

I have the following definitions:

pub trait PlayerActionFnT: FnMut(&mut State) -> RunState + Send + Sync + 'static {}

impl<F> PlayerActionFnT for F where F: FnMut(&mut State) -> RunState + Send + Sync + 'static {}

pub type PlayerActionFn = Arc<dyn PlayerActionFnT>;

When I try to use this on some mutable global state (gs), like (action_and_id.action)(gs)

error[E0596]: cannot borrow data in an `Arc` as mutable
   --> src/player.rs:294:36
    |
294 |             Some(action_and_id) => (action_and_id.action)(gs),
    |                                    ^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
    |
    = help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Arc<dyn PlayerActionFnT<for<'a> Output = RunState>>`

This is a bit confusing to me, since I'm not trying to mutate what is stored in the Arc (a function), but rather trying to use it to mutate something I already have mutable access to (gs: &mut State).

bbarker
  • 11,636
  • 9
  • 38
  • 62
  • 3
    Try using `Fn(&mut State) -> ...` instead, since you don't need to modify the closure. See [this answer](https://stackoverflow.com/questions/30177395#30232500) for more info – Filipe Rodrigues May 08 '23 at 13:07
  • Ah I see, I misunderstood what was meant by the "mut" in FnMut – bbarker May 08 '23 at 13:53

0 Answers0