https://github.com/purpleprotocol/hashcow
A Rust HashMap implementation with copy-on-write keys and values
https://github.com/purpleprotocol/hashcow
copy-on-write data-structure rust
Last synced: about 1 month ago
JSON representation
A Rust HashMap implementation with copy-on-write keys and values
- Host: GitHub
- URL: https://github.com/purpleprotocol/hashcow
- Owner: purpleprotocol
- License: mit
- Created: 2019-07-08T15:32:41.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-09-03T17:40:41.000Z (almost 6 years ago)
- Last Synced: 2025-04-18T00:19:54.773Z (2 months ago)
- Topics: copy-on-write, data-structure, rust
- Language: Rust
- Homepage:
- Size: 10.7 KB
- Stars: 32
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# HashCow
[![Build Status]][travis] [![Discord Badge]][Discord] [![Latest Version]][crates.io] [![Documentation]][docs.rs]HashCow is a Rust HashMap implementation with copy-on-write keys and values.
---
Originally built for optimizing the [Purple Protocol](https://github.com/purpleprotocol/purple), 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
```rust
use hashcow::{Form, CowHashMap};let mut hm: CowHashMap = 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][issues] of the repository before starting out.### License
HashCow is licensed under the MIT license.
[Build Status]: https://travis-ci.org/purpleprotocol/hashcow.svg?branch=master
[Discord Badge]: https://img.shields.io/discord/435827644915777536.svg
[Discord]: https://discord.gg/eGBzyaA
[travis]: https://travis-ci.org/purpleprotocol/hashcow
[crates.io]: https://crates.io/crates/hashcow
[Latest Version]: https://img.shields.io/crates/v/hashcow.svg
[Documentation]: https://docs.rs/hashcow/badge.svg
[docs.rs]: https://docs.rs/hashcow
[issues]: https://github.com/purpleprotocol/hashcow/issues