PrimitiveSigned

Trait PrimitiveSigned 

Source
pub trait PrimitiveSigned:
    PrimitiveInteger
    + From<i8>
    + Neg<Output = Self> {
    type Unsigned: PrimitiveUnsigned;

Show 20 methods // Required methods fn abs(self) -> Self; fn abs_diff(self, other: Self) -> Self::Unsigned; fn checked_abs(self) -> Option<Self>; fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>; fn checked_isqrt(self) -> Option<Self>; fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>; fn is_negative(self) -> bool; fn is_positive(self) -> bool; fn overflowing_abs(self) -> (Self, bool); fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool); fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool); fn saturating_abs(self) -> Self; fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self; fn saturating_neg(self) -> Self; fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self; fn signum(self) -> Self; fn unsigned_abs(self) -> Self::Unsigned; fn wrapping_abs(self) -> Self; fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self; fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
}
Expand description

Trait for all primitive signed integer types, including the supertraits PrimitiveInteger and PrimitiveNumber.

This encapsulates trait implementations and inherent methods that are common among all of the primitive signed integer types: i8, i16, i32, i64, i128, and isize.

See the corresponding items on the individual types for more documentation and examples.

This trait is sealed with a private trait to prevent downstream implementations, so we may continue to expand along with the standard library without worrying about breaking changes for implementors.

§Examples

use num_primitive::PrimitiveSigned;

// GCD with Bézout coefficients (extended Euclidean algorithm)
fn extended_gcd<T: PrimitiveSigned>(a: T, b: T) -> (T, T, T) {
    let zero = T::from(0i8);
    let one = T::from(1i8);

    let (mut old_r, mut r) = (a, b);
    let (mut old_s, mut s) = (one, zero);
    let (mut old_t, mut t) = (zero, one);

    while r != zero {
        let quotient = old_r.div_euclid(r);
        (old_r, r) = (r, old_r - quotient * r);
        (old_s, s) = (s, old_s - quotient * s);
        (old_t, t) = (t, old_t - quotient * t);
    }

    let (gcd, x, y) = if old_r.is_negative() {
        (-old_r, -old_s, -old_t)
    } else {
        (old_r, old_s, old_t)
    };
    assert_eq!(gcd, a * x + b * y);
    (gcd, x, y)
}

assert_eq!(extended_gcd::<i8>(0, -42), (42, 0, -1));
assert_eq!(extended_gcd::<i8>(48, 18), (6, -1, 3));
assert_eq!(extended_gcd::<i16>(1071, -462), (21, -3, -7));
assert_eq!(extended_gcd::<i64>(6_700_417, 2_147_483_647), (1, 715_828_096, -2_233_473));

Required Associated Types§

Source

type Unsigned: PrimitiveUnsigned

The unsigned integer type used by methods like abs_diff and checked_add_unsigned.

Required Methods§

Source

fn abs(self) -> Self

Computes the absolute value of self.

Source

fn abs_diff(self, other: Self) -> Self::Unsigned

Computes the absolute difference between self and other.

Source

fn checked_abs(self) -> Option<Self>

Checked absolute value. Computes self.abs(), returning None if self == MIN.

Source

fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Checked addition with an unsigned integer. Computes self + rhs, returning None if overflow occurred.

Source

fn checked_isqrt(self) -> Option<Self>

Returns the square root of the number, rounded down. Returns None if self is negative.

Source

fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Checked subtraction with an unsigned integer. Computes self - rhs, returning None if overflow occurred.

Source

fn is_negative(self) -> bool

Returns true if self is negative and false if the number is zero or positive.

Source

fn is_positive(self) -> bool

Returns true if self is positive and false if the number is zero or negative.

Source

fn overflowing_abs(self) -> (Self, bool)

Computes the absolute value of self. Returns a tuple of the absolute version of self along with a boolean indicating whether an overflow happened.

Source

fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Calculates self + rhs with an unsigned rhs. Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would occur.

Source

fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Calculates self - rhs with an unsigned rhs. Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur.

Source

fn saturating_abs(self) -> Self

Saturating absolute value. Computes self.abs(), returning MAX if self == MIN instead of overflowing.

Source

fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self

Saturating addition with an unsigned integer. Computes self + rhs, saturating at the numeric bounds instead of overflowing.

Source

fn saturating_neg(self) -> Self

Saturating integer negation. Computes -self, returning MAX if self == MIN instead of overflowing.

Source

fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Saturating subtraction with an unsigned integer. Computes self - rhs, saturating at the numeric bounds instead of overflowing.

Source

fn signum(self) -> Self

Returns a number representing sign of self.

Source

fn unsigned_abs(self) -> Self::Unsigned

Computes the absolute value of self without any wrapping or panicking.

Source

fn wrapping_abs(self) -> Self

Wrapping (modular) absolute value. Computes self.abs(), wrapping around at the boundary of the type.

Source

fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self

Wrapping (modular) addition with an unsigned integer. Computes self + rhs, wrapping around at the boundary of the type.

Source

fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Wrapping (modular) subtraction with an unsigned integer. Computes self - rhs, wrapping around at the boundary of the type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl PrimitiveSigned for i8

Source§

type Unsigned = u8

Source§

fn abs(self) -> Self

Source§

fn abs_diff(self, other: Self) -> Self::Unsigned

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn checked_isqrt(self) -> Option<Self>

Source§

fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn overflowing_abs(self) -> (Self, bool)

Source§

fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn saturating_abs(self) -> Self

Source§

fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn saturating_neg(self) -> Self

Source§

fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn signum(self) -> Self

Source§

fn unsigned_abs(self) -> Self::Unsigned

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

impl PrimitiveSigned for i16

Source§

type Unsigned = u16

Source§

fn abs(self) -> Self

Source§

fn abs_diff(self, other: Self) -> Self::Unsigned

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn checked_isqrt(self) -> Option<Self>

Source§

fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn overflowing_abs(self) -> (Self, bool)

Source§

fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn saturating_abs(self) -> Self

Source§

fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn saturating_neg(self) -> Self

Source§

fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn signum(self) -> Self

Source§

fn unsigned_abs(self) -> Self::Unsigned

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

impl PrimitiveSigned for i32

Source§

type Unsigned = u32

Source§

fn abs(self) -> Self

Source§

fn abs_diff(self, other: Self) -> Self::Unsigned

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn checked_isqrt(self) -> Option<Self>

Source§

fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn overflowing_abs(self) -> (Self, bool)

Source§

fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn saturating_abs(self) -> Self

Source§

fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn saturating_neg(self) -> Self

Source§

fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn signum(self) -> Self

Source§

fn unsigned_abs(self) -> Self::Unsigned

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

impl PrimitiveSigned for i64

Source§

type Unsigned = u64

Source§

fn abs(self) -> Self

Source§

fn abs_diff(self, other: Self) -> Self::Unsigned

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn checked_isqrt(self) -> Option<Self>

Source§

fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn overflowing_abs(self) -> (Self, bool)

Source§

fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn saturating_abs(self) -> Self

Source§

fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn saturating_neg(self) -> Self

Source§

fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn signum(self) -> Self

Source§

fn unsigned_abs(self) -> Self::Unsigned

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

impl PrimitiveSigned for i128

Source§

type Unsigned = u128

Source§

fn abs(self) -> Self

Source§

fn abs_diff(self, other: Self) -> Self::Unsigned

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn checked_isqrt(self) -> Option<Self>

Source§

fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn overflowing_abs(self) -> (Self, bool)

Source§

fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn saturating_abs(self) -> Self

Source§

fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn saturating_neg(self) -> Self

Source§

fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn signum(self) -> Self

Source§

fn unsigned_abs(self) -> Self::Unsigned

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

impl PrimitiveSigned for isize

Source§

type Unsigned = usize

Source§

fn abs(self) -> Self

Source§

fn abs_diff(self, other: Self) -> Self::Unsigned

Source§

fn checked_abs(self) -> Option<Self>

Source§

fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn checked_isqrt(self) -> Option<Self>

Source§

fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>

Source§

fn is_negative(self) -> bool

Source§

fn is_positive(self) -> bool

Source§

fn overflowing_abs(self) -> (Self, bool)

Source§

fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool)

Source§

fn saturating_abs(self) -> Self

Source§

fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn saturating_neg(self) -> Self

Source§

fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn signum(self) -> Self

Source§

fn unsigned_abs(self) -> Self::Unsigned

Source§

fn wrapping_abs(self) -> Self

Source§

fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self

Source§

fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self

Implementors§