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))]
28#[derive(Debug)]
29pub struct KeyedSetProvider<Hash: Hasher + Default + 'static = DefaultHasher>(
30    core::marker::PhantomData<Hash>,
31);
32impl<T: 'static, Hash: Hasher + Default + 'static> IChildrenProvider<T> for KeyedSetProvider<Hash> {
33    type Assoc = KeyedSet<T, ChunkExtractor>;
34}
35#[derive(Debug, Default, Clone, Copy)]
36pub struct ChunkExtractor;
37impl<'a, T: HasChunk> KeyExtractor<'a, T> for ChunkExtractor {
38    type Key = &'a keyexpr;
39    fn extract(&self, from: &'a T) -> Self::Key {
40        from.chunk()
41    }
42}
43impl<'a, 'b, T: HasChunk> IEntry<'a, 'b, T>
44    for keyed_set::Entry<'a, T, ChunkExtractor, &'b keyexpr>
45{
46    fn get_or_insert_with<F: FnOnce(&'b keyexpr) -> T>(self, f: F) -> &'a mut T {
47        Self::get_or_insert_with(self, f)
48    }
49}
50
51impl<T: HasChunk + AsNode<T> + AsNodeMut<T>> IChildren<T> for KeyedSet<T, ChunkExtractor> {
52    type Node = T;
53    fn child_at(&self, chunk: &keyexpr) -> Option<&T> {
54        self.get(&chunk)
55    }
56    fn child_at_mut(&mut self, chunk: &keyexpr) -> Option<&mut T> {
57        // Unicity is guaranteed by &mut self
58        // SAFETY: upheld by the surrounding invariants and prior validation.
59        unsafe { self.get_mut_unguarded(&chunk) }
60    }
61    fn remove(&mut self, chunk: &keyexpr) -> Option<T> {
62        self.remove(&chunk)
63    }
64    fn len(&self) -> usize {
65        self.len()
66    }
67    fn is_empty(&self) -> bool {
68        self.is_empty()
69    }
70    type Entry<'a, 'b>
71        = keyed_set::Entry<'a, T, ChunkExtractor, &'b keyexpr>
72    where
73        Self: 'a,
74        'a: 'b,
75        T: 'b;
76    fn entry<'a, 'b>(&'a mut self, chunk: &'b keyexpr) -> Self::Entry<'a, 'b>
77    where
78        Self: 'a,
79        'a: 'b,
80        T: 'b,
81    {
82        self.entry(chunk)
83    }
84
85    type Iter<'a>
86        = keyed_set::Iter<'a, T>
87    where
88        Self: 'a;
89    fn children<'a>(&'a self) -> Self::Iter<'a>
90    where
91        Self: 'a,
92    {
93        self.iter()
94    }
95
96    type IterMut<'a>
97        = keyed_set::IterMut<'a, T>
98    where
99        Self: 'a;
100
101    fn children_mut<'a>(&'a mut self) -> Self::IterMut<'a>
102    where
103        Self: 'a,
104    {
105        self.iter_mut()
106    }
107
108    fn filter_out<F: FnMut(&mut T) -> bool>(&mut self, predicate: &mut F) {
109        self.drain_where(predicate);
110    }
111
112    type Intersection<'a>
113        = super::FilterMap<keyed_set::Iter<'a, T>, super::Intersection<'a>>
114    where
115        Self: 'a,
116        Self::Node: 'a;
117    fn intersection<'a>(&'a self, key: &'a keyexpr) -> Self::Intersection<'a> {
118        super::FilterMap::new(self.iter(), super::Intersection(key))
119    }
120    type IntersectionMut<'a>
121        = super::FilterMap<keyed_set::IterMut<'a, T>, super::Intersection<'a>>
122    where
123        Self: 'a,
124        Self::Node: 'a;
125    fn intersection_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::IntersectionMut<'a> {
126        super::FilterMap::new(self.iter_mut(), super::Intersection(key))
127    }
128    type Inclusion<'a>
129        = super::FilterMap<keyed_set::Iter<'a, T>, super::Inclusion<'a>>
130    where
131        Self: 'a,
132        Self::Node: 'a;
133    fn inclusion<'a>(&'a self, key: &'a keyexpr) -> Self::Inclusion<'a> {
134        super::FilterMap::new(self.iter(), super::Inclusion(key))
135    }
136    type InclusionMut<'a>
137        = super::FilterMap<keyed_set::IterMut<'a, T>, super::Inclusion<'a>>
138    where
139        Self: 'a,
140        Self::Node: 'a;
141    fn inclusion_mut<'a>(&'a mut self, key: &'a keyexpr) -> Self::InclusionMut<'a> {
142        super::FilterMap::new(self.iter_mut(), super::Inclusion(key))
143    }
144}