Crates.io | many-to-many |
lib.rs | many-to-many |
version | 0.1.7 |
source | src |
created_at | 2020-08-28 20:58:02.016669 |
updated_at | 2020-12-02 20:29:27.388312 |
description | Rust crate for creating many-to-many data structures with the ability to query either side, useful for applications such as pubsub. Like a fusion between `bimap` and `multimap`. |
homepage | |
repository | https://gitlab.insrt.uk/insert/many-to-many |
max_upload_size | |
id | 282072 |
size | 14,935 |
This is a very simple crate which you can use for creating many-to-many data structures in Rust, it's intended purpose or use case is for situations where you need to map a set of ids to another set of ids (for example in PubSub, such as hive_pubsub
which is what this crate was designed for). It does this by using two HashMap
s, one linking Left
to a set of Right
and vice-versa.
This crate is like a fusion of bimap
and multimap
. I didn't see anything like this on crates.io, or anywhere really so I made my own thing.
Keys on either side (left or right) must implement Hash, Eq and Clone. See documentation for more information.
use many_to_many::ManyToMany;
let mut map = ManyToMany::new();
map.insert(1, 2);
map.insert(1, 3);
map.insert(1, 4);
assert_eq_sorted(map.get_left(&1), vec![ 2, 3, 4 ]);
map.insert(5, 2);
map.remove(&1, &4);
assert_eq_sorted(map.get_left(&1), vec![ 2, 3 ]);
assert_eq_sorted(map.get_right(&2), vec![ 1, 5 ]);
map.remove(&1, &2);
map.remove(&1, &3);
assert_eq!(map.get_left(&1), None);
map.insert(11, 10);
map.insert(12, 10);
map.insert(13, 10);
map.remove_right(&10);
assert_eq!(map.get_left(&11), None);
assert_eq!(map.get_right(&10), None);
/// This is a helper function to unwrap the option,
/// sort the array and compare it with a sorted array.
fn assert_eq_sorted(a: Option<Vec<i32>>, b: Vec<i32>) {
assert!(a.is_some());
let mut list = a.unwrap();
list.sort();
assert_eq!(list, b);
}
This crate has optional Serde support. To enable it, specify the serde
feature in Cargo.toml
. For example:
[dependencies]
many-to-many = { version = "^0.1.7", features = ["serde"] }