osom_lib_hash_tables/abseil/hash_table/
abseil_immutable.rs1use core::borrow::Borrow;
2use core::hash::Hash;
3
4use osom_lib_primitives::kvp::KVP;
5use osom_lib_primitives::length::Length;
6
7use crate::abseil::hash_table::abseil_unsafe_iter::AbseilUnsafeIter;
8use crate::helpers::ptr_to_ref;
9use crate::traits::ImmutableHashTable;
10
11use crate::abseil::{configuration::AbseilConfig, hash_table::AbseilHashTable};
12
13impl<TKey, TValue, TConfig> ImmutableHashTable<TKey, TValue> for AbseilHashTable<TKey, TValue, TConfig>
14where
15 TKey: Eq + Hash,
16 TConfig: AbseilConfig,
17{
18 #[inline(always)]
19 fn length(&self) -> Length {
20 self.length()
21 }
22
23 #[inline(always)]
24 fn contains<Q>(&self, key: &Q) -> bool
25 where
26 TKey: Borrow<Q>,
27 Q: Eq + Hash + ?Sized,
28 {
29 self.get_key_value(key).is_some()
30 }
31
32 #[inline(always)]
33 fn get<Q>(&self, key: &Q) -> Option<&TValue>
34 where
35 TKey: Borrow<Q>,
36 Q: Eq + Hash + ?Sized,
37 {
38 match self.get_key_value(key) {
39 Some(kvp) => Some(kvp.value),
40 None => None,
41 }
42 }
43
44 fn get_key_value<Q>(&self, key: &Q) -> Option<KVP<&TKey, &TValue>>
45 where
46 TKey: Borrow<Q>,
47 Q: Eq + Hash + ?Sized,
48 {
49 let result = self.get_key_value_raw(key)?;
50 unsafe { Some(result.as_ref_unchecked().as_ref_kvp()) }
51 }
52
53 #[inline(always)]
54 fn iter<'a>(&'a self) -> impl Iterator<Item = KVP<&'a TKey, &'a TValue>>
55 where
56 TKey: 'a,
57 TValue: 'a,
58 Self: 'a,
59 {
60 AbseilUnsafeIter::from_hash_table(self).map(|kvp| ptr_to_ref!(kvp).as_ref_kvp())
61 }
62}