Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub trait Signed: Sized + Num + Neg<Output = Self> {
///
/// Returns `zero` if the number is less than or equal to `other`, otherwise the difference
/// between `self` and `other` is returned.
///
/// For signed integers, the difference may not be representable (e.g.
/// `i32::MAX.abs_sub(&-1)`), in which case the subtraction overflows: it panics when
/// overflow checks are enabled and wraps around otherwise.
fn abs_sub(&self, other: &Self) -> Self;

/// Returns the sign of the number.
Expand Down Expand Up @@ -46,7 +50,9 @@ macro_rules! signed_impl {
impl Signed for $t {
#[inline]
fn abs(&self) -> $t {
if self.is_negative() { -*self } else { *self }
// `wrapping_abs` matches the documented behavior (`::MIN` is returned for
// `::MIN`); negation would panic on `::MIN` when overflow checks are enabled.
self.wrapping_abs()
}

#[inline]
Expand Down Expand Up @@ -214,3 +220,16 @@ fn signed_wrapping_is_signed() {
fn require_signed<T: Signed>(_: &T) {}
require_signed(&Wrapping(-42));
}

#[test]
fn abs_min_returns_min() {
// The documented behavior: "For signed integers, `::MIN` will be returned if the number
// is `::MIN`." This must also hold when overflow checks are enabled.
assert_eq!(Signed::abs(&isize::MIN), isize::MIN);
assert_eq!(Signed::abs(&i8::MIN), i8::MIN);
assert_eq!(Signed::abs(&i16::MIN), i16::MIN);
assert_eq!(Signed::abs(&i32::MIN), i32::MIN);
assert_eq!(Signed::abs(&i64::MIN), i64::MIN);
assert_eq!(Signed::abs(&i128::MIN), i128::MIN);
assert_eq!(crate::sign::abs(i32::MIN), i32::MIN);
}