pub fn three_cells_borrow_mut<'c1, 'c2, 'c3, 'cx, 'r1, 'r2, 'r3, T1, T2, T3>(
cell1: &'c1 JSCell<T1>,
cell2: &'c2 JSCell<T2>,
cell3: &'c3 JSCell<T3>,
_exclusive: &'cx mut NoGC,
) -> (&'r1 mut T1, &'r2 mut T2, &'r3 mut T3)where
'c1: 'r1,
'c2: 'r2,
'c3: 'r3,
'cx: 'r1 + 'r2 + 'r3,Expand description
Mutably borrows three different JSCells at the same time.
This is impossible to do with normal JSCell::borrow_mut,
because it would require three mutable borrows of the same NoGC token at the same time:
ⓘ
use mozjs::context::NoGC;
use mozjs::cell::JSCell;
fn f(no_gc: &mut NoGC, cell1: &JSCell<String>, cell2: &JSCell<String>, cell3: &JSCell<String>) {
let borrow1 = cell1.borrow_mut(no_gc);
let borrow2 = cell2.borrow_mut(no_gc);
let borrow3 = cell3.borrow_mut(no_gc); // cannot borrow `no_gc` as mutable more than once at a time
std::mem::swap(borrow1, borrow2);
std::mem::swap(borrow2, borrow3);
}instead one should do it like this:
use mozjs::context::NoGC;
use mozjs::cell::JSCell;
fn f(no_gc: &mut NoGC, cell1: &JSCell<String>, cell2: &JSCell<String>, cell3: &JSCell<String>) {
let (borrow1, borrow2, borrow3) = mozjs::cell::three_cells_borrow_mut(cell1, cell2, cell3, no_gc);
std::mem::swap(borrow1, borrow2);
std::mem::swap(borrow2, borrow3);
}§Panics
Panics if any two of cell1, cell2 and cell3 are the same cell.