Skip to main content

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        // SAFETY: upheld by the surrounding invariants and prior validation.
58        unsafe { self.get_mut_unguarded(&chunk) }
59    }
60    fn remove(&mut self, chunk: &keyexpr) -> Option<T> {
61        self.remove(&chunk)
62    }
63    fn len(&self) -> usize {
64        self.len()
65    }
66    fn is_empty(&self) -> bool {
67        self.is_empty()
68    }
69    type Entry<'a, 'b>
70        = keyed_set::Entry<'a, T, ChunkExtractor, &'b keyexpr>
71    where
72        Self: 'a,
73        'a: 'b,
74        T: 'b;
75    fn entry<'a, 'b>(&'a mut self, chunk: &'b keyexpr) -> Self::Entry<'a, 'b>
76    where
77        Self: 'a,
78        'a: 'b,
79        T: 'b,
80    {
81        self.entry(chunk)
82    }
83
84    type Iter<'a>
85        = keyed_set::Iter<'a, T>
86    where
87        Self: 'a;
88    fn children<'a>(&'a self) -> Self::Iter<'a>
89    where
90        Self: 'a,
91    {
92        self.iter()
93    }
94
95    type IterMut<'a>
96        = keyed_set::IterMut<'a, T>
97    where
98        Self: 'a;
99
100    fn children_mut<'a>(&'a mut self) -> Self::IterMut<'a>
101    where
102        Self: 'a,
103    {
104        self.iter_mut()
105    }
106
107    fn filter_out<F: FnMut(&mut T) -> bool>(&mut self, predicate: &mut F) {
108        self.drain_where(predicate);
109    }
110
111    type Intersection<'a>
112        = super::FilterMap<keyed_set::Iter<'a, T>, super::Intersection<'a>>
113    where
114        Self: 'a,
115        Self::Node: 'a;
116    fn intersection<'a>(&'a self, key: &'a keyexpr) -> Self::Intersection<'a> {
117        super::FilterMap::new(self.iter(), super::Intersection(key))
118    }
119    type IntersectionMut<'a>
120        = super::FilterMap<keyed_set::IterMut<'a, T>, super::Intersection<'a>>
121    where
122        Self: 'a,
123        Self::Node: 'a;
124    fn intersection_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::IntersectionMut<'a> {
125        super::FilterMap::new(self.iter_mut(), super::Intersection(key))
126    }
127    type Inclusion<'a>
128        = super::FilterMap<keyed_set::Iter<'a, T>, super::Inclusion<'a>>
129    where
130        Self: 'a,
131        Self::Node: 'a;
132    fn inclusion<'a>(&'a self, key: &'a keyexpr) -> Self::Inclusion<'a> {
133        super::FilterMap::new(self.iter(), super::Inclusion(key))
134    }
135    type InclusionMut<'a>
136        = super::FilterMap<keyed_set::IterMut<'a, T>, super::Inclusion<'a>>
137    where
138        Self: 'a,
139        Self::Node: 'a;
140    fn inclusion_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::InclusionMut<'a> {
141        super::FilterMap::new(self.iter_mut(), super::Inclusion(key))
142    }
143}