pub trait IntegerExt: 'static + Eq + Ord + Copy + Default + Binary + Debug + Display + LowerHex + Octal + UpperHex + Hash + Add<Self, Output = Self> + AddAssign<Self> + BitAnd<Self, Output = Self> + BitAndAssign<Self> + BitOr<Self, Output = Self> + BitOrAssign<Self> + BitXor<Self, Output = Self> + BitXorAssign<Self> + Div<Self, Output = Self> + DivAssign<Self> + Mul<Self, Output = Self> + MulAssign<Self> + Not<Output = Self> + Rem<Self, Output = Self> + RemAssign<Self> + Shl<Self, Output = Self> + ShlAssign<Self> + Shr<Self, Output = Self> + ShrAssign<Self> + Sub<Self, Output = Self> + SubAssign<Self> + Send + Sync {
    type Unsigned: IntegerExt;

    const MIN: Self;
    const MAX: Self;
    const ZERO: Self;
    const ONE: Self;

    fn from_u8(n: u8) -> Self;
    fn from_i8(n: i8) -> Self;
    fn power(self, n: u32) -> Self;
    fn abs_unsigned(self) -> Self::Unsigned;
    fn number_of_digits(self) -> u32;

    fn get_sign(self) -> Sign { ... }
    fn safe_div(self, other: Self) -> Self { ... }
}
Available on crate feature integers only.
Expand description

Extension trait for built-in integers.

Required Associated Types

The unsigned version of this integer type.

Required Associated Constants

The minimum value of this integer type.

The maximum value of this integer type.

0 of this integer type.

1 of this integer type.

Required Methods

Converts from a u8 to Self.

if Self is an i8 this method returns 127 for n > 127.

Example
use core_extensions::IntegerExt;

assert_eq!(u8::from_u8(0  ), 0);
assert_eq!(u8::from_u8(255), 255);

assert_eq!(i8::from_u8(0  ), 0);
assert_eq!(i8::from_u8(255), 127);

assert_eq!(u16::from_u8(0  ), 0);
assert_eq!(u16::from_u8(255), 255);

assert_eq!(i16::from_u8(0  ), 0);
assert_eq!(i16::from_u8(255), 255);

Converts from an i8 to Self.

if Self is an unsigned integer type, this method returns 0 for n < 0.

Example
use core_extensions::IntegerExt;

assert_eq!(u8::from_i8(-128), 0);
assert_eq!(u8::from_i8(0   ), 0);
assert_eq!(u8::from_i8(1   ), 1);
assert_eq!(u8::from_i8(127 ), 127);

assert_eq!(i8::from_i8(-128), -128);
assert_eq!(i8::from_i8(0   ), 0   );
assert_eq!(i8::from_i8(127 ), 127 );

assert_eq!(u16::from_i8(-128), 0);
assert_eq!(u16::from_i8(0   ), 0);
assert_eq!(u16::from_i8(1   ), 1);
assert_eq!(u16::from_i8(127 ), 127);

assert_eq!(i16::from_i8(-128), -128);
assert_eq!(i16::from_i8(0   ), 0   );
assert_eq!(i16::from_i8(127 ), 127 );

Raises self to the nth power.

This delegates to the inherent pow method.

Returns the absolute value of this integer as the equivalent unsigned integer type.

This method allows getting the absolute value for the minimum signed integer value.

Example
use core_extensions::IntegerExt;

assert_eq!(0u8.abs_unsigned(), 0u8);
assert_eq!(0i8.abs_unsigned(), 0u8);
assert_eq!(127i8.abs_unsigned(), 127u8);
assert_eq!((-1i8).abs_unsigned(), 1u8);
assert_eq!((-16i8).abs_unsigned(), 16u8);
assert_eq!((-128i8).abs_unsigned(), 128u8);

Returns the number of decimal digits of self.

This counts the - sign as a digit.

Example
use core_extensions::IntegerExt;

assert_eq!(100.number_of_digits(), 3);
assert_eq!(10.number_of_digits(), 2);
assert_eq!(1.number_of_digits(), 1);
assert_eq!(0.number_of_digits(), 1);
assert_eq!((-1).number_of_digits(), 2);
assert_eq!((-100).number_of_digits(), 4);

Provided Methods

Gets the sign of this integer.

Example
use core_extensions::integers::{IntegerExt, Sign};

assert_eq!(0u8.get_sign(), Sign::Positive);
assert_eq!(0i8.get_sign(), Sign::Positive);
assert_eq!(127i8.get_sign(), Sign::Positive);
assert_eq!((-1i8).get_sign(), Sign::Negative);
assert_eq!((-128i8).get_sign(), Sign::Negative);

Non-panicking division which returns self when other == 0.

Example
use core_extensions::IntegerExt;

assert_eq!(60.safe_div(12), 5);
assert_eq!(60.safe_div(30), 2);
assert_eq!(60.safe_div(31), 1);

assert_eq!(60.safe_div(0), 60);
assert_eq!(13.safe_div(0), 13);

Implementations on Foreign Types

Implementors