diff options
| author | superwhiskers <[email protected]> | 2025-12-17 21:22:37 -0600 |
|---|---|---|
| committer | superwhiskers <[email protected]> | 2026-01-04 22:23:01 -0600 |
| commit | 54e988aa3d31fb21d3397758f4b71d084e1a1130 (patch) | |
| tree | 8cef7d5a61946a1c90707e60e5022a11022f421d /crates/core/src/utilities.rs | |
| parent | e12b1f4459aee80ee333e90e3b56a3b09f81ae3e (diff) | |
| download | azimuth-canon.tar.gz azimuth-canon.tar.bz2 azimuth-canon.zip | |
Change-Id: I32b78b3eee68205032591578fca70c366a6a6964
Diffstat (limited to 'crates/core/src/utilities.rs')
| -rw-r--r-- | crates/core/src/utilities.rs | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/crates/core/src/utilities.rs b/crates/core/src/utilities.rs index 583ad7c..b6bb86d 100644 --- a/crates/core/src/utilities.rs +++ b/crates/core/src/utilities.rs @@ -34,7 +34,7 @@ where } /// Extension trait for wrapping arithmetic. -pub trait WrappingArithmeticExt { +pub const trait WrappingArithmeticExt { /// Wrapping integer increment. fn wrapping_increment(self) -> Self; @@ -50,7 +50,7 @@ pub trait WrappingArithmeticExt { macro_rules! wrapping_arithmetic_ext_impl { ($t:ty) => { - impl WrappingArithmeticExt for $t { + impl const WrappingArithmeticExt for $t { fn wrapping_increment(self) -> Self { self.wrapping_add(1) } @@ -84,7 +84,7 @@ wrapping_arithmetic_ext_impl!(i8); /// Extension trait for checked arithmetic that turns the output into a /// [`Result`]. -pub trait CheckedArithmeticExt: Sized { +pub const trait CheckedArithmeticExt: Sized { /// Checked integer addition. /// /// # Errors @@ -92,6 +92,13 @@ pub trait CheckedArithmeticExt: Sized { /// Returns an error if the result would have overflowed. fn errored_add(self, rhs: Self) -> Result<Self, IntegerOverflowError>; + /// Checked integer decrement. + /// + /// # Errors + /// + /// Returns an error if the result would have overflowed. + fn errored_decrement(self) -> Result<Self, IntegerOverflowError>; + /// Checked integer division. /// /// # Errors @@ -99,6 +106,13 @@ pub trait CheckedArithmeticExt: Sized { /// Returns an error if the result would have overflowed. fn errored_div(self, rhs: Self) -> Result<Self, IntegerOverflowError>; + /// Checked integer increment. + /// + /// # Errors + /// + /// Returns an error if the result would have overflowed. + fn errored_increment(self) -> Result<Self, IntegerOverflowError>; + /// Checked integer multiplication. /// /// # Errors @@ -123,7 +137,7 @@ pub trait CheckedArithmeticExt: Sized { macro_rules! checked_arithmetic_ext_impl { ($t:ty) => { - impl CheckedArithmeticExt for $t { + impl const CheckedArithmeticExt for $t { fn errored_add( self, rhs: Self, @@ -131,6 +145,10 @@ macro_rules! checked_arithmetic_ext_impl { self.checked_add(rhs).ok_or(IntegerOverflowError) } + fn errored_decrement(self) -> Result<Self, IntegerOverflowError> { + self.errored_sub(1) + } + fn errored_div( self, rhs: Self, @@ -138,6 +156,10 @@ macro_rules! checked_arithmetic_ext_impl { self.checked_div(rhs).ok_or(IntegerOverflowError) } + fn errored_increment(self) -> Result<Self, IntegerOverflowError> { + self.errored_add(1) + } + fn errored_mul( self, rhs: Self, @@ -175,7 +197,7 @@ checked_arithmetic_ext_impl!(i16); checked_arithmetic_ext_impl!(i8); /// Representation of an integer overflow error. -#[derive(PartialEq, Eq)] +#[derive(Copy, Clone, PartialEq, Eq)] #[cfg_attr(feature = "core-fmt", derive(Debug))] pub struct IntegerOverflowError; |
