zenoh_keyexpr/keyexpr_tree/
support.rsuse alloc::boxed::Box;
use core::ops::{Deref, DerefMut};
pub trait IWildness: 'static {
fn non_wild() -> Self;
fn get(&self) -> bool;
fn set(&mut self, wildness: bool) -> bool;
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NonWild;
impl IWildness for NonWild {
fn non_wild() -> Self {
NonWild
}
fn get(&self) -> bool {
false
}
fn set(&mut self, wildness: bool) -> bool {
if wildness {
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.")
}
false
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UnknownWildness;
impl IWildness for UnknownWildness {
fn non_wild() -> Self {
UnknownWildness
}
fn get(&self) -> bool {
true
}
fn set(&mut self, _wildness: bool) -> bool {
true
}
}
impl IWildness for bool {
fn non_wild() -> Self {
false
}
fn get(&self) -> bool {
*self
}
fn set(&mut self, wildness: bool) -> bool {
core::mem::replace(self, wildness)
}
}
pub enum IterOrOption<Iter: Iterator, Item> {
Opt(Option<Item>),
Iter(Iter),
}
impl<Iter: Iterator, Item> Iterator for IterOrOption<Iter, Item>
where
Iter::Item: Coerce<Item>,
{
type Item = Item;
fn next(&mut self) -> Option<Self::Item> {
match self {
IterOrOption::Opt(v) => v.take(),
IterOrOption::Iter(it) => it.next().map(Coerce::coerce),
}
}
}
pub struct Coerced<Iter: Iterator, Item> {
iter: Iter,
_item: core::marker::PhantomData<Item>,
}
impl<Iter: Iterator, Item> Coerced<Iter, Item> {
pub fn new(iter: Iter) -> Self {
Self {
iter,
_item: Default::default(),
}
}
}
impl<Iter: Iterator, Item> Iterator for Coerced<Iter, Item>
where
Iter::Item: Coerce<Item>,
{
type Item = Item;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(Coerce::coerce)
}
}
trait Coerce<Into> {
fn coerce(self) -> Into;
}
impl<T> Coerce<T> for T {
fn coerce(self) -> T {
self
}
}
impl<'a, T> Coerce<&'a T> for &'a Box<T> {
fn coerce(self) -> &'a T {
self.deref()
}
}
impl<'a, T> Coerce<&'a mut T> for &'a mut Box<T> {
fn coerce(self) -> &'a mut T {
self.deref_mut()
}
}
impl<Iter: Iterator, Item> From<Iter> for IterOrOption<Iter, Item> {
fn from(it: Iter) -> Self {
Self::Iter(it)
}
}