Skip to main content

zenoh_keyexpr/keyexpr_tree/
support.rs

1use alloc::boxed::Box;
2use core::ops::{Deref, DerefMut};
3
4/// Allows specializing KeTrees based on their eventual storage of wild KEs.
5///
6/// This is useful to allow KeTrees to be much faster at iterating when the queried KE is non-wild,
7/// and the KeTree is informed by its wildness that it doesn't contain any wilds.
8pub trait IWildness: 'static {
9    fn non_wild() -> Self;
10    fn get(&self) -> bool;
11    fn set(&mut self, wildness: bool) -> bool;
12}
13/// Disallows the KeTree from storing _any_ wild KE.
14///
15/// Attempting to store a wild KE on a `KeTree<NonWild>` will cause a panic.
16#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct NonWild;
18impl IWildness for NonWild {
19    fn non_wild() -> Self {
20        NonWild
21    }
22    fn get(&self) -> bool {
23        false
24    }
25    fn set(&mut self, wildness: bool) -> bool {
26        if wildness {
27            panic!("Attempted to set NonWild to wild, which breaks its contract. You likely attempted to insert a wild key into a `NonWild` tree. Use `bool` instead to make wildness determined at runtime.")
28        }
29        false
30    }
31}
32/// A ZST that forces the KeTree to always believe it contains wild KEs.
33#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
34pub struct UnknownWildness;
35impl IWildness for UnknownWildness {
36    fn non_wild() -> Self {
37        UnknownWildness
38    }
39    fn get(&self) -> bool {
40        true
41    }
42    fn set(&mut self, _wildness: bool) -> bool {
43        true
44    }
45}
46/// Stores the wildness of the KeTree at runtime, allowing it to select its
47/// iteration algorithms based on the wildness at the moment of query.
48impl IWildness for bool {
49    fn non_wild() -> Self {
50        false
51    }
52    fn get(&self) -> bool {
53        *self
54    }
55    fn set(&mut self, wildness: bool) -> bool {
56        core::mem::replace(self, wildness)
57    }
58}
59
60pub enum IterOrOption<Iter: Iterator, Item> {
61    Opt(Option<Item>),
62    Iter(Iter),
63}
64
65impl<Iter: Iterator, Item> core::fmt::Debug for IterOrOption<Iter, Item> {
66    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
67        match self {
68            Self::Opt(_) => f.debug_tuple("Opt").field(&"..").finish(),
69            Self::Iter(_) => f.debug_tuple("Iter").field(&"..").finish(),
70        }
71    }
72}
73impl<Iter: Iterator, Item> Iterator for IterOrOption<Iter, Item>
74where
75    Iter::Item: Coerce<Item>,
76{
77    type Item = Item;
78    fn next(&mut self) -> Option<Self::Item> {
79        match self {
80            IterOrOption::Opt(v) => v.take(),
81            IterOrOption::Iter(it) => it.next().map(Coerce::coerce),
82        }
83    }
84}
85trait Coerce<Into> {
86    fn coerce(self) -> Into;
87}
88impl<T> Coerce<T> for T {
89    fn coerce(self) -> T {
90        self
91    }
92}
93impl<'a, T> Coerce<&'a T> for &'a Box<T> {
94    fn coerce(self) -> &'a T {
95        self.deref()
96    }
97}
98impl<'a, T> Coerce<&'a mut T> for &'a mut Box<T> {
99    fn coerce(self) -> &'a mut T {
100        self.deref_mut()
101    }
102}
103impl<Iter: Iterator, Item> From<Iter> for IterOrOption<Iter, Item> {
104    fn from(it: Iter) -> Self {
105        Self::Iter(it)
106    }
107}