#data #mutation #cow #ownership

cowvert

a flexible data container supporting owned, reference-counted, and copy-on-write semantics. designed for use in interpreters

2 releases

new 0.1.1 Feb 16, 2025
0.1.0 Feb 15, 2025

#561 in Data structures

Download history 178/week @ 2025-02-11

178 downloads per month

MIT/Apache

15KB
362 lines

A simple library for creating references and copy on write values.

It is designed to be used in interpreters for handling the env.

See the tests to learn more.


Example Usage

Converting Between Value and Reference

use cowvert::Data;

fn main() {
    let mut data = Data::value(100);
    assert!(data.is_val());

    let mut ref_data = data.by_ref();
    assert!(ref_data.is_ref());

    *ref_data.borrow_mut() += 50;
    
    assert_eq!(*data.borrow(), 150); // mutates the original
}

Copy-on-Write (COW)

use cowvert::Data;

fn main() {
    let mut data = Data::value("hello".to_string());

    let mut cow_data = data.by_cow();
    *cow_data.borrow_mut() = "goodbye".to_string();

    assert_eq!(*data.borrow(), "hello"); // original remains unchanged
    assert_eq!(*cow_data.borrow(), "goodbye"); // copy is modified
}

No runtime deps