#vec #cell #reference #mutable #interior-mutability #elements #disjoint

vec_cell

A Vec with interior mutability and dynamically checked borrow rules which allows to take disjoint mutable references to its elements

4 releases

0.1.3 Apr 26, 2023
0.1.2 Mar 31, 2023
0.1.1 Mar 27, 2023
0.1.0 Mar 24, 2023

#405 in Memory management

Download history 8/week @ 2024-02-25 22/week @ 2024-03-31 1/week @ 2024-04-07 48/week @ 2024-04-14

71 downloads per month
Used in 2 crates (via arena_system)

MPL-2.0 license

43KB
730 lines

vec_cell

Rust Vec with interior mutability which allows to take disjoint mutable references to its elements.

use vec_cell::VecCell;

// Create `VecCell`.
let vec_cell: VecCell<i32> = VecCell::new();

// Push elements to `VecCell`.
vec_cell.push(0);
vec_cell.push(1);
vec_cell.push(2);

// Take immutable borrows to `VecCell` elements.
{
    assert_eq!(*vec_cell.borrow(0), 0);
    assert_eq!(*vec_cell.borrow(1), 1);
    assert_eq!(*vec_cell.borrow(2), 2);
}

// Take disjoint mutable borrows to `VecCell` elements.
{
    let borrow_mut1 = &mut *vec_cell.borrow_mut(1);
    let borrow_mut2 = &mut *vec_cell.borrow_mut(2);

    *borrow_mut1 = 10;
    *borrow_mut2 = 15;
}

// Pop elements from `VecCell`.
assert_eq!(vec_cell.pop(), 15);
assert_eq!(vec_cell.pop(), 10);
assert_eq!(vec_cell.pop(), 0);

lib.rs:

VecCell is a Vec with interior mutability and dynamically checked borrow rules. VecCell allows to take disjoint mutable borrows to its elements.

Example

use vec_cell::VecCell;

let mut vec_cell: VecCell<i32> = VecCell::new();

vec_cell.push(0);
vec_cell.push(1);
vec_cell.push(2);

{
    assert_eq!(*vec_cell.borrow(0), 0);
    assert_eq!(*vec_cell.borrow(1), 1);
    assert_eq!(*vec_cell.borrow(2), 2);
}

{
    let borrow_mut1 = &mut *vec_cell.borrow_mut(1);
    let borrow_mut2 = &mut *vec_cell.borrow_mut(2);

    *borrow_mut1 = 10;
    *borrow_mut2 = 15;
}

assert_eq!(vec_cell.pop(), Some(15));
assert_eq!(vec_cell.pop(), Some(10));
assert_eq!(vec_cell.pop(), Some(0));

Dependencies

~0.4–0.8MB
~20K SLoC