2025-01-16 12:04:31 +00:00
|
|
|
use num_traits::ops::checked::{CheckedAdd, CheckedDiv, CheckedMul, CheckedRem, CheckedSub};
|
|
|
|
|
|
2025-02-23 01:17:45 -05:00
|
|
|
use crate::{Result, checked};
|
2025-01-16 12:04:31 +00:00
|
|
|
|
|
|
|
|
pub trait Tried {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn try_add(self, rhs: Self) -> Result<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: CheckedAdd + Sized,
|
|
|
|
|
{
|
|
|
|
|
checked!(self + rhs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn try_sub(self, rhs: Self) -> Result<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: CheckedSub + Sized,
|
|
|
|
|
{
|
|
|
|
|
checked!(self - rhs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn try_mul(self, rhs: Self) -> Result<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: CheckedMul + Sized,
|
|
|
|
|
{
|
|
|
|
|
checked!(self * rhs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn try_div(self, rhs: Self) -> Result<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: CheckedDiv + Sized,
|
|
|
|
|
{
|
|
|
|
|
checked!(self / rhs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn try_rem(self, rhs: Self) -> Result<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: CheckedRem + Sized,
|
|
|
|
|
{
|
|
|
|
|
checked!(self % rhs)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Tried for T {}
|