pub fn two_cells_borrow_mut<'c1, 'c2, 'cx, 'r1, 'r2, T1, T2>(
cell1: &'c1 JSCell<T1>,
cell2: &'c2 JSCell<T2>,
_exclusive: &'cx mut NoGC,
) -> (&'r1 mut T1, &'r2 mut T2)where
'c1: 'r1,
'c2: 'r2,
'cx: 'r1 + 'r2,Expand description
Mutably borrows two different JSCells at the same time.
This is impossible to do with normal JSCell::borrow_mut,
because it would require two 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>) {
let borrow1 = cell1.borrow_mut(no_gc);
let borrow2 = cell2.borrow_mut(no_gc); // cannot borrow `no_gc` as mutable more than once at a time
std::mem::swap(borrow1, borrow2);
}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>) {
let (borrow1, borrow2) = mozjs::cell::two_cells_borrow_mut(cell1, cell2, no_gc);
std::mem::swap(borrow1, borrow2);
}§Panics
Panics if cell1 and cell2 are the same cell.