Multiple mutable references compile error

Hi, I met the "multiple mutable references" compile error in this PR: perf(cursor): reduce lock/unlock calls during cursor movement by linrongbin16 · Pull Request #383 · rsvim/rsvim · GitHub.

There are two parts:

  1. The code block: perf(cursor): reduce lock/unlock calls during cursor movement by linrongbin16 · Pull Request #383 · rsvim/rsvim · GitHub.
  2. The _raw_cursor_move method (please use "split" diff-view for better readibility): perf(cursor): reduce lock/unlock calls during cursor movement by linrongbin16 · Pull Request #383 · rsvim/rsvim · GitHub.

The two code are exactly the same, I want to extract the code blocks into a single method, to avoid too many lines code logic, and also improves the unit test coverage.

The variable tree is compiling correct with 1st code block. While in the 2nd method, it use unsafe and std::ptr::NonNull to bypass the safe checker, but I want to avoid the unsafe code.

Is there a way to write this method in a safe way?

can you reduce the code a bit, or describe briefly what the code does? it's hard to understand a small snippet from a large chunk of unfamilar code base.

generally, the common safe workaround for "multiple mutable reference" I can think of may be:

  • take() out, use, and then replace() back: needs a placeholder value, e.g. Default or Option

  • split borrow: e.g. slice::split_at_mut(), or the partial-borrow crate

  • polonius-the-crab: for certain known scenarios, see the crate's doc for details.

1 Like

Yes, I think again and find out I just need to introduce more refcell/mutex pointers to make it correct.