zenoh_keyexpr/keyexpr_tree/impls/
keyed_set_impl.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15use core::hash::Hasher;
16#[cfg(not(feature = "std"))]
17// `SipHasher` is deprecated in favour of a symbol that only exists in `std`
18#[allow(deprecated)]
19use core::hash::SipHasher as DefaultHasher;
20#[cfg(feature = "std")]
21use std::collections::hash_map::DefaultHasher;
22
23use keyed_set::{KeyExtractor, KeyedSet};
24
25use crate::keyexpr_tree::*;
26
27#[cfg_attr(not(feature = "std"), allow(deprecated))]
28pub struct KeyedSetProvider<Hash: Hasher + Default + 'static = DefaultHasher>(
29    core::marker::PhantomData<Hash>,
30);
31impl<T: 'static, Hash: Hasher + Default + 'static> IChildrenProvider<T> for KeyedSetProvider<Hash> {
32    type Assoc = KeyedSet<T, ChunkExtractor>;
33}
34#[derive(Debug, Default, Clone, Copy)]
35pub struct ChunkExtractor;
36impl<'a, T: HasChunk> KeyExtractor<'a, T> for ChunkExtractor {
37    type Key = &'a keyexpr;
38    fn extract(&self, from: &'a T) -> Self::Key {
39        from.chunk()
40    }
41}
42impl<'a, 'b, T: HasChunk> IEntry<'a, 'b, T>
43    for keyed_set::Entry<'a, T, ChunkExtractor, &'b keyexpr>
44{
45    fn get_or_insert_with<F: FnOnce(&'b keyexpr) -> T>(self, f: F) -> &'a mut T {
46        Self::get_or_insert_with(self, f)
47    }
48}
49
50impl<T: HasChunk + AsNode<T> + AsNodeMut<T>> IChildren<T> for KeyedSet<T, ChunkExtractor> {
51    type Node = T;
52    fn child_at(&self, chunk: &keyexpr) -> Option<&T> {
53        self.get(&chunk)
54    }
55    fn child_at_mut(&mut self, chunk: &keyexpr) -> Option<&mut T> {
56        // Unicity is guaranteed by &mut self
57        unsafe { self.get_mut_unguarded(&chunk) }
58    }
59    fn remove(&mut self, chunk: &keyexpr) -> Option<T> {
60        self.remove(&chunk)
61    }
62    fn len(&self) -> usize {
63        self.len()
64    }
65    fn is_empty(&self) -> bool {
66        self.is_empty()
67    }
68    type Entry<'a, 'b>
69        = keyed_set::Entry<'a, T, ChunkExtractor, &'b keyexpr>
70    where
71        Self: 'a,
72        'a: 'b,
73        T: 'b;
74    fn entry<'a, 'b>(&'a mut self, chunk: &'b keyexpr) -> Self::Entry<'a, 'b>
75    where
76        Self: 'a,
77        'a: 'b,
78        T: 'b,
79    {
80        self.entry(chunk)
81    }
82
83    type Iter<'a>
84        = keyed_set::Iter<'a, T>
85    where
86        Self: 'a;
87    fn children<'a>(&'a self) -> Self::Iter<'a>
88    where
89        Self: 'a,
90    {
91        self.iter()
92    }
93
94    type IterMut<'a>
95        = keyed_set::IterMut<'a, T>
96    where
97        Self: 'a;
98
99    fn children_mut<'a>(&'a mut self) -> Self::IterMut<'a>
100    where
101        Self: 'a,
102    {
103        self.iter_mut()
104    }
105
106    fn filter_out<F: FnMut(&mut T) -> bool>(&mut self, predicate: &mut F) {
107        self.drain_where(predicate);
108    }
109
110    type Intersection<'a>
111        = super::FilterMap<keyed_set::Iter<'a, T>, super::Intersection<'a>>
112    where
113        Self: 'a,
114        Self::Node: 'a;
115    fn intersection<'a>(&'a self, key: &'a keyexpr) -> Self::Intersection<'a> {
116        super::FilterMap::new(self.iter(), super::Intersection(key))
117    }
118    type IntersectionMut<'a>
119        = super::FilterMap<keyed_set::IterMut<'a, T>, super::Intersection<'a>>
120    where
121        Self: 'a,
122        Self::Node: 'a;
123    fn intersection_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::IntersectionMut<'a> {
124        super::FilterMap::new(self.iter_mut(), super::Intersection(key))
125    }
126    type Inclusion<'a>
127        = super::FilterMap<keyed_set::Iter<'a, T>, super::Inclusion<'a>>
128    where
129        Self: 'a,
130        Self::Node: 'a;
131    fn inclusion<'a>(&'a self, key: &'a keyexpr) -> Self::Inclusion<'a> {
132        super::FilterMap::new(self.iter(), super::Inclusion(key))
133    }
134    type InclusionMut<'a>
135        = super::FilterMap<keyed_set::IterMut<'a, T>, super::Inclusion<'a>>
136    where
137        Self: 'a,
138        Self::Node: 'a;
139    fn inclusion_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::InclusionMut<'a> {
140        super::FilterMap::new(self.iter_mut(), super::Inclusion(key))
141    }
142}