use std::ops::{Add, AddAssign, Deref};
#[derive(Clone, Copy)]
pub(crate) struct Wrapping {
value: usize,
max: usize,
}
impl Wrapping {
pub const fn new(value: usize, max: usize) -> Self {
Self { value, max }
}
}
impl Add<usize> for Wrapping {
type Output = usize;
fn add(self, other: usize) -> Self::Output {
if other > self.max { unimplemented!() }
let mut result = self.value + other;
if result > self.max { result = result - self.max - 1 };
result
}
}
impl AddAssign<usize> for Wrapping {
fn add_assign(&mut self, other: usize) {
if other > self.max && self.max != 0 { unimplemented!() }
let mut result = self.value + other;
if result > self.max { result = result - self.max - 1 };
self.value = result;
}
}
impl Deref for Wrapping {
type Target = usize;
fn deref(&self) -> &Self::Target {
&self.value
}
}