use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Prior<T> {
None,
Single(T),
Merge(T, T),
}
impl<T> Prior<T> {
pub fn as_ref(&self) -> Prior<&T> {
match self {
Prior::None => Prior::None,
Prior::Single(x) => Prior::Single(x),
Prior::Merge(x, y) => Prior::Merge(x, y),
}
}
}
impl<T: Clone> Prior<&T> {
pub fn cloned(self) -> Prior<T> {
match self {
Prior::None => Prior::None,
Prior::Single(x) => Prior::Single(x.clone()),
Prior::Merge(x, y) => Prior::Merge(x.clone(), y.clone()),
}
}
}
impl<T: Copy> Prior<&T> {
pub fn copied(self) -> Prior<T> {
match self {
Prior::None => Prior::None,
Prior::Single(x) => Prior::Single(*x),
Prior::Merge(x, y) => Prior::Merge(*x, *y),
}
}
}
pub struct IntoIter<T>(Prior<T>);
impl<T> IntoIterator for Prior<T> {
type IntoIter = IntoIter<T>;
type Item = T;
fn into_iter(self) -> Self::IntoIter {
IntoIter(self)
}
}
impl<T> Iterator for IntoIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match core::mem::replace(&mut self.0, Prior::None) {
Prior::None => None,
Prior::Single(x) => Some(x),
Prior::Merge(x, y) => {
self.0 = Prior::Single(y);
Some(x)
}
}
}
}