pub trait RevDigits<Digits>
where
Digits: Iterator<Item = u8>,
{
fn reversed_digits(&self) -> Digits;
fn into_triplets(&self) -> Triplets<Digits> {
Triplets::from(self.reversed_digits())
}
}
#[derive(Debug, Clone, Copy)]
pub struct Triplet([u8; 3]);
impl From<[u8; 3]> for Triplet {
fn from(arr: [u8; 3]) -> Self {
debug_assert!(arr.iter().all(|&x| x < 10), "digits must be less then 10");
Self(arr)
}
}
impl Triplet {
#[inline]
pub fn hs(&self) -> u8 {
self.0[0]
}
#[inline]
pub fn ds(&self) -> u8 {
self.0[1]
}
#[inline]
pub fn us(&self) -> u8 {
self.0[2]
}
#[inline]
pub fn ds_us(&self) -> [u8; 2] {
let [_, ds, us] = self.0;
[ds, us]
}
#[inline]
pub fn is_sero(&self) -> bool {
self.0.iter().all(|&x| x == 0)
}
}
impl AsRef<[u8; 3]> for Triplet {
#[inline]
fn as_ref(&self) -> &[u8; 3] {
&self.0
}
}
pub struct Triplets<T>(T);
impl<T> From<T> for Triplets<T>
where
T: Iterator<Item = u8>,
{
#[inline]
fn from(reversed_digits: T) -> Self {
Self(reversed_digits)
}
}
impl<T> Iterator for Triplets<T>
where
T: Iterator<Item = u8>,
{
type Item = Triplet;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|third| {
let second = self.0.next().unwrap_or_default();
let first = self.0.next().unwrap_or_default();
let triplet = [first, second, third];
triplet.into()
})
}
}