Your months are 1-based (1..=12), but your modulo math is 0-based (0..=11).
When you do (total_months % 12) as u8, if total_months is 12, you get 0, which is not a valid month.
2. Add Implementation
Adding 11 to 1: 1 + 11 = 12, but (12 % 12) = 0, so you get Month::new(0), which is invalid.
3. Sub Implementation
Subtracting 1 from 1: 1 - 1 = 0, (0 % 12 + 12) % 12 = 0, so you get Month::new(0), which is invalid.
Corrected Implementation
You need to:
Convert to 0-based for calculation, then back to 1-based for storage.
Ensure the result is always in 1..=12.
Add Implementation
rust
impl Add<u8> for Month {
type Output = Self;
fn add(self, other: u8) -> Self {
// Convert to 0-based
let zero_based = (self.value() - 1) as i32;
let total = zero_based + other as i32;
// Modulo 12, then back to 1-based
Month::new(((total % 12 + 12) % 12 + 1) as u8)
}
}
Sub Implementation
rust
impl Sub<u8> for Month {
type Output = Self;
fn sub(self, other: u8) -> Self {
// Convert to 0-based
let zero_based = (self.value() - 1) as i32;
let total = zero_based - other as i32;
// Modulo 12, then back to 1-based
Month::new(((total % 12 + 12) % 12 + 1) as u8)
}
}
Full Example
Assuming you have a Month::new(u8) constructor and a value() method that returns the month as (1..=12):
User
It still fails with Month::new(1) - Month::new(12):
code
Diff < left / right > :
Month {
< month: 1,
> month: 11,
}
Assistant
Great catch! The issue is that you haven't implemented Sub<Month> for Month, only Sub<u8>.
Your test:
is using Sub<Month>, but your code only supports Sub<u8>.
So, Month::new(1) - Month::new(12) is not using your intended logic.
Solution
Implement Sub<Month> for Month:
rust
use std::ops::Sub;
impl Sub<Month> for Month {
type Output = Self;
fn sub(self, other: Month) -> Self {
// Convert both to 0-based
let lhs = (self.value() - 1) as i32;
let rhs = (other.value() - 1) as i32;
let total = lhs - rhs;
Month::new(((total % 12 + 12) % 12 + 1) as u8)
}
}
Full Example
rust
use std::ops::{Add, Sub};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Month {
month: u8,
}
impl Month {
pub fn new(month: u8) -> Self {
assert!((1..=12).contains(&month));
Self { month }
}
pub fn value(&self) -> u8 {
self.month
}
}
impl Add<u8> for Month {
type Output = Self;
fn add(self, other: u8) -> Self {
let zero_based = (self.value() - 1) as i32;
let total = zero_based + other as i32;
Month::new(((total % 12 + 12) % 12 + 1) as u8)
}
}
impl Sub<u8> for Month {
type Output = Self;
fn sub(self, other: u8) -> Self {
let zero_based = (self.value() - 1) as i32;
let total = zero_based - other as i32;
Month::new(((total % 12 + 12) % 12 + 1) as u8)
}
}
impl Sub<Month> for Month {
type Output = Self;
fn sub(self, other: Month) -> Self {
let lhs = (self.value() - 1) as i32;
let rhs = (other.value() - 1) as i32;
let total = lhs - rhs;
Month::new(((total % 12 + 12) % 12 + 1) as u8)
}
}
use std::ops::{Add, Sub};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Month {
month: u8,
}
impl Month {
pub fn new(month: u8) -> Self {
assert!((1..=12).contains(&month));
Self { month }
}
pub fn value(&self) -> u8 {
self.month
}
}
impl Add<u8> for Month {
type Output = Self;
fn add(self, other: u8) -> Self {
let zero_based = (self.value() - 1) as i32;
let total = zero_based + other as i32;
Month::new(((total % 12 + 12) % 12 + 1) as u8)
}
}
impl Sub<u8> for Month {
type Output = Self;
fn sub(self, other: u8) -> Self {
let zero_based = (self.value() - 1) as i32;
let total = zero_based - other as i32;
Month::new(((total % 12 + 12) % 12 + 1) as u8)
}
}
Test Results
Month::new(1) + 11 == Month::new(12) ✔️
Month::new(1) - 1 == Month::new(12) ✔️
Month::new(1) - Month::new(12) (if you want to support Sub<Month>, implement that trait similarly).
Summary:
Always convert to 0-based for modulo, then back to 1-based for storage. The formula ((value % 12 + 12) % 12 + 1) ensures the result is always in 1..=12.
You can copy-paste these into your test module.
They will thoroughly check all the edge cases and normal cases for your Month arithmetic!
impl Sub<Month> for Month {
type Output = Self;
fn sub(self, other: Month) -> Self {
let lhs = self.value() as i32;
let rhs = other.value() as i32;
let diff = ((lhs - rhs) % 12 + 12) % 12;
let result = if diff == 0 { 12 } else { diff };
Month::new(result as u8)
}
}
let diff = ((other.value() as i32 - self.value() as i32) + 12) % 12;
let result = if diff == 0 { 12 } else { diff };
Month::new(result as u8)
But this gives the months backwards.
So, perhaps your test is expecting the number of months you need to go forward from rhs to reach lhs.
Conclusion
If you want Month::new(1) - Month::new(12) == Month::new(11), then the formula is:
((self.value() as i32 - other.value() as i32 + 12) % 12)
If 0, return 12; else, return the value.
But as shown, this gives 1, not 11.
But if you use ((other.value() as i32 - self.value() as i32 + 12) % 12), you get 11.
So, your test expects the number of months from rhs to lhs in the forward direction.
Final Implementation
rust
impl Sub<Month> for Month {
type Output = Self;
fn sub(self, other: Month) -> Self {
let diff = ((other.value() as i32 - self.value() as i32 + 12) % 12) as u8;
let result = if diff == 0 { 12 } else { diff };
Month::new(result)
}
}
Test Table
lhs
rhs
((rhs - lhs + 12) % 12)
result
1
12
(12 - 1 + 12) % 12 = 23 % 12 = 11
11
5
3
(3 - 5 + 12) % 12 = 10 % 12 = 10
10
3
5
(5 - 3 + 12) % 12 = 14 % 12 = 2
2
1
1
(1 - 1 + 12) % 12 = 12 % 12 = 0
12
Summary
If you want Month::new(1) - Month::new(12) == Month::new(11), use:
rust
let diff = ((other.value() as i32 - self.value() as i32 + 12) % 12) as u8;
let result = if diff == 0 { 12 } else { diff };
Month::new(result)
This matches your test expectations.
If you want the result to be "how many months forward from rhs to lhs", use the above. If you want "how many months backward from lhs to rhs", swap the order.
Final code:
rust
impl Sub<Month> for Month {
type Output = Self;
fn sub(self, other: Month) -> Self {
let diff = ((other.value() as i32 - self.value() as i32 + 12) % 12) as u8;
let result = if diff == 0 { 12 } else { diff };
Month::new(result)
}
}