vec_cell

Crates.iovec_cell
lib.rsvec_cell
version0.1.3
sourcesrc
created_at2023-03-24 17:15:00.821258
updated_at2023-04-26 08:33:02.746411
descriptionA Vec with interior mutability and dynamically checked borrow rules which allows to take disjoint mutable references to its elements
homepage
repositoryhttps://github.com/alexanderved/vec_cell
max_upload_size
id819555
size59,195
(alexanderved)

documentation

README

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);
Commit count: 23

cargo fmt