#key-value #store #data #implemented #pair #multiple #hashing

kvp

Provides types to store multiple fields where equality, ordering, and hashing are only implemented through one of the types

3 stable releases

1.0.2 Jun 5, 2023

#2879 in Database interfaces

MIT license

5KB
79 lines

kvp

This crate is designed to provide a type, KeyValuePair<TKey, TValue>, which can be used when someone wishes to "tag" a value with additional data, while keeping its functional traits (PartialEq, Eq, PartialOrd, Ord, Hash) entirely on the key, disregarding the tagged data. This can be used, for example, to have a BinaryHeap which acts more similarly to a HashMap in its ability to store key-value data.

let heap = BinaryHeap::new();

heap.push(KeyValuePair { key: 50, value: "Hey, here's an associated value" })
// You can also use ::new syntax
heap.push(KeyValuePair::new(0, "another bit of associated data!"));
heap.push(KeyValuePair::new(22, "voila"));

assert_matches!(heap.pop(), Some(50));
assert_matches!(heap.pop(), Some(22));
assert_matches!(heap.pop(), Some(0));

No runtime deps