#immutability #collection #indexing #once #times #multiple #index

qindex_multi

Provides means of indexing collections mutable and immutable multiple times at once

6 releases (3 breaking)

Uses old Rust 2015

0.4.0 Aug 18, 2015
0.3.0 Aug 16, 2015
0.2.0 Aug 13, 2015
0.1.2 Aug 8, 2015

#69 in #once


Used in 2 crates

CC0 license

8KB
137 lines

qindex_multi

API Documentation

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.

TODO

  • Doc, Tests, Examples

Example Usage

#![feature(slice_patterns)]

extern crate qcollect;
extern crate qindex_multi;
extern crate vec_map;

use vec_map::VecMap;
use qindex_multi::{MultiIndexable, MultiIndex};

#[test]
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);
}

Dependencies

~91KB