Runtime managed mutable borrowing from a vec.
This library provides a vec that allows mutable borrows to different elements at the same time. For a map implementation of this, see [rt_map
].
Add the following to Cargo.toml
toml
rt_vec = "0.1.1" # or
rt_vec = { version = "0.1.1", features = ["unsafe_debug"] }
In code:
```rust use rt_vec::RtVec;
struct A(u32);
let mut rt_vec = RtVec::new();
rtvec.push(A(1)); rtvec.push(A(2));
// We can validly have two mutable borrows from the RtVec
map!
let mut a = rtvec.borrowmut(0);
let mut b = rtvec.borrowmut(1);
a.0 = 2;
b.0 = 3;
// We need to explicitly drop the A and B borrows, because they are runtime // managed borrows, and rustc doesn't know to drop them before the immutable // borrows after this. drop(a); drop(b);
// Multiple immutable borrows to the same value are valid. let a0 = rtvec.borrow(0); let a1 = rtvec.borrow(0); let b = rtvec.borrow(1);
println!("A: {}", a_0.0); println!("B: {}", b.0);
// Trying to mutably borrow a value that is already borrowed (immutably
// or mutably) returns Err
.
let atryborrowmut = rtvec.tryborrowmut(0);
let exists = if atryborrowmut.isok() {
"Ok(..)"
} else {
"Err"
};
println!("atryborrow_mut: {}", exists); // prints "Err"
```
"unsafe_debug"
Enables the ["unsafe_debug"
] feature of [rt_ref
].
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.