#(FnMut, FnMut)

1 messages · Page 1 of 1 (latest)

round sigil
#

Is there any way to express to the borrow checker that a pair of FnMut closures will never be called at the same time, allowing both to mutably capture by reference the same value(s)?
I tried attaching the same lifetime to both of them (+ 'a) but that didn't work.

distant plover
#

No, it is not possible. However:

  1. I had exactly the same problem and just yesterday realized that I can combine them into one closure with another argument telling it which of the two things to do (in my case, a boolean, but an enum would also work and allow it to have different fields for the two cases).

  2. Before then, I was using a RefCell wrapped around the mutable reference they both wanted to use.

  3. If they have very different signatures then you can make both functions be methods in a trait, instead; this means you have to replace the closure with a struct + impl, though.

#

Of these, only option 2 is possible if you don't control the code calling the functions.