use std::fmt;
use serde::{Deserialize, Serialize};
use super::LoopRounds;
#[derive(
Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct LoopRound(u32);
impl LoopRound {
pub const ZERO: Self = Self(0);
#[must_use]
pub const fn of(value: u32) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u32 {
self.0
}
#[must_use]
pub const fn next(self) -> Self {
Self(self.0.saturating_add(1))
}
#[must_use]
pub const fn is_exhausted(self, bound: LoopRounds) -> bool {
self.0 >= bound.get()
}
}
impl fmt::Display for LoopRound {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_bounded_loop_stops_at_its_bound() {
let bound = LoopRounds::new(2).unwrap();
assert!(!LoopRound::ZERO.is_exhausted(bound));
assert!(!LoopRound::ZERO.next().is_exhausted(bound));
assert!(LoopRound::ZERO.next().next().is_exhausted(bound));
}
}