🗃️ rt_map
Runtime managed mutable borrowing from a map or vec.
This library provides a map and vec that allows mutable borrows to different entries at the same time.
The map implementation is extracted and slightly modified from shred.
Usage
Add the following to Cargo.toml
= "0.5.1"
= { = "0.5.1", = ["rt_vec"] } # to enable `RtVec`
RtMap
use RtMap;
;
RtVec
use RtVec;
;
let mut rt_vec = new;
rt_vec.push;
rt_vec.push;
// We can validly have two mutable borrows from the `RtVec` map!
let mut a = rt_vec.borrow_mut;
let mut b = rt_vec.borrow_mut;
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;
drop;
// Multiple immutable borrows to the same value are valid.
let a_0 = rt_vec.borrow;
let _a_1 = rt_vec.borrow;
let b = rt_vec.borrow;
println!;
println!;
// Trying to mutably borrow a value that is already borrowed (immutably
// or mutably) returns `Err`.
let a_try_borrow_mut = rt_vec.try_borrow_mut;
let exists = if a_try_borrow_mut.is_ok else ;
println!; // prints "Err"
See Also
anymap: Map of any type, without multiple mutable borrows.resman: Map of any type, with runtime managed borrowing.shred: Likeresman, plus a task dispatcher.
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Contribution
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.