rt_vec 0.1.1

Runtime managed mutable borrowing from a vec.
Documentation
  • Coverage
  • 66.67%
    2 out of 3 items documented1 out of 1 items with examples
  • Size
  • Source code size: 40.41 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 5.49 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • azriel91/rt_vec
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • azriel91

🗃️ rt_vec

Crates.io docs.rs CI Coverage Status

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.

Usage

Add the following to Cargo.toml

rt_vec = "0.1.1" # or
rt_vec = { version = "0.1.1", features = ["unsafe_debug"] }

In code:

use rt_vec::RtVec;

struct A(u32);

let mut rt_vec = RtVec::new();

rt_vec.push(A(1));
rt_vec.push(A(2));

// We can validly have two mutable borrows from the `RtVec` map!
let mut a = rt_vec.borrow_mut(0);
let mut b = rt_vec.borrow_mut(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 a_0 = rt_vec.borrow(0);
let _a_1 = rt_vec.borrow(0);
let b = rt_vec.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 a_try_borrow_mut = rt_vec.try_borrow_mut(0);
let exists = if a_try_borrow_mut.is_ok() {
    "Ok(..)"
} else {
    "Err"
};
println!("a_try_borrow_mut: {}", exists); // prints "Err"

Features

"unsafe_debug"

Enables the "unsafe_debug" feature of rt_ref.

License

Licensed under either of

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.