Skip to main content

osom_lib_hash_tables/
std_hash_map.rs

1#![allow(clippy::implicit_hasher)]
2use core::borrow::Borrow;
3use core::hash::Hash;
4
5use std::collections::{HashMap, hash_map::Entry};
6
7use osom_lib_primitives::kvp::KVP;
8use osom_lib_primitives::length::Length;
9
10use crate::errors::HashTableError;
11use crate::traits::{ImmutableHashTable, MutableHashTable};
12
13impl<TKey, TValue> ImmutableHashTable<TKey, TValue> for HashMap<TKey, TValue>
14where
15    TKey: Hash + Eq,
16{
17    #[inline]
18    fn length(&self) -> Length {
19        Length::try_from_usize(self.len()).unwrap()
20    }
21
22    #[inline]
23    fn contains<Q>(&self, key: &Q) -> bool
24    where
25        TKey: Borrow<Q>,
26        Q: Eq + core::hash::Hash + ?Sized,
27    {
28        self.contains_key(key)
29    }
30
31    #[inline]
32    fn get<Q>(&self, key: &Q) -> Option<&TValue>
33    where
34        TKey: Borrow<Q>,
35        Q: Eq + core::hash::Hash + ?Sized,
36    {
37        self.get(key)
38    }
39
40    #[inline]
41    fn get_key_value<Q>(&self, key: &Q) -> Option<KVP<&TKey, &TValue>>
42    where
43        TKey: Borrow<Q>,
44        Q: Eq + core::hash::Hash + ?Sized,
45    {
46        self.get_key_value(key).map(Into::into)
47    }
48
49    #[inline]
50    fn iter<'a>(&'a self) -> impl Iterator<Item = KVP<&'a TKey, &'a TValue>>
51    where
52        TKey: 'a,
53        TValue: 'a,
54        Self: 'a,
55    {
56        self.iter().map(Into::into)
57    }
58}
59
60impl<TKey, TValue> MutableHashTable<TKey, TValue> for HashMap<TKey, TValue>
61where
62    TKey: Hash + Eq,
63{
64    fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut TValue>
65    where
66        TKey: Borrow<Q>,
67        Q: Eq + Hash + ?Sized,
68    {
69        self.get_mut(key)
70    }
71
72    fn get_key_value_mut<Q>(&mut self, _key: &Q) -> Option<KVP<&TKey, &mut TValue>>
73    where
74        TKey: Borrow<Q>,
75        Q: Eq + Hash + ?Sized,
76    {
77        panic!("std::HashMap does not support get_key_value_mut");
78    }
79
80    #[inline]
81    fn remove_entry<Q>(&mut self, key: &Q) -> Option<KVP<TKey, TValue>>
82    where
83        TKey: Borrow<Q>,
84        Q: Eq + Hash + ?Sized,
85    {
86        self.remove_entry(key).map(Into::into)
87    }
88
89    #[inline]
90    fn try_insert_or_update_with<FAdd, FUpdate>(
91        &mut self,
92        key: TKey,
93        adder: FAdd,
94        updater: FUpdate,
95    ) -> Result<&mut TValue, HashTableError>
96    where
97        FAdd: FnOnce() -> TValue,
98        FUpdate: FnOnce(&mut TValue),
99    {
100        let reference = match self.entry(key) {
101            Entry::Occupied(mut o) => {
102                updater(o.get_mut());
103                o.into_mut()
104            }
105            Entry::Vacant(v) => v.insert(adder()),
106        };
107
108        Ok(reference)
109    }
110
111    #[inline]
112    fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = KVP<&'a TKey, &'a mut TValue>>
113    where
114        TKey: 'a,
115        TValue: 'a,
116        Self: 'a,
117    {
118        self.iter_mut().map(Into::into)
119    }
120}