use crate::config::{DefaultKeyConfig, KeyConfig};
use crate::parity::Odd;
use core::cmp::Ordering;
use core::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Key<K: KeyConfig = DefaultKeyConfig> {
repr: K,
}
impl<K: KeyConfig> Key<K> {
#[inline]
pub fn idx(&self) -> K::Idx {
self.repr.idx()
}
#[inline]
pub fn generation(&self) -> Odd<K::Gen> {
self.repr.generation()
}
#[inline]
pub fn is_max_generation(&self) -> bool {
self.generation() == K::max_generation()
}
#[inline]
pub fn from_repr(repr: K) -> Self {
Self { repr }
}
#[inline]
pub fn repr(&self) -> K {
self.repr
}
}
impl<K: KeyConfig> PartialOrd for Key<K> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<K: KeyConfig> Ord for Key<K> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.idx()
.cmp(&other.idx())
.then(self.generation().cmp(&other.generation()))
}
}
impl<K: KeyConfig> fmt::Debug for Key<K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Key")
.field("idx", &self.idx())
.field("generation", &self.generation().get())
.finish()
}
}