hashcow

Crates.iohashcow
lib.rshashcow
version0.2.0
sourcesrc
created_at2019-07-08 15:56:22.364262
updated_at2019-07-09 11:08:00.735611
descriptionA Rust HashMap implementation with copy-on-write keys and values
homepage
repositoryhttps://github.com/purpleprotocol/hashcow
max_upload_size
id147591
size19,727
Octavian (octavonce)

documentation

README

HashCow

Build Status Discord Badge Latest Version Documentation

HashCow is a Rust HashMap implementation with copy-on-write keys and values.


Originally built for optimizing the Purple Protocol, this library provides a way to link HashMaps in memory that have duplicate entries. Instead of the duplicate data, it is instead borrowed and it is only cloned when mutation is needed.

Using HashCow

use hashcow::{Form, CowHashMap};

let mut hm: CowHashMap<str, [u8]> = CowHashMap::new();

// We insert an owned value in the map
hm.insert_owned("key".to_owned(), vec![1, 2, 3]);
assert_eq!(hm.entry_form(&"key").unwrap(), Form::Owned);

// We now create a clone with borrowed fields
let mut hm_clone = hm.borrow_fields();
assert_eq!(hm_clone.entry_form(&"key").unwrap(), Form::Borrowed);

// On mutation, the borrowed entry is cloned
let entry = hm_clone.get_mut(&"key").unwrap();

// We now mutate the cloned value
*entry = vec![4, 5, 6];
assert_eq!(hm_clone.entry_form(&"key").unwrap(), Form::Owned);

// The two maps now have different entries for the same key
assert_eq!(hm.get(&"key").unwrap(), &[1, 2, 3]);
assert_eq!(hm_clone.get(&"key").unwrap(), &[4, 5, 6]);

Contributing

We welcome anyone wishing to contribute to HashCow! Check out the issues section of the repository before starting out.

License

HashCow is licensed under the MIT license.

Commit count: 14

cargo fmt