Expand description

Ref types with internal mutability that implement Send and Sync.

These types are shared by rt_map and rt_vec.


Add the following to Cargo.toml:

rt_ref = "0.2.0" # or
rt_ref = { version = "0.2.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();

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));



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

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

let mut v = Vec::new();

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


A custom cell container that is a RefCell with thread-safety.

An immutable reference to data in a Cell.

A mutable reference to data in a Cell.

Reference to a value.

Mutable reference to a value.

Error when trying to clone a Ref, but there are already isize::MAX references.


Failures to borrow a value.