diff options
| -rw-r--r-- | src/year.rs | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/src/year.rs b/src/year.rs index 22627dd..76a42fe 100644 --- a/src/year.rs +++ b/src/year.rs @@ -2,16 +2,16 @@ use core::ops::{Add, AddAssign, Sub, SubAssign}; use derive_more::{Display, FromStr}; -/// A year value type, stored as an i32 +/// A year value type, stored as an i16 #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, FromStr, Display)] -pub struct Year(i32); +pub struct Year(i16); impl Year { /// The latest year that can be represented - pub const MAX: Self = Self(i32::MAX); + pub const MAX: Self = Self(i16::MAX); /// The earliest year that can be represented - pub const MIN: Self = Self(i32::MIN); + pub const MIN: Self = Self(i16::MIN); /// An equivalent of `Year::from(i32)`, which can be run at compile-time /// @@ -23,7 +23,7 @@ impl Year { /// const YEAR: Year = Year::from_i32(2021); /// assert_eq!(2021, YEAR.as_i32()); /// ``` - pub const fn from_i32(i: i32) -> Self { + pub const fn from_i32(i: i16) -> Self { Self(i) } @@ -38,7 +38,7 @@ impl Year { /// const YEAR_INT: i32 = YEAR.as_i32(); /// assert_eq!(2021, YEAR_INT); /// ``` - pub const fn as_i32(self) -> i32 { + pub const fn as_i32(self) -> i16 { self.0 } @@ -53,7 +53,7 @@ impl Year { /// assert_eq!(Some(Year::from(2022)), Year::from_i32(2021).checked_add(1)); /// assert_eq!(None, Year::MAX.checked_add(1)); /// ``` - pub const fn checked_add(self, rhs: i32) -> Option<Year> { + pub const fn checked_add(self, rhs: i16) -> Option<Year> { match self.0.checked_add(rhs) { Some(year) => Some(Self(year)), None => None, @@ -74,7 +74,7 @@ impl Year { /// assert_eq!((Year::from(2022), false), Year::from(2021).overflowing_add(1)); /// assert_eq!((Year::MIN, true), Year::MAX.overflowing_add(1)); /// ``` - pub const fn overflowing_add(self, rhs: i32) -> (Year, bool) { + pub const fn overflowing_add(self, rhs: i16) -> (Year, bool) { let int_result = self.0.overflowing_add(rhs); (Year(int_result.0), int_result.1) } @@ -90,7 +90,7 @@ impl Year { /// assert_eq!(Year::from(2022), Year::from(2021).saturating_add(1)); /// assert_eq!(Year::MAX, Year::MAX.saturating_add(1)); /// ``` - pub const fn saturating_add(self, rhs: i32) -> Year { + pub const fn saturating_add(self, rhs: i16) -> Year { Year(self.0.saturating_add(rhs)) } @@ -104,7 +104,7 @@ impl Year { /// /// assert_eq!(Year::from(2022), Year::from(2021).wrapping_add(1)); /// assert_eq!(Year::MIN, Year::MAX.wrapping_add(1)); - pub const fn wrapping_add(self, rhs: i32) -> Year { + pub const fn wrapping_add(self, rhs: i16) -> Year { Year(self.0.wrapping_add(rhs)) } @@ -119,7 +119,7 @@ impl Year { /// assert_eq!(Some(Year::from(2020)), Year::from_i32(2021).checked_sub(1)); /// assert_eq!(None, Year::MIN.checked_sub(1)); /// ``` - pub const fn checked_sub(self, rhs: i32) -> Option<Year> { + pub const fn checked_sub(self, rhs: i16) -> Option<Year> { match self.0.checked_sub(rhs) { Some(year) => Some(Self(year)), None => None, @@ -140,7 +140,7 @@ impl Year { /// assert_eq!((Year::from(2020), false), Year::from(2021).overflowing_sub(1)); /// assert_eq!((Year::MAX, true), Year::MIN.overflowing_sub(1)); /// ``` - pub const fn overflowing_sub(self, rhs: i32) -> (Year, bool) { + pub const fn overflowing_sub(self, rhs: i16) -> (Year, bool) { let int_result = self.0.overflowing_sub(rhs); (Year(int_result.0), int_result.1) } @@ -156,7 +156,7 @@ impl Year { /// assert_eq!(Year::from(2020), Year::from(2021).saturating_sub(1)); /// assert_eq!(Year::MIN, Year::MIN.saturating_sub(1)); /// ``` - pub const fn saturating_sub(self, rhs: i32) -> Year { + pub const fn saturating_sub(self, rhs: i16) -> Year { Year(self.0.saturating_sub(rhs)) } @@ -170,24 +170,24 @@ impl Year { /// /// assert_eq!(Year::from(2020), Year::from(2021).wrapping_sub(1)); /// assert_eq!(Year::MAX, Year::MIN.wrapping_sub(1)); - pub const fn wrapping_sub(self, rhs: i32) -> Year { + pub const fn wrapping_sub(self, rhs: i16) -> Year { Year(self.0.wrapping_sub(rhs)) } } -impl From<i32> for Year { - fn from(i: i32) -> Self { +impl From<i16> for Year { + fn from(i: i16) -> Self { Self(i) } } -impl From<Year> for i32 { +impl From<Year> for i16 { fn from(year: Year) -> Self { year.0 } } -impl<I: Into<i32>> Add<I> for Year { +impl<I: Into<i16>> Add<I> for Year { type Output = Self; fn add(self, rhs: I) -> Self::Output { @@ -195,7 +195,7 @@ impl<I: Into<i32>> Add<I> for Year { } } -impl<I: Into<i32>> Sub<I> for Year { +impl<I: Into<i16>> Sub<I> for Year { type Output = Self; fn sub(self, rhs: I) -> Self::Output { @@ -203,14 +203,14 @@ impl<I: Into<i32>> Sub<I> for Year { } } -impl AddAssign<i32> for Year { - fn add_assign(&mut self, rhs: i32) { +impl AddAssign<i16> for Year { + fn add_assign(&mut self, rhs: i16) { self.0 = self.0 + rhs } } -impl SubAssign<i32> for Year { - fn sub_assign(&mut self, rhs: i32) { +impl SubAssign<i16> for Year { + fn sub_assign(&mut self, rhs: i16) { self.0 = self.0 - rhs } } |
