use crate::{Canonicity, DecodeErrorKind};
pub trait EmptyState: ForOverwrite {
#[inline(always)]
fn empty() -> Self
where
Self: Sized,
{
ForOverwrite::for_overwrite()
}
fn is_empty(&self) -> bool;
fn clear(&mut self);
}
pub trait ForOverwrite {
fn for_overwrite() -> Self
where
Self: Sized;
}
#[macro_export]
macro_rules! for_overwrite_via_default {
(
$ty:ty
$(, with generics ($($generics:tt)*))?
$(, with where clause ($($where_clause:tt)*))?
) => {
impl<$($($generics)*)?> $crate::encoding::ForOverwrite for $ty
where
Self: ::core::default::Default,
$($($where_clause)*)?
{
#[inline]
fn for_overwrite() -> Self {
::core::default::Default::default()
}
}
};
}
pub use for_overwrite_via_default;
#[macro_export]
macro_rules! empty_state_via_for_overwrite {
(
$ty:ty
$(, with generics ($($generics:tt)*))?
$(, with where clause ($($where_clause:tt)*))?
) => {
impl<$($($generics)*)?> $crate::encoding::EmptyState for $ty
where
Self: $crate::encoding::ForOverwrite + ::core::cmp::PartialEq,
$($($where_clause)*)?
{
#[inline]
fn is_empty(&self) -> bool {
*self == Self::empty()
}
#[inline]
fn clear(&mut self) {
*self = Self::empty();
}
}
};
}
pub use empty_state_via_for_overwrite;
#[macro_export]
macro_rules! empty_state_via_default {
(
$ty:ty
$(, with generics ($($generics:tt)*))?
$(, with where clause ($($where_clause:tt)*))?
) => {
$crate::for_overwrite_via_default!(
$ty
$(, with generics ($($generics*)*))?
$(, with where clause ($($where_clause)*))?
);
$crate::empty_state_via_for_overwrite!(
$ty
$(, with generics ($($generics*)*))?
$(, with where clause ($($where_clause)*))?
);
};
}
pub use empty_state_via_default;
pub trait Enumeration: Eq + Sized {
fn to_number(&self) -> u32;
fn try_from_number(n: u32) -> Result<Self, u32>;
fn is_valid(n: u32) -> bool;
}
pub trait Collection: EmptyState {
type Item;
type RefIter<'a>: ExactSizeIterator<Item = &'a Self::Item>
where
Self::Item: 'a,
Self: 'a;
type ReverseIter<'a>: Iterator<Item = &'a Self::Item>
where
Self::Item: 'a,
Self: 'a;
fn len(&self) -> usize;
fn iter(&self) -> Self::RefIter<'_>;
fn reversed(&self) -> Self::ReverseIter<'_>;
fn insert(&mut self, item: Self::Item) -> Result<(), DecodeErrorKind>;
}
pub trait DistinguishedCollection: Collection + Eq {
fn insert_distinguished(&mut self, item: Self::Item) -> Result<Canonicity, DecodeErrorKind>;
}
pub(crate) trait TriviallyDistinguishedCollection {}
impl<T> DistinguishedCollection for T
where
T: Eq + Collection + TriviallyDistinguishedCollection,
{
#[inline]
fn insert_distinguished(&mut self, item: Self::Item) -> Result<Canonicity, DecodeErrorKind> {
self.insert(item).map(|()| Canonicity::Canonical)
}
}
pub trait Mapping: EmptyState {
type Key;
type Value;
type RefIter<'a>: ExactSizeIterator<Item = (&'a Self::Key, &'a Self::Value)>
where
Self::Key: 'a,
Self::Value: 'a,
Self: 'a;
type ReverseIter<'a>: Iterator<Item = (&'a Self::Key, &'a Self::Value)>
where
Self::Key: 'a,
Self::Value: 'a,
Self: 'a;
fn len(&self) -> usize;
fn iter(&self) -> Self::RefIter<'_>;
fn reversed(&self) -> Self::ReverseIter<'_>;
fn insert(&mut self, key: Self::Key, value: Self::Value) -> Result<(), DecodeErrorKind>;
}
pub trait DistinguishedMapping: Mapping {
fn insert_distinguished(
&mut self,
key: Self::Key,
value: Self::Value,
) -> Result<Canonicity, DecodeErrorKind>;
}