lazy-cow 0.1.0

Copy-on-write pointers with lazy modification support to minimise clones with a cost counter to limit work duplication.
Documentation
use lazy_cow::{LazyCow, LazyView, LazyViewMut};

fn main() {
    let mut xs = LazyCow::<Vec<u8>>::new(vec![4, 3, 2, 1, 0]);
    // not shared, so no need modify lazily
    match xs.get_mut() {
        LazyViewMut::Strict(xs_mut) => xs_mut.reverse(),
        LazyViewMut::Lazy(..) => unreachable!(),
    }
    let mut ys = xs.clone();
    // shared, modifications must be lazy
    match xs.get_mut() {
        LazyViewMut::Strict(..) => unreachable!(),
        LazyViewMut::Lazy(_xs, xs_diff) => xs_diff.append(&mut vec![5, 6, 7]),
    }
    // still considered strict when getting immutably
    match ys.get() {
        LazyView::Strict(ys) => assert_eq!(ys.iter().sum::<u8>(), 10),
        LazyView::Lazy(..) => unreachable!(),
    }
    // copy lazy work
    let mut zs = xs.clone();
    assert_eq!(zs.eval(), vec![0, 1, 2, 3, 4, 5, 6, 7]);
    let mut n = 0;
    // now strict and unique
    for z in zs.get_strict().iter_mut() {
        n += *z;
        *z = n;
    }
    assert_eq!(zs.into_inner(), vec![0, 1, 3, 6, 10, 15, 21, 28]);
    let mut ws = xs.clone();
    // exceeded difference cloning budget, forced to clone whole vec
    match ws.get_mut() {
        LazyViewMut::Strict(ws) => ws.reverse(),
        LazyViewMut::Lazy(..) => unreachable!(),
    }
    drop(xs);
    // last pointer to original vec now gone, can be aquired mutably
    match ys.get_mut() {
        LazyViewMut::Strict(ys) => ys.iter_mut().for_each(|y| *y += 1),
        LazyViewMut::Lazy(..) => unreachable!(),
    }
    println!("moo~")
}