3 ms·
You have lots of options for non-borrowing references to instances of T: Indicies, Arc<T>, RefCell<T>, Cell<T>, dictionary keys, paths to files containing seri
by MaulingMonkey 8y ago
You have lots of options for non-borrowing references to instances of T: Indicies, Arc<T>, RefCell<T>, Cell<T>, dictionary keys, paths to files containing serialized Ts... the list is unbounded. This is all perfectly fine: It's not the goal of the borrow checker to prevent simultaneous references to data, that'd be ridiculous and useless. The goal is to prevent unsafe simultaneous access of data.
And accessing data through an index still requires you to first acquire e.g. a borrow, and the useful invariants of that borrow still hold: for the duration of that borrow, you have freedom from data races - there will be no other borrows to that instance when you're holding a &mut T, or there will be no other mutable borrows to that instance when you're holding a &T.
An index may allow you to reduce the scope and lifetimes of your borrows, but it does not allow you to eliminate your borrows.
- malkia 8y agoa nice side effect - index can now be "compressed" - e.g. just 1 bytes, 2, 4 or more. Such approach comes useful in GC languages too, as you limit the work a GC has to do, going through the roots, checking anything that can be a pointer.
- beagle3 8y agoIndeed. If you take that to the logical conclusion, you reach the K/J/APL mindset - objects are not a useful abstraction. Just use arrays with correlates indexes and be done with it.