rt_ref/
lib.rs

1//! `Ref` types with internal mutability that implement `Send` and `Sync`.
2//!
3//! These types are shared by [`rt_map`] and [`rt_vec`].
4//!
5//!
6//! ## Usage
7//!
8//! Add the following to `Cargo.toml`:
9//!
10//! ```toml
11//! rt_ref = "0.3.0" # or
12//! rt_ref = { version = "0.3.0", features = ["unsafe_debug"] }
13//! ```
14//!
15//! In code:
16//!
17//! ```rust
18//! use rt_ref::{Cell, Ref, RefMut};
19//!
20//! let a = 1;
21//!
22//! // Insert a value into a collection, wrapped with `Cell`.
23//! let mut v = Vec::new();
24//! v.push(Cell::new(a));
25//!
26//! let v = v; // v is now compile-time immutable.
27//! let a = v.get(0).map(|cell| RefMut::new(cell.borrow_mut()));
28//! a.map(|mut a| {
29//!     *a += 2;
30//! });
31//!
32//! let a = v.get(0).map(|cell| Ref::new(cell.borrow()));
33//! assert_eq!(Some(3), a.map(|a| *a));
34//! ```
35//!
36//!
37//! ### Features
38//!
39//! #### `"unsafe_debug"`:
40//!
41//! The borrowed reference will use the inner type's `Debug` implementation when
42//! formatted.
43//!
44//! ```rust
45//! use rt_ref::{Cell, Ref, RefMut};
46//!
47//! let mut v = Vec::new();
48//! v.push(Cell::new("a"));
49//!
50//! #[cfg(not(feature = "unsafe_debug"))]
51//! assert_eq!(
52//!     r#"[Cell { flag: 0, inner: UnsafeCell { .. } }]"#,
53//!     format!("{v:?}")
54//! );
55//! #[cfg(feature = "unsafe_debug")]
56//! assert_eq!(r#"[Cell { flag: 0, inner: "a" }]"#, format!("{v:?}"));
57//! ```
58//!
59//!
60//! [`rt_map`]: https://crates.io/crates/rt_map
61//! [`rt_vec`]: https://crates.io/crates/rt_vec
62
63pub use crate::{
64    borrow_fail::BorrowFail, cell::Cell, cell_ref::CellRef, cell_ref_mut::CellRefMut, r#ref::Ref,
65    ref_mut::RefMut, ref_overflow::RefOverflow,
66};
67
68mod borrow_fail;
69mod cell;
70mod cell_ref;
71mod cell_ref_mut;
72mod r#ref;
73mod ref_mut;
74mod ref_overflow;