/// Absolute value helper.
///
/// Contract (minimum): return the magnitude of `self`. Implementers must
/// document how they handle values that cannot be represented after abs (e.g.,
/// negative minimum for signed ints) and incomparable inputs (e.g., `NaN`).
pub trait Abs: Sized {
/// Returns the absolute value of `self`.
///
/// Implementations for unsigned types usually return `self`.
fn abs(self) -> Self;
}
macro_rules! impl_abs_signed {
($($ty:ty),+ $(,)?) => {
$(impl Abs for $ty {
fn abs(self) -> Self {
<$ty>::abs(self)
}
})+
};
}
macro_rules! impl_abs_unsigned {
($($ty:ty),+ $(,)?) => {
$(impl Abs for $ty {
fn abs(self) -> Self {
self
}
})+
};
}
impl_abs_signed!(i8, i16, i32, i64, i128, isize);
impl_abs_signed!(f32, f64);
impl_abs_unsigned!(u8, u16, u32, u64, u128, usize);