#[derive(Clone, Copy, PartialEq, Eq)]
pub struct LoopBehavior {
pub(crate) rep: u16,
}
pub trait IntoLoopBehaviorFinite {
type Output;
fn into_loop_behavior(self) -> Self::Output;
}
impl IntoLoopBehaviorFinite for u16 {
type Output = Option<LoopBehavior>;
fn into_loop_behavior(self) -> Self::Output {
if self == 0 {
None
} else {
Some(LoopBehavior { rep: self - 1 })
}
}
}
impl IntoLoopBehaviorFinite for std::num::NonZeroU16 {
type Output = LoopBehavior;
fn into_loop_behavior(self) -> Self::Output {
LoopBehavior {
rep: self.get() - 1,
}
}
}
impl LoopBehavior {
pub const fn infinite() -> Self {
LoopBehavior { rep: 0xFFFF }
}
pub fn finite<T: IntoLoopBehaviorFinite>(repeat: T) -> T::Output {
repeat.into_loop_behavior()
}
pub const fn once() -> Self {
Self { rep: 0 }
}
pub const fn rep(&self) -> u16 {
self.rep
}
}
impl std::fmt::Debug for LoopBehavior {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.rep {
0xFFFF => write!(f, "LoopBehavior::Infinite"),
0 => write!(f, "LoopBehavior::Once"),
i => write!(f, "LoopBehavior::Finite({})", i + 1),
}
}
}
impl std::fmt::Display for LoopBehavior {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.rep {
0xFFFF => write!(f, "LoopBehavior::Infinite"),
0 => write!(f, "LoopBehavior::Once"),
i => write!(f, "LoopBehavior::Finite({})", i + 1),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[rstest::rstest]
#[test]
#[case::infinite(0xFFFF, LoopBehavior::infinite())]
#[case::finite(0x1233, LoopBehavior::finite(0x1234).unwrap())]
#[case::once(0x0000, LoopBehavior::once())]
#[cfg_attr(miri, ignore)]
fn loop_behavior(#[case] expect: u16, #[case] target: LoopBehavior) {
assert_eq!(expect, target.rep());
}
#[rstest::rstest]
#[test]
#[case(Some(LoopBehavior{ rep: 0 }), 1)]
#[case(Some(LoopBehavior{ rep: 0xFFFE }), 0xFFFF)]
#[case(None, 0)]
#[cfg_attr(miri, ignore)]
fn into_loop_behavior_u16(#[case] expect: Option<LoopBehavior>, #[case] rep: u16) {
assert_eq!(expect, LoopBehavior::finite(rep));
}
#[rstest::rstest]
#[test]
#[case(LoopBehavior{ rep: 0 }, std::num::NonZeroU16::new(1).unwrap())]
#[case(LoopBehavior{ rep: 0xFFFE }, std::num::NonZeroU16::new(0xFFFF).unwrap())]
#[cfg_attr(miri, ignore)]
fn into_loop_behavior_non_zero_u16(
#[case] expect: LoopBehavior,
#[case] rep: std::num::NonZeroU16,
) {
assert_eq!(expect, LoopBehavior::finite(rep));
}
#[test]
#[cfg_attr(miri, ignore)]
fn display() {
assert_eq!(
format!("{}", LoopBehavior::infinite()),
"LoopBehavior::Infinite"
);
assert_eq!(format!("{}", LoopBehavior::once()), "LoopBehavior::Once");
assert_eq!(
format!("{}", LoopBehavior::finite(0x1234).unwrap()),
"LoopBehavior::Finite(4660)"
);
}
#[test]
#[cfg_attr(miri, ignore)]
fn debug() {
assert_eq!(
format!("{:?}", LoopBehavior::infinite()),
"LoopBehavior::Infinite"
);
assert_eq!(format!("{:?}", LoopBehavior::once()), "LoopBehavior::Once");
assert_eq!(
format!("{:?}", LoopBehavior::finite(0x1234).unwrap()),
"LoopBehavior::Finite(4660)"
);
}
}