Crates.io | lineage |
lib.rs | lineage |
version | 0.4.0 |
source | src |
created_at | 2023-09-30 12:17:48.334526 |
updated_at | 2023-09-30 12:17:48.334526 |
description | A cell to replace the contained value while it may still be borrowed. |
homepage | |
repository | https://github.com/markusolt/lineage |
max_upload_size | |
id | 988604 |
size | 15,339 |
A small rust crate that provides a type of cell that can replace its contained value while the previous value is still immutably borrowed:
impl Lineage {
pub fn new(value: T) -> Self;
pub fn get(&self) -> &T;
pub fn set(&self, value: T);
pub fn clear(&mut self);
}
Notice how a new value can be inserted into the cell with Lineage::set
using only &self
.
This means the Lineage
may still be borrowed by previous calls to Lineage::get
but you can replace the contained value anyways.
Internally the replaced value is added to a linked list which is not cleared until you call Lineage::clear
or drop the Lineage
.
let lineage: Lineage<String> = Lineage::new(String::from("ONE"));
let s1 = lineage.get();
lineage.set(String::from("TWO"));
let s2 = lineage.get();
assert_eq!(s1, "ONE");
assert_eq!(s2, "TWO");
As is expected for this kind of utility crate, the implementation makes use of unsafe
.
We have a number of tests to look for undefined behavior which can all be run natively or with miri.
miri
is a fantastic tool to execute rust applications in a virtual runtime that is sensitive to various kinds of undefined behavior.
# run tests normally
cargo test
# install miri
rustup toolchain install nightly
rustup +nightly component add miri
# run tests in miri
cargo clean
cargo +nightly miri test