#[cfg(feature = "std")]
use std::time::{Instant, SystemTime};
use {
crate::{Error, MaybeParens},
alloc::format,
core::{num::NonZero, time::Duration},
};
pub trait Cadd: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cadd(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
fn cadd_assign(&mut self, other: Self::Other) -> Result<(), Self::Error>;
}
#[doc(alias = "checked_add")]
#[inline]
pub fn cadd<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: Cadd,
{
a.cadd(b)
}
impl Cadd for NonZero<u8> {
type Other = u8;
type Output = NonZero<u8>;
type Error = Error;
#[inline]
fn cadd(self, other: u8) -> Result<NonZero<u8>, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<u8>"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: u8) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for NonZero<u16> {
type Other = u16;
type Output = NonZero<u16>;
type Error = Error;
#[inline]
fn cadd(self, other: u16) -> Result<NonZero<u16>, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<u16>"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: u16) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for NonZero<u32> {
type Other = u32;
type Output = NonZero<u32>;
type Error = Error;
#[inline]
fn cadd(self, other: u32) -> Result<NonZero<u32>, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<u32>"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for NonZero<u64> {
type Other = u64;
type Output = NonZero<u64>;
type Error = Error;
#[inline]
fn cadd(self, other: u64) -> Result<NonZero<u64>, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<u64>"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: u64) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for NonZero<u128> {
type Other = u128;
type Output = NonZero<u128>;
type Error = Error;
#[inline]
fn cadd(self, other: u128) -> Result<NonZero<u128>, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<u128>"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: u128) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for NonZero<usize> {
type Other = usize;
type Output = NonZero<usize>;
type Error = Error;
#[inline]
fn cadd(self, other: usize) -> Result<NonZero<usize>, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<usize>"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: usize) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for i8 {
type Other = i8;
type Output = i8;
type Error = Error;
#[inline]
fn cadd(self, other: i8) -> Result<i8, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"i8"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: i8) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for i16 {
type Other = i16;
type Output = i16;
type Error = Error;
#[inline]
fn cadd(self, other: i16) -> Result<i16, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"i16"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: i16) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for i32 {
type Other = i32;
type Output = i32;
type Error = Error;
#[inline]
fn cadd(self, other: i32) -> Result<i32, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"i32"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: i32) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for i64 {
type Other = i64;
type Output = i64;
type Error = Error;
#[inline]
fn cadd(self, other: i64) -> Result<i64, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"i64"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: i64) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for i128 {
type Other = i128;
type Output = i128;
type Error = Error;
#[inline]
fn cadd(self, other: i128) -> Result<i128, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"i128"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: i128) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for isize {
type Other = isize;
type Output = isize;
type Error = Error;
#[inline]
fn cadd(self, other: isize) -> Result<isize, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"isize"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: isize) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for u8 {
type Other = u8;
type Output = u8;
type Error = Error;
#[inline]
fn cadd(self, other: u8) -> Result<u8, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"u8"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: u8) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for u16 {
type Other = u16;
type Output = u16;
type Error = Error;
#[inline]
fn cadd(self, other: u16) -> Result<u16, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"u16"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: u16) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for u32 {
type Other = u32;
type Output = u32;
type Error = Error;
#[inline]
fn cadd(self, other: u32) -> Result<u32, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"u32"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for u64 {
type Other = u64;
type Output = u64;
type Error = Error;
#[inline]
fn cadd(self, other: u64) -> Result<u64, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"u64"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: u64) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for u128 {
type Other = u128;
type Output = u128;
type Error = Error;
#[inline]
fn cadd(self, other: u128) -> Result<u128, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"u128"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: u128) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for usize {
type Other = usize;
type Output = usize;
type Error = Error;
#[inline]
fn cadd(self, other: usize) -> Result<usize, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"usize"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: usize) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
impl Cadd for Duration {
type Other = Duration;
type Output = Duration;
type Error = Error;
#[inline]
fn cadd(self, other: Duration) -> Result<Duration, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"Duration"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
#[cfg(feature = "std")]
impl Cadd for Instant {
type Other = Duration;
type Output = Instant;
type Error = Error;
#[inline]
fn cadd(self, other: Duration) -> Result<Instant, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"Instant"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
#[cfg(feature = "std")]
impl Cadd for SystemTime {
type Other = Duration;
type Output = SystemTime;
type Error = Error;
#[inline]
fn cadd(self, other: Duration) -> Result<SystemTime, Error> {
self.checked_add(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} + {:?}: {} overflow",
self,
MaybeParens(other),
"SystemTime"
))
})
}
#[inline]
fn cadd_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
*self = self.cadd(other)?;
Ok(())
}
}
pub trait CaddUnsigned: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cadd_unsigned(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_add_unsigned")]
#[inline]
pub fn cadd_unsigned<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: CaddUnsigned,
{
a.cadd_unsigned(b)
}
impl CaddUnsigned for i8 {
type Other = u8;
type Output = i8;
type Error = Error;
#[inline]
fn cadd_unsigned(self, other: u8) -> Result<i8, Error> {
self.checked_add_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_unsigned({:?}, {:?}): {} overflow",
self, other, "i8"
))
})
}
}
impl CaddUnsigned for i16 {
type Other = u16;
type Output = i16;
type Error = Error;
#[inline]
fn cadd_unsigned(self, other: u16) -> Result<i16, Error> {
self.checked_add_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_unsigned({:?}, {:?}): {} overflow",
self, other, "i16"
))
})
}
}
impl CaddUnsigned for i32 {
type Other = u32;
type Output = i32;
type Error = Error;
#[inline]
fn cadd_unsigned(self, other: u32) -> Result<i32, Error> {
self.checked_add_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_unsigned({:?}, {:?}): {} overflow",
self, other, "i32"
))
})
}
}
impl CaddUnsigned for i64 {
type Other = u64;
type Output = i64;
type Error = Error;
#[inline]
fn cadd_unsigned(self, other: u64) -> Result<i64, Error> {
self.checked_add_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_unsigned({:?}, {:?}): {} overflow",
self, other, "i64"
))
})
}
}
impl CaddUnsigned for i128 {
type Other = u128;
type Output = i128;
type Error = Error;
#[inline]
fn cadd_unsigned(self, other: u128) -> Result<i128, Error> {
self.checked_add_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_unsigned({:?}, {:?}): {} overflow",
self, other, "i128"
))
})
}
}
impl CaddUnsigned for isize {
type Other = usize;
type Output = isize;
type Error = Error;
#[inline]
fn cadd_unsigned(self, other: usize) -> Result<isize, Error> {
self.checked_add_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_unsigned({:?}, {:?}): {} overflow",
self, other, "isize"
))
})
}
}
pub trait CaddSigned: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cadd_signed(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_add_signed")]
#[inline]
pub fn cadd_signed<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: CaddSigned,
{
a.cadd_signed(b)
}
impl CaddSigned for u8 {
type Other = i8;
type Output = u8;
type Error = Error;
#[inline]
fn cadd_signed(self, other: i8) -> Result<u8, Error> {
self.checked_add_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_signed({:?}, {:?}): {} overflow",
self, other, "u8"
))
})
}
}
impl CaddSigned for u16 {
type Other = i16;
type Output = u16;
type Error = Error;
#[inline]
fn cadd_signed(self, other: i16) -> Result<u16, Error> {
self.checked_add_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_signed({:?}, {:?}): {} overflow",
self, other, "u16"
))
})
}
}
impl CaddSigned for u32 {
type Other = i32;
type Output = u32;
type Error = Error;
#[inline]
fn cadd_signed(self, other: i32) -> Result<u32, Error> {
self.checked_add_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_signed({:?}, {:?}): {} overflow",
self, other, "u32"
))
})
}
}
impl CaddSigned for u64 {
type Other = i64;
type Output = u64;
type Error = Error;
#[inline]
fn cadd_signed(self, other: i64) -> Result<u64, Error> {
self.checked_add_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_signed({:?}, {:?}): {} overflow",
self, other, "u64"
))
})
}
}
impl CaddSigned for u128 {
type Other = i128;
type Output = u128;
type Error = Error;
#[inline]
fn cadd_signed(self, other: i128) -> Result<u128, Error> {
self.checked_add_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_signed({:?}, {:?}): {} overflow",
self, other, "u128"
))
})
}
}
impl CaddSigned for usize {
type Other = isize;
type Output = usize;
type Error = Error;
#[inline]
fn cadd_signed(self, other: isize) -> Result<usize, Error> {
self.checked_add_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute add_signed({:?}, {:?}): {} overflow",
self, other, "usize"
))
})
}
}
pub trait Csub: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn csub(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
fn csub_assign(&mut self, other: Self::Other) -> Result<(), Self::Error>;
}
#[doc(alias = "checked_sub")]
#[inline]
pub fn csub<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: Csub,
{
a.csub(b)
}
impl Csub for i8 {
type Other = i8;
type Output = i8;
type Error = Error;
#[inline]
fn csub(self, other: i8) -> Result<i8, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"i8"
))
})
}
#[inline]
fn csub_assign(&mut self, other: i8) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for i16 {
type Other = i16;
type Output = i16;
type Error = Error;
#[inline]
fn csub(self, other: i16) -> Result<i16, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"i16"
))
})
}
#[inline]
fn csub_assign(&mut self, other: i16) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for i32 {
type Other = i32;
type Output = i32;
type Error = Error;
#[inline]
fn csub(self, other: i32) -> Result<i32, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"i32"
))
})
}
#[inline]
fn csub_assign(&mut self, other: i32) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for i64 {
type Other = i64;
type Output = i64;
type Error = Error;
#[inline]
fn csub(self, other: i64) -> Result<i64, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"i64"
))
})
}
#[inline]
fn csub_assign(&mut self, other: i64) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for i128 {
type Other = i128;
type Output = i128;
type Error = Error;
#[inline]
fn csub(self, other: i128) -> Result<i128, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"i128"
))
})
}
#[inline]
fn csub_assign(&mut self, other: i128) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for isize {
type Other = isize;
type Output = isize;
type Error = Error;
#[inline]
fn csub(self, other: isize) -> Result<isize, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"isize"
))
})
}
#[inline]
fn csub_assign(&mut self, other: isize) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for u8 {
type Other = u8;
type Output = u8;
type Error = Error;
#[inline]
fn csub(self, other: u8) -> Result<u8, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"u8"
))
})
}
#[inline]
fn csub_assign(&mut self, other: u8) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for u16 {
type Other = u16;
type Output = u16;
type Error = Error;
#[inline]
fn csub(self, other: u16) -> Result<u16, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"u16"
))
})
}
#[inline]
fn csub_assign(&mut self, other: u16) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for u32 {
type Other = u32;
type Output = u32;
type Error = Error;
#[inline]
fn csub(self, other: u32) -> Result<u32, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"u32"
))
})
}
#[inline]
fn csub_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for u64 {
type Other = u64;
type Output = u64;
type Error = Error;
#[inline]
fn csub(self, other: u64) -> Result<u64, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"u64"
))
})
}
#[inline]
fn csub_assign(&mut self, other: u64) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for u128 {
type Other = u128;
type Output = u128;
type Error = Error;
#[inline]
fn csub(self, other: u128) -> Result<u128, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"u128"
))
})
}
#[inline]
fn csub_assign(&mut self, other: u128) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for usize {
type Other = usize;
type Output = usize;
type Error = Error;
#[inline]
fn csub(self, other: usize) -> Result<usize, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"usize"
))
})
}
#[inline]
fn csub_assign(&mut self, other: usize) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
impl Csub for Duration {
type Other = Duration;
type Output = Duration;
type Error = Error;
#[inline]
fn csub(self, other: Duration) -> Result<Duration, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"Duration"
))
})
}
#[inline]
fn csub_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
#[cfg(feature = "std")]
impl Csub for Instant {
type Other = Duration;
type Output = Instant;
type Error = Error;
#[inline]
fn csub(self, other: Duration) -> Result<Instant, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"Instant"
))
})
}
#[inline]
fn csub_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
#[cfg(feature = "std")]
impl Csub for SystemTime {
type Other = Duration;
type Output = SystemTime;
type Error = Error;
#[inline]
fn csub(self, other: Duration) -> Result<SystemTime, Error> {
self.checked_sub(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} - {:?}: {} overflow",
self,
MaybeParens(other),
"SystemTime"
))
})
}
#[inline]
fn csub_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
*self = self.csub(other)?;
Ok(())
}
}
pub trait CsubUnsigned: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn csub_unsigned(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_sub_unsigned")]
#[inline]
pub fn csub_unsigned<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: CsubUnsigned,
{
a.csub_unsigned(b)
}
impl CsubUnsigned for i8 {
type Other = u8;
type Output = i8;
type Error = Error;
#[inline]
fn csub_unsigned(self, other: u8) -> Result<i8, Error> {
self.checked_sub_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_unsigned({:?}, {:?}): {} overflow",
self, other, "i8"
))
})
}
}
impl CsubUnsigned for i16 {
type Other = u16;
type Output = i16;
type Error = Error;
#[inline]
fn csub_unsigned(self, other: u16) -> Result<i16, Error> {
self.checked_sub_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_unsigned({:?}, {:?}): {} overflow",
self, other, "i16"
))
})
}
}
impl CsubUnsigned for i32 {
type Other = u32;
type Output = i32;
type Error = Error;
#[inline]
fn csub_unsigned(self, other: u32) -> Result<i32, Error> {
self.checked_sub_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_unsigned({:?}, {:?}): {} overflow",
self, other, "i32"
))
})
}
}
impl CsubUnsigned for i64 {
type Other = u64;
type Output = i64;
type Error = Error;
#[inline]
fn csub_unsigned(self, other: u64) -> Result<i64, Error> {
self.checked_sub_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_unsigned({:?}, {:?}): {} overflow",
self, other, "i64"
))
})
}
}
impl CsubUnsigned for i128 {
type Other = u128;
type Output = i128;
type Error = Error;
#[inline]
fn csub_unsigned(self, other: u128) -> Result<i128, Error> {
self.checked_sub_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_unsigned({:?}, {:?}): {} overflow",
self, other, "i128"
))
})
}
}
impl CsubUnsigned for isize {
type Other = usize;
type Output = isize;
type Error = Error;
#[inline]
fn csub_unsigned(self, other: usize) -> Result<isize, Error> {
self.checked_sub_unsigned(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_unsigned({:?}, {:?}): {} overflow",
self, other, "isize"
))
})
}
}
pub trait CsubSigned: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn csub_signed(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_sub_signed")]
#[inline]
pub fn csub_signed<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: CsubSigned,
{
a.csub_signed(b)
}
impl CsubSigned for u8 {
type Other = i8;
type Output = u8;
type Error = Error;
#[inline]
fn csub_signed(self, other: i8) -> Result<u8, Error> {
self.checked_sub_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_signed({:?}, {:?}): {} overflow",
self, other, "u8"
))
})
}
}
impl CsubSigned for u16 {
type Other = i16;
type Output = u16;
type Error = Error;
#[inline]
fn csub_signed(self, other: i16) -> Result<u16, Error> {
self.checked_sub_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_signed({:?}, {:?}): {} overflow",
self, other, "u16"
))
})
}
}
impl CsubSigned for u32 {
type Other = i32;
type Output = u32;
type Error = Error;
#[inline]
fn csub_signed(self, other: i32) -> Result<u32, Error> {
self.checked_sub_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_signed({:?}, {:?}): {} overflow",
self, other, "u32"
))
})
}
}
impl CsubSigned for u64 {
type Other = i64;
type Output = u64;
type Error = Error;
#[inline]
fn csub_signed(self, other: i64) -> Result<u64, Error> {
self.checked_sub_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_signed({:?}, {:?}): {} overflow",
self, other, "u64"
))
})
}
}
impl CsubSigned for u128 {
type Other = i128;
type Output = u128;
type Error = Error;
#[inline]
fn csub_signed(self, other: i128) -> Result<u128, Error> {
self.checked_sub_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_signed({:?}, {:?}): {} overflow",
self, other, "u128"
))
})
}
}
impl CsubSigned for usize {
type Other = isize;
type Output = usize;
type Error = Error;
#[inline]
fn csub_signed(self, other: isize) -> Result<usize, Error> {
self.checked_sub_signed(other).ok_or_else(|| {
Error::new(format!(
"failed to compute sub_signed({:?}, {:?}): {} overflow",
self, other, "usize"
))
})
}
}
pub trait CsignedDiff: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn csigned_diff(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_signed_diff")]
#[inline]
pub fn csigned_diff<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: CsignedDiff,
{
a.csigned_diff(b)
}
impl CsignedDiff for u8 {
type Other = u8;
type Output = i8;
type Error = Error;
#[inline]
fn csigned_diff(self, other: u8) -> Result<i8, Error> {
self.checked_signed_diff(other).ok_or_else(|| {
Error::new(format!(
"failed to compute signed_diff({:?}, {:?}): {} overflow",
self, other, "i8"
))
})
}
}
impl CsignedDiff for u16 {
type Other = u16;
type Output = i16;
type Error = Error;
#[inline]
fn csigned_diff(self, other: u16) -> Result<i16, Error> {
self.checked_signed_diff(other).ok_or_else(|| {
Error::new(format!(
"failed to compute signed_diff({:?}, {:?}): {} overflow",
self, other, "i16"
))
})
}
}
impl CsignedDiff for u32 {
type Other = u32;
type Output = i32;
type Error = Error;
#[inline]
fn csigned_diff(self, other: u32) -> Result<i32, Error> {
self.checked_signed_diff(other).ok_or_else(|| {
Error::new(format!(
"failed to compute signed_diff({:?}, {:?}): {} overflow",
self, other, "i32"
))
})
}
}
impl CsignedDiff for u64 {
type Other = u64;
type Output = i64;
type Error = Error;
#[inline]
fn csigned_diff(self, other: u64) -> Result<i64, Error> {
self.checked_signed_diff(other).ok_or_else(|| {
Error::new(format!(
"failed to compute signed_diff({:?}, {:?}): {} overflow",
self, other, "i64"
))
})
}
}
impl CsignedDiff for u128 {
type Other = u128;
type Output = i128;
type Error = Error;
#[inline]
fn csigned_diff(self, other: u128) -> Result<i128, Error> {
self.checked_signed_diff(other).ok_or_else(|| {
Error::new(format!(
"failed to compute signed_diff({:?}, {:?}): {} overflow",
self, other, "i128"
))
})
}
}
impl CsignedDiff for usize {
type Other = usize;
type Output = isize;
type Error = Error;
#[inline]
fn csigned_diff(self, other: usize) -> Result<isize, Error> {
self.checked_signed_diff(other).ok_or_else(|| {
Error::new(format!(
"failed to compute signed_diff({:?}, {:?}): {} overflow",
self, other, "isize"
))
})
}
}
pub trait Cneg: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cneg(self) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_neg")]
#[inline]
pub fn cneg<T>(value: T) -> Result<T::Output, T::Error>
where
T: Cneg,
{
Cneg::cneg(value)
}
impl Cneg for NonZero<i8> {
type Output = NonZero<i8>;
type Error = Error;
#[inline]
fn cneg(self) -> Result<NonZero<i8>, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "NonZero<i8>"
))
})
}
}
impl Cneg for NonZero<i16> {
type Output = NonZero<i16>;
type Error = Error;
#[inline]
fn cneg(self) -> Result<NonZero<i16>, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "NonZero<i16>"
))
})
}
}
impl Cneg for NonZero<i32> {
type Output = NonZero<i32>;
type Error = Error;
#[inline]
fn cneg(self) -> Result<NonZero<i32>, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "NonZero<i32>"
))
})
}
}
impl Cneg for NonZero<i64> {
type Output = NonZero<i64>;
type Error = Error;
#[inline]
fn cneg(self) -> Result<NonZero<i64>, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "NonZero<i64>"
))
})
}
}
impl Cneg for NonZero<i128> {
type Output = NonZero<i128>;
type Error = Error;
#[inline]
fn cneg(self) -> Result<NonZero<i128>, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "NonZero<i128>"
))
})
}
}
impl Cneg for NonZero<isize> {
type Output = NonZero<isize>;
type Error = Error;
#[inline]
fn cneg(self) -> Result<NonZero<isize>, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "NonZero<isize>"
))
})
}
}
impl Cneg for i8 {
type Output = i8;
type Error = Error;
#[inline]
fn cneg(self) -> Result<i8, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "i8"
))
})
}
}
impl Cneg for i16 {
type Output = i16;
type Error = Error;
#[inline]
fn cneg(self) -> Result<i16, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "i16"
))
})
}
}
impl Cneg for i32 {
type Output = i32;
type Error = Error;
#[inline]
fn cneg(self) -> Result<i32, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "i32"
))
})
}
}
impl Cneg for i64 {
type Output = i64;
type Error = Error;
#[inline]
fn cneg(self) -> Result<i64, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "i64"
))
})
}
}
impl Cneg for i128 {
type Output = i128;
type Error = Error;
#[inline]
fn cneg(self) -> Result<i128, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "i128"
))
})
}
}
impl Cneg for isize {
type Output = isize;
type Error = Error;
#[inline]
fn cneg(self) -> Result<isize, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "isize"
))
})
}
}
impl Cneg for u8 {
type Output = u8;
type Error = Error;
#[inline]
fn cneg(self) -> Result<u8, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "u8"
))
})
}
}
impl Cneg for u16 {
type Output = u16;
type Error = Error;
#[inline]
fn cneg(self) -> Result<u16, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "u16"
))
})
}
}
impl Cneg for u32 {
type Output = u32;
type Error = Error;
#[inline]
fn cneg(self) -> Result<u32, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "u32"
))
})
}
}
impl Cneg for u64 {
type Output = u64;
type Error = Error;
#[inline]
fn cneg(self) -> Result<u64, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "u64"
))
})
}
}
impl Cneg for u128 {
type Output = u128;
type Error = Error;
#[inline]
fn cneg(self) -> Result<u128, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "u128"
))
})
}
}
impl Cneg for usize {
type Output = usize;
type Error = Error;
#[inline]
fn cneg(self) -> Result<usize, Error> {
self.checked_neg().ok_or_else(|| {
Error::new(format!(
"failed to compute -({:?}): {} overflow",
self, "usize"
))
})
}
}
pub trait Cmul: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cmul(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
fn cmul_assign(&mut self, other: Self::Other) -> Result<(), Self::Error>;
}
#[doc(alias = "checked_mul")]
#[inline]
pub fn cmul<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: Cmul,
{
a.cmul(b)
}
impl Cmul for NonZero<u8> {
type Other = NonZero<u8>;
type Output = NonZero<u8>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<u8>) -> Result<NonZero<u8>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<u8>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<u8>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<u16> {
type Other = NonZero<u16>;
type Output = NonZero<u16>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<u16>) -> Result<NonZero<u16>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<u16>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<u16>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<u32> {
type Other = NonZero<u32>;
type Output = NonZero<u32>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<u32>) -> Result<NonZero<u32>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<u32>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<u32>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<u64> {
type Other = NonZero<u64>;
type Output = NonZero<u64>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<u64>) -> Result<NonZero<u64>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<u64>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<u64>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<u128> {
type Other = NonZero<u128>;
type Output = NonZero<u128>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<u128>) -> Result<NonZero<u128>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<u128>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<u128>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<usize> {
type Other = NonZero<usize>;
type Output = NonZero<usize>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<usize>) -> Result<NonZero<usize>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<usize>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<usize>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<i8> {
type Other = NonZero<i8>;
type Output = NonZero<i8>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<i8>) -> Result<NonZero<i8>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<i8>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<i8>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<i16> {
type Other = NonZero<i16>;
type Output = NonZero<i16>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<i16>) -> Result<NonZero<i16>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<i16>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<i16>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<i32> {
type Other = NonZero<i32>;
type Output = NonZero<i32>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<i32>) -> Result<NonZero<i32>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<i32>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<i32>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<i64> {
type Other = NonZero<i64>;
type Output = NonZero<i64>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<i64>) -> Result<NonZero<i64>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<i64>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<i64>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<i128> {
type Other = NonZero<i128>;
type Output = NonZero<i128>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<i128>) -> Result<NonZero<i128>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<i128>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<i128>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for NonZero<isize> {
type Other = NonZero<isize>;
type Output = NonZero<isize>;
type Error = Error;
#[inline]
fn cmul(self, other: NonZero<isize>) -> Result<NonZero<isize>, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"NonZero<isize>"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: NonZero<isize>) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for i8 {
type Other = i8;
type Output = i8;
type Error = Error;
#[inline]
fn cmul(self, other: i8) -> Result<i8, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"i8"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: i8) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for i16 {
type Other = i16;
type Output = i16;
type Error = Error;
#[inline]
fn cmul(self, other: i16) -> Result<i16, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"i16"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: i16) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for i32 {
type Other = i32;
type Output = i32;
type Error = Error;
#[inline]
fn cmul(self, other: i32) -> Result<i32, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"i32"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: i32) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for i64 {
type Other = i64;
type Output = i64;
type Error = Error;
#[inline]
fn cmul(self, other: i64) -> Result<i64, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"i64"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: i64) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for i128 {
type Other = i128;
type Output = i128;
type Error = Error;
#[inline]
fn cmul(self, other: i128) -> Result<i128, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"i128"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: i128) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for isize {
type Other = isize;
type Output = isize;
type Error = Error;
#[inline]
fn cmul(self, other: isize) -> Result<isize, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"isize"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: isize) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for u8 {
type Other = u8;
type Output = u8;
type Error = Error;
#[inline]
fn cmul(self, other: u8) -> Result<u8, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"u8"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: u8) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for u16 {
type Other = u16;
type Output = u16;
type Error = Error;
#[inline]
fn cmul(self, other: u16) -> Result<u16, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"u16"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: u16) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for u32 {
type Other = u32;
type Output = u32;
type Error = Error;
#[inline]
fn cmul(self, other: u32) -> Result<u32, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"u32"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for u64 {
type Other = u64;
type Output = u64;
type Error = Error;
#[inline]
fn cmul(self, other: u64) -> Result<u64, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"u64"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: u64) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for u128 {
type Other = u128;
type Output = u128;
type Error = Error;
#[inline]
fn cmul(self, other: u128) -> Result<u128, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"u128"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: u128) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for usize {
type Other = usize;
type Output = usize;
type Error = Error;
#[inline]
fn cmul(self, other: usize) -> Result<usize, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"usize"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: usize) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
impl Cmul for Duration {
type Other = u32;
type Output = Duration;
type Error = Error;
#[inline]
fn cmul(self, other: u32) -> Result<Duration, Error> {
self.checked_mul(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} * {:?}: {} overflow",
self,
MaybeParens(other),
"Duration"
))
})
}
#[inline]
fn cmul_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cmul(other)?;
Ok(())
}
}
pub trait Cdiv: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Divisor;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cdiv(self, divisor: Self::Divisor) -> Result<Self::Output, Self::Error>;
fn cdiv_assign(&mut self, divisor: Self::Divisor) -> Result<(), Self::Error>;
}
#[doc(alias = "checked_div")]
#[inline]
pub fn cdiv<T>(value: T, divisor: T::Divisor) -> Result<T::Output, T::Error>
where
T: Cdiv,
{
value.cdiv(divisor)
}
impl Cdiv for i8 {
type Divisor = i8;
type Output = i8;
type Error = Error;
#[inline]
fn cdiv(self, divisor: i8) -> Result<i8, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"i8"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: i8) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for i16 {
type Divisor = i16;
type Output = i16;
type Error = Error;
#[inline]
fn cdiv(self, divisor: i16) -> Result<i16, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"i16"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: i16) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for i32 {
type Divisor = i32;
type Output = i32;
type Error = Error;
#[inline]
fn cdiv(self, divisor: i32) -> Result<i32, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"i32"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: i32) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for i64 {
type Divisor = i64;
type Output = i64;
type Error = Error;
#[inline]
fn cdiv(self, divisor: i64) -> Result<i64, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"i64"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: i64) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for i128 {
type Divisor = i128;
type Output = i128;
type Error = Error;
#[inline]
fn cdiv(self, divisor: i128) -> Result<i128, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"i128"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: i128) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for isize {
type Divisor = isize;
type Output = isize;
type Error = Error;
#[inline]
fn cdiv(self, divisor: isize) -> Result<isize, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"isize"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: isize) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for u8 {
type Divisor = u8;
type Output = u8;
type Error = Error;
#[inline]
fn cdiv(self, divisor: u8) -> Result<u8, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"u8"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: u8) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for u16 {
type Divisor = u16;
type Output = u16;
type Error = Error;
#[inline]
fn cdiv(self, divisor: u16) -> Result<u16, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"u16"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: u16) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for u32 {
type Divisor = u32;
type Output = u32;
type Error = Error;
#[inline]
fn cdiv(self, divisor: u32) -> Result<u32, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"u32"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: u32) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for u64 {
type Divisor = u64;
type Output = u64;
type Error = Error;
#[inline]
fn cdiv(self, divisor: u64) -> Result<u64, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"u64"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: u64) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for u128 {
type Divisor = u128;
type Output = u128;
type Error = Error;
#[inline]
fn cdiv(self, divisor: u128) -> Result<u128, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"u128"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: u128) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for usize {
type Divisor = usize;
type Output = usize;
type Error = Error;
#[inline]
fn cdiv(self, divisor: usize) -> Result<usize, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"usize"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: usize) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
impl Cdiv for Duration {
type Divisor = u32;
type Output = Duration;
type Error = Error;
#[inline]
fn cdiv(self, divisor: u32) -> Result<Duration, Error> {
self.checked_div(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} / {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} / {:?}: {} overflow",
self,
MaybeParens(divisor),
"Duration"
)
}
})
})
}
#[inline]
fn cdiv_assign(&mut self, divisor: u32) -> Result<(), Self::Error> {
*self = self.cdiv(divisor)?;
Ok(())
}
}
pub trait CdivEuclid: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Divisor;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cdiv_euclid(self, divisor: Self::Divisor) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_div_euclid")]
#[inline]
pub fn cdiv_euclid<T>(value: T, divisor: T::Divisor) -> Result<T::Output, T::Error>
where
T: CdivEuclid,
{
value.cdiv_euclid(divisor)
}
impl CdivEuclid for i8 {
type Divisor = i8;
type Output = i8;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: i8) -> Result<i8, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "i8"
)
}
})
})
}
}
impl CdivEuclid for i16 {
type Divisor = i16;
type Output = i16;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: i16) -> Result<i16, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "i16"
)
}
})
})
}
}
impl CdivEuclid for i32 {
type Divisor = i32;
type Output = i32;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: i32) -> Result<i32, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "i32"
)
}
})
})
}
}
impl CdivEuclid for i64 {
type Divisor = i64;
type Output = i64;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: i64) -> Result<i64, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "i64"
)
}
})
})
}
}
impl CdivEuclid for i128 {
type Divisor = i128;
type Output = i128;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: i128) -> Result<i128, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "i128"
)
}
})
})
}
}
impl CdivEuclid for isize {
type Divisor = isize;
type Output = isize;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: isize) -> Result<isize, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "isize"
)
}
})
})
}
}
impl CdivEuclid for u8 {
type Divisor = u8;
type Output = u8;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: u8) -> Result<u8, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "u8"
)
}
})
})
}
}
impl CdivEuclid for u16 {
type Divisor = u16;
type Output = u16;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: u16) -> Result<u16, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "u16"
)
}
})
})
}
}
impl CdivEuclid for u32 {
type Divisor = u32;
type Output = u32;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: u32) -> Result<u32, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "u32"
)
}
})
})
}
}
impl CdivEuclid for u64 {
type Divisor = u64;
type Output = u64;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: u64) -> Result<u64, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "u64"
)
}
})
})
}
}
impl CdivEuclid for u128 {
type Divisor = u128;
type Output = u128;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: u128) -> Result<u128, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "u128"
)
}
})
})
}
}
impl CdivEuclid for usize {
type Divisor = usize;
type Output = usize;
type Error = Error;
#[inline]
fn cdiv_euclid(self, divisor: usize) -> Result<usize, Error> {
self.checked_div_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute div_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute div_euclid({:?}, {:?}): {} overflow",
self, divisor, "usize"
)
}
})
})
}
}
pub trait Crem: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Divisor;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn crem(self, divisor: Self::Divisor) -> Result<Self::Output, Self::Error>;
fn crem_assign(&mut self, divisor: Self::Divisor) -> Result<(), Self::Error>;
}
#[doc(alias = "checked_rem")]
#[inline]
pub fn crem<T>(value: T, divisor: T::Divisor) -> Result<T::Output, T::Error>
where
T: Crem,
{
value.crem(divisor)
}
impl Crem for i8 {
type Divisor = i8;
type Output = i8;
type Error = Error;
#[inline]
fn crem(self, divisor: i8) -> Result<i8, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"i8"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: i8) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for i16 {
type Divisor = i16;
type Output = i16;
type Error = Error;
#[inline]
fn crem(self, divisor: i16) -> Result<i16, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"i16"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: i16) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for i32 {
type Divisor = i32;
type Output = i32;
type Error = Error;
#[inline]
fn crem(self, divisor: i32) -> Result<i32, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"i32"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: i32) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for i64 {
type Divisor = i64;
type Output = i64;
type Error = Error;
#[inline]
fn crem(self, divisor: i64) -> Result<i64, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"i64"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: i64) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for i128 {
type Divisor = i128;
type Output = i128;
type Error = Error;
#[inline]
fn crem(self, divisor: i128) -> Result<i128, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"i128"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: i128) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for isize {
type Divisor = isize;
type Output = isize;
type Error = Error;
#[inline]
fn crem(self, divisor: isize) -> Result<isize, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"isize"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: isize) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for u8 {
type Divisor = u8;
type Output = u8;
type Error = Error;
#[inline]
fn crem(self, divisor: u8) -> Result<u8, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"u8"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: u8) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for u16 {
type Divisor = u16;
type Output = u16;
type Error = Error;
#[inline]
fn crem(self, divisor: u16) -> Result<u16, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"u16"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: u16) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for u32 {
type Divisor = u32;
type Output = u32;
type Error = Error;
#[inline]
fn crem(self, divisor: u32) -> Result<u32, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"u32"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: u32) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for u64 {
type Divisor = u64;
type Output = u64;
type Error = Error;
#[inline]
fn crem(self, divisor: u64) -> Result<u64, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"u64"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: u64) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for u128 {
type Divisor = u128;
type Output = u128;
type Error = Error;
#[inline]
fn crem(self, divisor: u128) -> Result<u128, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"u128"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: u128) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
impl Crem for usize {
type Divisor = usize;
type Output = usize;
type Error = Error;
#[inline]
fn crem(self, divisor: usize) -> Result<usize, Error> {
self.checked_rem(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute {:?} % {:?}: division by zero",
self,
MaybeParens(divisor)
)
} else {
format!(
"failed to compute {:?} % {:?}: {} overflow",
self,
MaybeParens(divisor),
"usize"
)
}
})
})
}
#[inline]
fn crem_assign(&mut self, divisor: usize) -> Result<(), Self::Error> {
*self = self.crem(divisor)?;
Ok(())
}
}
pub trait CremEuclid: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Divisor;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn crem_euclid(self, divisor: Self::Divisor) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_rem_euclid")]
#[inline]
pub fn crem_euclid<T>(value: T, divisor: T::Divisor) -> Result<T::Output, T::Error>
where
T: CremEuclid,
{
value.crem_euclid(divisor)
}
impl CremEuclid for i8 {
type Divisor = i8;
type Output = i8;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: i8) -> Result<i8, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "i8"
)
}
})
})
}
}
impl CremEuclid for i16 {
type Divisor = i16;
type Output = i16;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: i16) -> Result<i16, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "i16"
)
}
})
})
}
}
impl CremEuclid for i32 {
type Divisor = i32;
type Output = i32;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: i32) -> Result<i32, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "i32"
)
}
})
})
}
}
impl CremEuclid for i64 {
type Divisor = i64;
type Output = i64;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: i64) -> Result<i64, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "i64"
)
}
})
})
}
}
impl CremEuclid for i128 {
type Divisor = i128;
type Output = i128;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: i128) -> Result<i128, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "i128"
)
}
})
})
}
}
impl CremEuclid for isize {
type Divisor = isize;
type Output = isize;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: isize) -> Result<isize, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "isize"
)
}
})
})
}
}
impl CremEuclid for u8 {
type Divisor = u8;
type Output = u8;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: u8) -> Result<u8, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "u8"
)
}
})
})
}
}
impl CremEuclid for u16 {
type Divisor = u16;
type Output = u16;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: u16) -> Result<u16, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "u16"
)
}
})
})
}
}
impl CremEuclid for u32 {
type Divisor = u32;
type Output = u32;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: u32) -> Result<u32, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "u32"
)
}
})
})
}
}
impl CremEuclid for u64 {
type Divisor = u64;
type Output = u64;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: u64) -> Result<u64, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "u64"
)
}
})
})
}
}
impl CremEuclid for u128 {
type Divisor = u128;
type Output = u128;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: u128) -> Result<u128, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "u128"
)
}
})
})
}
}
impl CremEuclid for usize {
type Divisor = usize;
type Output = usize;
type Error = Error;
#[inline]
fn crem_euclid(self, divisor: usize) -> Result<usize, Error> {
self.checked_rem_euclid(divisor).ok_or_else(|| {
Error::new({
if divisor == 0 {
format!(
"failed to compute rem_euclid({:?}, {:?}): division by zero",
self, divisor
)
} else {
format!(
"failed to compute rem_euclid({:?}, {:?}): {} overflow",
self, divisor, "usize"
)
}
})
})
}
}
pub trait Cilog: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Base;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cilog(self, base: Self::Base) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_ilog")]
#[inline]
pub fn cilog<T>(value: T, base: T::Base) -> Result<T::Output, T::Error>
where
T: Cilog,
{
value.cilog(base)
}
impl Cilog for i8 {
type Base = i8;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: i8) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for i16 {
type Base = i16;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: i16) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for i32 {
type Base = i32;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: i32) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for i64 {
type Base = i64;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: i64) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for i128 {
type Base = i128;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: i128) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for isize {
type Base = isize;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: isize) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for u8 {
type Base = u8;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: u8) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for u16 {
type Base = u16;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: u16) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for u32 {
type Base = u32;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: u32) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for u64 {
type Base = u64;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: u64) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for u128 {
type Base = u128;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: u128) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
impl Cilog for usize {
type Base = usize;
type Output = u32;
type Error = Error;
#[inline]
fn cilog(self, base: usize) -> Result<u32, Error> {
self.checked_ilog(base).ok_or_else(|| {
Error::new({
if base < 2 {
format!(
"failed to compute ilog({:?}, {:?}): base is less than 2",
self, base
)
} else {
format!(
"failed to compute ilog({:?}, {:?}): first argument is not positive",
self, base
)
}
})
})
}
}
pub trait Cilog2: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cilog2(self) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_ilog2")]
#[inline]
pub fn cilog2<T>(value: T) -> Result<T::Output, T::Error>
where
T: Cilog2,
{
Cilog2::cilog2(value)
}
impl Cilog2 for i8 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for i16 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for i32 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for i64 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for i128 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for isize {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for u8 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for u16 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for u32 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for u64 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for u128 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
impl Cilog2 for usize {
type Output = u32;
type Error = Error;
#[inline]
fn cilog2(self) -> Result<u32, Error> {
self.checked_ilog2().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog2({:?}): argument is not positive",
self
))
})
}
}
pub trait Cilog10: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cilog10(self) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_ilog10")]
#[inline]
pub fn cilog10<T>(value: T) -> Result<T::Output, T::Error>
where
T: Cilog10,
{
Cilog10::cilog10(value)
}
impl Cilog10 for i8 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for i16 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for i32 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for i64 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for i128 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for isize {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for u8 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for u16 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for u32 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for u64 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for u128 {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
impl Cilog10 for usize {
type Output = u32;
type Error = Error;
#[inline]
fn cilog10(self) -> Result<u32, Error> {
self.checked_ilog10().ok_or_else(|| {
Error::new(format!(
"failed to compute ilog10({:?}): argument is not positive",
self
))
})
}
}
pub trait Cshl: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cshl(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
fn cshl_assign(&mut self, other: Self::Other) -> Result<(), Self::Error>;
}
#[doc(alias = "checked_shl")]
#[inline]
pub fn cshl<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: Cshl,
{
a.cshl(b)
}
impl Cshl for i8 {
type Other = u32;
type Output = i8;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<i8, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for i16 {
type Other = u32;
type Output = i16;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<i16, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for i32 {
type Other = u32;
type Output = i32;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<i32, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for i64 {
type Other = u32;
type Output = i64;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<i64, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for i128 {
type Other = u32;
type Output = i128;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<i128, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for isize {
type Other = u32;
type Output = isize;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<isize, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for u8 {
type Other = u32;
type Output = u8;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<u8, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for u16 {
type Other = u32;
type Output = u16;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<u16, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for u32 {
type Other = u32;
type Output = u32;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<u32, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for u64 {
type Other = u32;
type Output = u64;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<u64, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for u128 {
type Other = u32;
type Output = u128;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<u128, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
impl Cshl for usize {
type Other = u32;
type Output = usize;
type Error = Error;
#[inline]
fn cshl(self, other: u32) -> Result<usize, Error> {
self.checked_shl(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} << {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshl(other)?;
Ok(())
}
}
pub trait Cshr: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cshr(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
fn cshr_assign(&mut self, other: Self::Other) -> Result<(), Self::Error>;
}
#[doc(alias = "checked_shr")]
#[inline]
pub fn cshr<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: Cshr,
{
a.cshr(b)
}
impl Cshr for i8 {
type Other = u32;
type Output = i8;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<i8, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for i16 {
type Other = u32;
type Output = i16;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<i16, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for i32 {
type Other = u32;
type Output = i32;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<i32, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for i64 {
type Other = u32;
type Output = i64;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<i64, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for i128 {
type Other = u32;
type Output = i128;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<i128, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for isize {
type Other = u32;
type Output = isize;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<isize, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for u8 {
type Other = u32;
type Output = u8;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<u8, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for u16 {
type Other = u32;
type Output = u16;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<u16, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for u32 {
type Other = u32;
type Output = u32;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<u32, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for u64 {
type Other = u32;
type Output = u64;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<u64, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for u128 {
type Other = u32;
type Output = u128;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<u128, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
impl Cshr for usize {
type Other = u32;
type Output = usize;
type Error = Error;
#[inline]
fn cshr(self, other: u32) -> Result<usize, Error> {
self.checked_shr(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?} >> {:?}: shift amount is too large",
MaybeParens(self),
MaybeParens(other)
))
})
}
#[inline]
fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
*self = self.cshr(other)?;
Ok(())
}
}
pub trait Cpow: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Power;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cpow(self, power: Self::Power) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_pow")]
#[inline]
pub fn cpow<T>(value: T, power: T::Power) -> Result<T::Output, T::Error>
where
T: Cpow,
{
value.cpow(power)
}
impl Cpow for NonZero<u8> {
type Power = u32;
type Output = NonZero<u8>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<u8>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<u8>"
))
})
}
}
impl Cpow for NonZero<u16> {
type Power = u32;
type Output = NonZero<u16>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<u16>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<u16>"
))
})
}
}
impl Cpow for NonZero<u32> {
type Power = u32;
type Output = NonZero<u32>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<u32>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<u32>"
))
})
}
}
impl Cpow for NonZero<u64> {
type Power = u32;
type Output = NonZero<u64>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<u64>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<u64>"
))
})
}
}
impl Cpow for NonZero<u128> {
type Power = u32;
type Output = NonZero<u128>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<u128>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<u128>"
))
})
}
}
impl Cpow for NonZero<usize> {
type Power = u32;
type Output = NonZero<usize>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<usize>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<usize>"
))
})
}
}
impl Cpow for NonZero<i8> {
type Power = u32;
type Output = NonZero<i8>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<i8>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<i8>"
))
})
}
}
impl Cpow for NonZero<i16> {
type Power = u32;
type Output = NonZero<i16>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<i16>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<i16>"
))
})
}
}
impl Cpow for NonZero<i32> {
type Power = u32;
type Output = NonZero<i32>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<i32>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<i32>"
))
})
}
}
impl Cpow for NonZero<i64> {
type Power = u32;
type Output = NonZero<i64>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<i64>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<i64>"
))
})
}
}
impl Cpow for NonZero<i128> {
type Power = u32;
type Output = NonZero<i128>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<i128>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<i128>"
))
})
}
}
impl Cpow for NonZero<isize> {
type Power = u32;
type Output = NonZero<isize>;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<NonZero<isize>, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "NonZero<isize>"
))
})
}
}
impl Cpow for i8 {
type Power = u32;
type Output = i8;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<i8, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "i8"
))
})
}
}
impl Cpow for i16 {
type Power = u32;
type Output = i16;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<i16, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "i16"
))
})
}
}
impl Cpow for i32 {
type Power = u32;
type Output = i32;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<i32, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "i32"
))
})
}
}
impl Cpow for i64 {
type Power = u32;
type Output = i64;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<i64, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "i64"
))
})
}
}
impl Cpow for i128 {
type Power = u32;
type Output = i128;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<i128, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "i128"
))
})
}
}
impl Cpow for isize {
type Power = u32;
type Output = isize;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<isize, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "isize"
))
})
}
}
impl Cpow for u8 {
type Power = u32;
type Output = u8;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<u8, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "u8"
))
})
}
}
impl Cpow for u16 {
type Power = u32;
type Output = u16;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<u16, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "u16"
))
})
}
}
impl Cpow for u32 {
type Power = u32;
type Output = u32;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<u32, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "u32"
))
})
}
}
impl Cpow for u64 {
type Power = u32;
type Output = u64;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<u64, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "u64"
))
})
}
}
impl Cpow for u128 {
type Power = u32;
type Output = u128;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<u128, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "u128"
))
})
}
}
impl Cpow for usize {
type Power = u32;
type Output = usize;
type Error = Error;
#[inline]
fn cpow(self, power: u32) -> Result<usize, Error> {
self.checked_pow(power).ok_or_else(|| {
Error::new(format!(
"failed to compute pow({:?}, {:?}): {} overflow",
self, power, "usize"
))
})
}
}
pub trait Cabs: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cabs(self) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_abs")]
#[inline]
pub fn cabs<T>(value: T) -> Result<T::Output, T::Error>
where
T: Cabs,
{
Cabs::cabs(value)
}
impl Cabs for NonZero<i8> {
type Output = NonZero<i8>;
type Error = Error;
#[inline]
fn cabs(self) -> Result<NonZero<i8>, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "NonZero<i8>"
))
})
}
}
impl Cabs for NonZero<i16> {
type Output = NonZero<i16>;
type Error = Error;
#[inline]
fn cabs(self) -> Result<NonZero<i16>, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "NonZero<i16>"
))
})
}
}
impl Cabs for NonZero<i32> {
type Output = NonZero<i32>;
type Error = Error;
#[inline]
fn cabs(self) -> Result<NonZero<i32>, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "NonZero<i32>"
))
})
}
}
impl Cabs for NonZero<i64> {
type Output = NonZero<i64>;
type Error = Error;
#[inline]
fn cabs(self) -> Result<NonZero<i64>, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "NonZero<i64>"
))
})
}
}
impl Cabs for NonZero<i128> {
type Output = NonZero<i128>;
type Error = Error;
#[inline]
fn cabs(self) -> Result<NonZero<i128>, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "NonZero<i128>"
))
})
}
}
impl Cabs for NonZero<isize> {
type Output = NonZero<isize>;
type Error = Error;
#[inline]
fn cabs(self) -> Result<NonZero<isize>, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "NonZero<isize>"
))
})
}
}
impl Cabs for i8 {
type Output = i8;
type Error = Error;
#[inline]
fn cabs(self) -> Result<i8, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "i8"
))
})
}
}
impl Cabs for i16 {
type Output = i16;
type Error = Error;
#[inline]
fn cabs(self) -> Result<i16, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "i16"
))
})
}
}
impl Cabs for i32 {
type Output = i32;
type Error = Error;
#[inline]
fn cabs(self) -> Result<i32, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "i32"
))
})
}
}
impl Cabs for i64 {
type Output = i64;
type Error = Error;
#[inline]
fn cabs(self) -> Result<i64, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "i64"
))
})
}
}
impl Cabs for i128 {
type Output = i128;
type Error = Error;
#[inline]
fn cabs(self) -> Result<i128, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "i128"
))
})
}
}
impl Cabs for isize {
type Output = isize;
type Error = Error;
#[inline]
fn cabs(self) -> Result<isize, Error> {
self.checked_abs().ok_or_else(|| {
Error::new(format!(
"failed to compute abs({:?}): {} overflow",
self, "isize"
))
})
}
}
pub trait Cisqrt: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cisqrt(self) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_isqrt")]
#[inline]
pub fn cisqrt<T>(value: T) -> Result<T::Output, T::Error>
where
T: Cisqrt,
{
Cisqrt::cisqrt(value)
}
impl Cisqrt for i8 {
type Output = i8;
type Error = Error;
#[inline]
fn cisqrt(self) -> Result<i8, Error> {
self.checked_isqrt().ok_or_else(|| {
Error::new(format!(
"failed to compute isqrt({:?}): argument is negative",
self
))
})
}
}
impl Cisqrt for i16 {
type Output = i16;
type Error = Error;
#[inline]
fn cisqrt(self) -> Result<i16, Error> {
self.checked_isqrt().ok_or_else(|| {
Error::new(format!(
"failed to compute isqrt({:?}): argument is negative",
self
))
})
}
}
impl Cisqrt for i32 {
type Output = i32;
type Error = Error;
#[inline]
fn cisqrt(self) -> Result<i32, Error> {
self.checked_isqrt().ok_or_else(|| {
Error::new(format!(
"failed to compute isqrt({:?}): argument is negative",
self
))
})
}
}
impl Cisqrt for i64 {
type Output = i64;
type Error = Error;
#[inline]
fn cisqrt(self) -> Result<i64, Error> {
self.checked_isqrt().ok_or_else(|| {
Error::new(format!(
"failed to compute isqrt({:?}): argument is negative",
self
))
})
}
}
impl Cisqrt for i128 {
type Output = i128;
type Error = Error;
#[inline]
fn cisqrt(self) -> Result<i128, Error> {
self.checked_isqrt().ok_or_else(|| {
Error::new(format!(
"failed to compute isqrt({:?}): argument is negative",
self
))
})
}
}
impl Cisqrt for isize {
type Output = isize;
type Error = Error;
#[inline]
fn cisqrt(self) -> Result<isize, Error> {
self.checked_isqrt().ok_or_else(|| {
Error::new(format!(
"failed to compute isqrt({:?}): argument is negative",
self
))
})
}
}
pub trait CnextMultipleOf: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cnext_multiple_of(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_next_multiple_of")]
#[inline]
pub fn cnext_multiple_of<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: CnextMultipleOf,
{
a.cnext_multiple_of(b)
}
impl CnextMultipleOf for u8 {
type Other = u8;
type Output = u8;
type Error = Error;
#[inline]
fn cnext_multiple_of(self, other: u8) -> Result<u8, Error> {
self.checked_next_multiple_of(other).ok_or_else(|| {
Error::new({
if other < 2 {
format!(
"failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
self, other
)
} else {
format!(
"failed to compute next_multiple_of({:?}, {:?}): {} overflow",
self, other, "u8"
)
}
})
})
}
}
impl CnextMultipleOf for u16 {
type Other = u16;
type Output = u16;
type Error = Error;
#[inline]
fn cnext_multiple_of(self, other: u16) -> Result<u16, Error> {
self.checked_next_multiple_of(other).ok_or_else(|| {
Error::new({
if other < 2 {
format!(
"failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
self, other
)
} else {
format!(
"failed to compute next_multiple_of({:?}, {:?}): {} overflow",
self, other, "u16"
)
}
})
})
}
}
impl CnextMultipleOf for u32 {
type Other = u32;
type Output = u32;
type Error = Error;
#[inline]
fn cnext_multiple_of(self, other: u32) -> Result<u32, Error> {
self.checked_next_multiple_of(other).ok_or_else(|| {
Error::new({
if other < 2 {
format!(
"failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
self, other
)
} else {
format!(
"failed to compute next_multiple_of({:?}, {:?}): {} overflow",
self, other, "u32"
)
}
})
})
}
}
impl CnextMultipleOf for u64 {
type Other = u64;
type Output = u64;
type Error = Error;
#[inline]
fn cnext_multiple_of(self, other: u64) -> Result<u64, Error> {
self.checked_next_multiple_of(other).ok_or_else(|| {
Error::new({
if other < 2 {
format!(
"failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
self, other
)
} else {
format!(
"failed to compute next_multiple_of({:?}, {:?}): {} overflow",
self, other, "u64"
)
}
})
})
}
}
impl CnextMultipleOf for u128 {
type Other = u128;
type Output = u128;
type Error = Error;
#[inline]
fn cnext_multiple_of(self, other: u128) -> Result<u128, Error> {
self.checked_next_multiple_of(other).ok_or_else(|| {
Error::new({
if other < 2 {
format!(
"failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
self, other
)
} else {
format!(
"failed to compute next_multiple_of({:?}, {:?}): {} overflow",
self, other, "u128"
)
}
})
})
}
}
impl CnextMultipleOf for usize {
type Other = usize;
type Output = usize;
type Error = Error;
#[inline]
fn cnext_multiple_of(self, other: usize) -> Result<usize, Error> {
self.checked_next_multiple_of(other).ok_or_else(|| {
Error::new({
if other < 2 {
format!(
"failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
self, other
)
} else {
format!(
"failed to compute next_multiple_of({:?}, {:?}): {} overflow",
self, other, "usize"
)
}
})
})
}
}
pub trait CnextPowerOfTwo: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cnext_power_of_two(self) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_next_power_of_two")]
#[inline]
pub fn cnext_power_of_two<T>(value: T) -> Result<T::Output, T::Error>
where
T: CnextPowerOfTwo,
{
CnextPowerOfTwo::cnext_power_of_two(value)
}
impl CnextPowerOfTwo for NonZero<u8> {
type Output = NonZero<u8>;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<NonZero<u8>, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "NonZero<u8>"
))
})
}
}
impl CnextPowerOfTwo for NonZero<u16> {
type Output = NonZero<u16>;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<NonZero<u16>, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "NonZero<u16>"
))
})
}
}
impl CnextPowerOfTwo for NonZero<u32> {
type Output = NonZero<u32>;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<NonZero<u32>, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "NonZero<u32>"
))
})
}
}
impl CnextPowerOfTwo for NonZero<u64> {
type Output = NonZero<u64>;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<NonZero<u64>, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "NonZero<u64>"
))
})
}
}
impl CnextPowerOfTwo for NonZero<u128> {
type Output = NonZero<u128>;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<NonZero<u128>, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "NonZero<u128>"
))
})
}
}
impl CnextPowerOfTwo for NonZero<usize> {
type Output = NonZero<usize>;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<NonZero<usize>, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "NonZero<usize>"
))
})
}
}
impl CnextPowerOfTwo for u8 {
type Output = u8;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<u8, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "u8"
))
})
}
}
impl CnextPowerOfTwo for u16 {
type Output = u16;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<u16, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "u16"
))
})
}
}
impl CnextPowerOfTwo for u32 {
type Output = u32;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<u32, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "u32"
))
})
}
}
impl CnextPowerOfTwo for u64 {
type Output = u64;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<u64, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "u64"
))
})
}
}
impl CnextPowerOfTwo for u128 {
type Output = u128;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<u128, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "u128"
))
})
}
}
impl CnextPowerOfTwo for usize {
type Output = usize;
type Error = Error;
#[inline]
fn cnext_power_of_two(self) -> Result<usize, Error> {
self.checked_next_power_of_two().ok_or_else(|| {
Error::new(format!(
"failed to compute next_power_of_two({:?}): {} overflow",
self, "usize"
))
})
}
}
pub trait CdurationSince: Sized {
#[expect(missing_docs, reason = "no need for doc")]
type Other;
#[expect(missing_docs, reason = "no need for doc")]
type Output;
#[expect(missing_docs, reason = "no need for doc")]
type Error;
fn cduration_since(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
}
#[doc(alias = "checked_duration_since")]
#[inline]
pub fn cduration_since<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
where
T: CdurationSince,
{
a.cduration_since(b)
}
#[cfg(feature = "std")]
impl CdurationSince for Instant {
type Other = Instant;
type Output = Duration;
type Error = Error;
#[inline]
fn cduration_since(self, other: Instant) -> Result<Duration, Error> {
self.checked_duration_since(other).ok_or_else(|| {
Error::new(format!(
"failed to compute {:?}.duration_since({:?}): {:?} is earlier than {:?}",
self, other, other, self
))
})
}
}