memcell - A utility crate which provides MemoryCell, a struct that stores a current and previous value, no cloning included
NOTE: This crate is mostly just a utility. I was looking for a crate that does this, and found none. I made it because why not.
Minimal example
use memcell::MemoryCell;
fn main() {
let mut cell = MemoryCell::new(5_u32);
let new_value = 10;
cell.update(new_value);
assert_eq!(cell.current(), &10);
assert_eq!(cell.last(), &Some(5));
}
MemoryCell definition
#[derive(Debug, Clone)]
pub struct MemoryCell<T> {
current: T,
last_val: Option<T>,
}
The update function does not clone any values by taking advantage of Rust's std::mem::replace.
Link to the crate: https://crates.io/crates/memcell\
Repository: https://github.com/ImajinDevon/memcell