lineage 0.4.0

A cell to replace the contained value while it may still be borrowed.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 15.02 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.59 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • markusolt/lineage
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • markusolt

Lineage

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

Safety

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