rspace_traits/
key_value.rs

1/*
2    Appellation: key_value <module>
3    Created At: 2025.12.26:17:44:22
4    Contrib: @FL03
5*/
6//! this module defines traits for key-value stores and their entries
7//!
8
9/// [`KeyValueEntry`] establishes a common interface for entries within a key-value store.
10pub trait KeyValueEntry<'a> {
11    type Key;
12    type Value;
13}
14
15/// The [`KeyValueStore`] trait is used to define a key-value store container.
16pub trait KeyValueStore<K, V> {
17    type Entry<'a>: KeyValueEntry<'a, Key = K, Value = V>
18    where
19        Self: 'a;
20}
21
22/*
23 ************* Implementations *************
24*/
25
26#[cfg(feature = "alloc")]
27mod impl_alloc {
28    use super::{KeyValueEntry, KeyValueStore};
29    use alloc::collections::btree_map::{self, BTreeMap};
30
31    impl<'a, K, V> KeyValueEntry<'a> for btree_map::Entry<'a, K, V> {
32        type Key = K;
33        type Value = V;
34    }
35
36    impl<K, V> KeyValueStore<K, V> for BTreeMap<K, V> {
37        type Entry<'a>
38            = btree_map::Entry<'a, K, V>
39        where
40            Self: 'a;
41    }
42}
43
44#[cfg(feature = "hashbrown")]
45mod impl_hashbrown {
46    use super::{KeyValueEntry, KeyValueStore};
47    use hashbrown::hash_map::{self, HashMap};
48
49    impl<'a, K, V, S> KeyValueEntry<'a> for hash_map::Entry<'a, K, V, S> {
50        type Key = K;
51        type Value = V;
52    }
53
54    impl<K, V, S> KeyValueStore<K, V> for HashMap<K, V, S> {
55        type Entry<'a>
56            = hash_map::Entry<'a, K, V, S>
57        where
58            Self: 'a;
59    }
60}
61
62#[cfg(feature = "std")]
63mod impl_std {
64    use super::{KeyValueEntry, KeyValueStore};
65    use std::collections::hash_map::{self, HashMap};
66
67    impl<'a, K, V> KeyValueEntry<'a> for hash_map::Entry<'a, K, V> {
68        type Key = K;
69        type Value = V;
70    }
71
72    impl<K, V> KeyValueStore<K, V> for HashMap<K, V> {
73        type Entry<'a>
74            = hash_map::Entry<'a, K, V>
75        where
76            Self: 'a;
77    }
78}