use crate::error::AssemblerError;
pub(crate) trait CastSign<T> {
fn cast_sign(self) -> T;
}
impl CastSign<i32> for u32 {
#[allow(clippy::as_conversions, clippy::cast_possible_wrap)]
#[inline]
fn cast_sign(self) -> i32 {
self as i32
}
}
pub(crate) trait Overflow<T> {
fn overflow<L>(self) -> Result<T, AssemblerError<L>>;
}
impl<T> Overflow<T> for Option<T> {
#[inline]
fn overflow<L>(self) -> Result<T, AssemblerError<L>> {
self.ok_or(AssemblerError::Overflow)
}
}
impl<T> Overflow<T> for Result<T, std::num::TryFromIntError> {
#[inline]
fn overflow<L>(self) -> Result<T, AssemblerError<L>> {
self.or(Err(AssemblerError::Overflow))
}
}
#[inline]
pub(crate) fn checked_next_multiple_of<L>(n: u32, m: u32) -> Result<u32, AssemblerError<L>> {
if u32::MAX - m + 1 < n {
Err(AssemblerError::Overflow)
} else {
Ok(n.next_multiple_of(m))
}
}