Crates.io | cell-ref |
lib.rs | cell-ref |
version | 0.1.1 |
source | src |
created_at | 2022-04-27 01:29:52.523823 |
updated_at | 2022-05-18 03:08:44.155672 |
description | Cell type with methods for by-reference mutation |
homepage | |
repository | https://github.com/taylordotfish/cell-ref |
max_upload_size | |
id | 575782 |
size | 24,017 |
This crate provides a Cell
type (like the standard library’s
Cell
) with methods for safely mutating and inspecting the
inner value by reference (with
and with_mut
).
For Copy
types, this is implemented with get
and
set
, but through an extension trait, this crate
provides those same operations for types that are Default
but not
Copy
. A get
method is also available for types that are both
Default
and Clone
.
This crate depends only on core
, so it can be used inside no_std
environments.
use cell_ref::{Cell, CellExt};
let c1 = Cell::new(2_u8);
c1.with_mut(|x| *x += 3);
assert!(c1.get() == 5);
let c2 = Cell::new(vec![1, 2, 3]);
c2.with_mut(|v| v.push(4)); // Works even though `Vec` isn't `Copy`
assert_eq!(c2.with(Vec::len), 4);
let v = c2.get(); // Clones the vector