#automation #ref #rt-ref

rt_ref

Internal Ref types for rt_ref and rt_vec

7 releases

Uses new Rust 2024

0.3.0 Mar 16, 2025
0.2.1 Jan 28, 2024
0.2.0 Jul 15, 2022
0.1.3 Jul 14, 2022
0.1.2 Jun 30, 2022

#225 in Data structures

Download history 38/week @ 2024-12-15 15/week @ 2024-12-22 11/week @ 2024-12-29 84/week @ 2025-01-05 154/week @ 2025-01-12 94/week @ 2025-01-19 33/week @ 2025-01-26 40/week @ 2025-02-02 22/week @ 2025-02-09 86/week @ 2025-02-16 34/week @ 2025-02-23 23/week @ 2025-03-02 25/week @ 2025-03-09 249/week @ 2025-03-16 83/week @ 2025-03-23 137/week @ 2025-03-30

495 downloads per month
Used in 39 crates (2 directly)

MIT/Apache

39KB
880 lines

♐ rt_ref

Crates.io docs.rs CI Coverage Status

Ref types with internal mutability that implement Send and Sync.

These types are shared by rt_map and rt_vec.

Usage

Add the following to Cargo.toml:

rt_ref = "0.3.0" # or
rt_ref = { version = "0.3.0", features = ["unsafe_debug"] }

In code:

use rt_ref::{Cell, Ref, RefMut};

let a = 1;

// Insert a value into a collection, wrapped with `Cell`.
let mut v = Vec::new();
v.push(Cell::new(a));

let v = v; // v is now compile-time immutable.
let a = v.get(0).map(|cell| RefMut::new(cell.borrow_mut()));
a.map(|mut a| {
    *a += 2;
});

let a = v.get(0).map(|cell| Ref::new(cell.borrow()));
assert_eq!(Some(3), a.map(|a| *a));

Features

"unsafe_debug":

The borrowed reference will use the inner type's Debug implementation when formatted.

use rt_ref::{Cell, Ref, RefMut};

let mut v = Vec::new();
v.push(Cell::new("a"));

#[cfg(not(feature = "unsafe_debug"))]
assert_eq!(
    r#"[Cell { flag: 0, inner: UnsafeCell { .. } }]"#,
    format!("{v:?}")
);
#[cfg(feature = "unsafe_debug")]
assert_eq!(r#"[Cell { flag: 0, inner: "a" }]"#, format!("{v:?}"));

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.

No runtime deps

Features