zenoh_keyexpr/keyexpr_tree/impls/
hashmap_impl.rs1use core::hash::Hasher;
16#[cfg(not(feature = "std"))]
17#[allow(deprecated)]
19use core::hash::SipHasher as DefaultHasher;
20#[cfg(feature = "std")]
21use std::collections::{
22 hash_map::{DefaultHasher, Entry, Iter, IterMut, Values, ValuesMut},
23 HashMap,
24};
25
26#[cfg(not(feature = "std"))]
27use hashbrown::{
28 hash_map::{Entry, Iter, IterMut, Values, ValuesMut},
29 HashMap,
30};
31
32use crate::keyexpr_tree::*;
33
34#[cfg_attr(not(feature = "std"), allow(deprecated))]
35pub struct HashMapProvider<Hash: Hasher + Default + 'static = DefaultHasher>(
36 core::marker::PhantomData<Hash>,
37);
38impl<T: 'static, Hash: Hasher + Default + 'static> IChildrenProvider<T> for HashMapProvider<Hash> {
39 type Assoc = HashMap<OwnedKeyExpr, T, core::hash::BuildHasherDefault<Hash>>;
40}
41
42#[cfg(not(feature = "std"))]
43impl<'a: 'b, 'b, T: HasChunk, S: core::hash::BuildHasher> IEntry<'a, 'b, T>
44 for Entry<'a, OwnedKeyExpr, T, S>
45{
46 fn get_or_insert_with<F: FnOnce(&'b keyexpr) -> T>(self, f: F) -> &'a mut T {
47 match self {
48 Entry::Vacant(entry) => {
49 let value = unsafe { f(core::mem::transmute::<&keyexpr, &keyexpr>(entry.key())) };
50 entry.insert(value)
51 }
52 Entry::Occupied(v) => v.into_mut(),
53 }
54 }
55}
56#[cfg(feature = "std")]
57impl<'a: 'b, 'b, T: HasChunk> IEntry<'a, 'b, T> for Entry<'a, OwnedKeyExpr, T> {
58 fn get_or_insert_with<F: FnOnce(&'b keyexpr) -> T>(self, f: F) -> &'a mut T {
59 match self {
60 Entry::Vacant(entry) => {
61 let value = unsafe { f(core::mem::transmute::<&keyexpr, &keyexpr>(entry.key())) };
62 entry.insert(value)
63 }
64 Entry::Occupied(v) => v.into_mut(),
65 }
66 }
67}
68
69impl<T: HasChunk + AsNode<T> + AsNodeMut<T> + 'static, S: core::hash::BuildHasher> IChildren<T>
70 for HashMap<OwnedKeyExpr, T, S>
71{
72 type Node = T;
73 fn child_at(&self, chunk: &keyexpr) -> Option<&T> {
74 self.get(chunk)
75 }
76 fn child_at_mut(&mut self, chunk: &keyexpr) -> Option<&mut T> {
77 self.get_mut(chunk)
78 }
79 fn remove(&mut self, chunk: &keyexpr) -> Option<Self::Node> {
80 self.remove(chunk)
81 }
82 fn len(&self) -> usize {
83 self.len()
84 }
85 fn is_empty(&self) -> bool {
86 self.is_empty()
87 }
88
89 #[cfg(feature = "std")]
90 type Entry<'a, 'b>
91 = Entry<'a, OwnedKeyExpr, T>
92 where
93 Self: 'a,
94 'a: 'b,
95 T: 'b;
96 #[cfg(not(feature = "std"))]
97 type Entry<'a, 'b>
98 = Entry<'a, OwnedKeyExpr, T, S>
99 where
100 Self: 'a,
101 'a: 'b,
102 T: 'b;
103 fn entry<'a, 'b>(&'a mut self, chunk: &'b keyexpr) -> Self::Entry<'a, 'b>
104 where
105 Self: 'a,
106 'a: 'b,
107 T: 'b,
108 {
109 self.entry(chunk.into())
110 }
111
112 type Iter<'a>
113 = Values<'a, OwnedKeyExpr, T>
114 where
115 Self: 'a;
116 fn children<'a>(&'a self) -> Self::Iter<'a>
117 where
118 Self: 'a,
119 {
120 self.values()
121 }
122
123 type IterMut<'a>
124 = ValuesMut<'a, OwnedKeyExpr, T>
125 where
126 Self: 'a;
127
128 fn children_mut<'a>(&'a mut self) -> Self::IterMut<'a>
129 where
130 Self: 'a,
131 {
132 self.values_mut()
133 }
134
135 fn filter_out<F: FnMut(&mut T) -> bool>(&mut self, predicate: &mut F) {
136 self.retain(|_, v| predicate(v));
137 }
138
139 type Intersection<'a>
140 = super::FilterMap<Iter<'a, OwnedKeyExpr, T>, super::Intersection<'a>>
141 where
142 Self: 'a,
143 Self::Node: 'a;
144 fn intersection<'a>(&'a self, key: &'a keyexpr) -> Self::Intersection<'a> {
145 super::FilterMap::new(self.iter(), super::Intersection(key))
146 }
147 type IntersectionMut<'a>
148 = super::FilterMap<IterMut<'a, OwnedKeyExpr, T>, super::Intersection<'a>>
149 where
150 Self: 'a,
151 Self::Node: 'a;
152 fn intersection_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::IntersectionMut<'a> {
153 super::FilterMap::new(self.iter_mut(), super::Intersection(key))
154 }
155 type Inclusion<'a>
156 = super::FilterMap<Iter<'a, OwnedKeyExpr, T>, super::Inclusion<'a>>
157 where
158 Self: 'a,
159 Self::Node: 'a;
160 fn inclusion<'a>(&'a self, key: &'a keyexpr) -> Self::Inclusion<'a> {
161 super::FilterMap::new(self.iter(), super::Inclusion(key))
162 }
163 type InclusionMut<'a>
164 = super::FilterMap<IterMut<'a, OwnedKeyExpr, T>, super::Inclusion<'a>>
165 where
166 Self: 'a,
167 Self::Node: 'a;
168 fn inclusion_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::InclusionMut<'a> {
169 super::FilterMap::new(self.iter_mut(), super::Inclusion(key))
170 }
171}