This crate provides MultiIndexable
, through which its implementors allow us to violate basic borrowing-rules when indexing them (using Index[Mut]
) , as long as we adhere to them for each individual element.
In other words: We can index a collection mutably and immutable multiple times at once, as long as there are no read/write clashes.
NOTE: MultiIndexable
is currently not implemented for libstd's HashMap
and BTreeMap
, due to them missing IndexMut
-implementations. This will change when IndexAssign
-functionality lands.
This crate requires the latest rust nightly to compile.
```rust
extern crate qcollect; extern crate qindexmulti; extern crate vecmap;
use vecmap::VecMap; use qindexmulti::{MultiIndexable, MultiIndex};
fn test1(){ let mut data = VecMap::new(); data.insert(0, 100u16); data.insert(2, 200); data.insert(20, 300); data.insert(200, 400);
let read_indicies = vec![0, 2, 20];
let write_indicies = vec![200];
let multi_idx = MultiIndex::new(read_indicies, write_indicies);
{
let mut output = data.index_multi(&multi_idx);
let [a, b, c]: [_; 3] = qcollect::iter_into_fixed(output.read);
let d = output.write.next().unwrap();
*d += *a + *b + c;
}
assert_eq!(data[200], 1000);
} ```