Trait IntegerExt

Source
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;

    // Required methods
    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;

    // Provided methods
    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 Constants§

Source

const MIN: Self

The minimum value of this integer type.

Source

const MAX: Self

The maximum value of this integer type.

Source

const ZERO: Self

0 of this integer type.

Source

const ONE: Self

1 of this integer type.

Required Associated Types§

Source

type Unsigned: IntegerExt

The unsigned version of this integer type.

Required Methods§

Source

fn from_u8(n: u8) -> Self

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);
Source

fn from_i8(n: i8) -> Self

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 );

Source

fn power(self, n: u32) -> Self

Raises self to the nth power.

This delegates to the inherent pow method.

Source

fn abs_unsigned(self) -> Self::Unsigned

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);
Source

fn number_of_digits(self) -> u32

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§

Source

fn get_sign(self) -> Sign

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);

Source

fn safe_div(self, other: Self) -> Self

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);

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 IntegerExt for i8

Source§

const ZERO: Self = 0i8

Source§

const ONE: Self = 1i8

Source§

const MIN: Self = -128i8

Source§

const MAX: Self = 127i8

Source§

type Unsigned = u8

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for i16

Source§

const ZERO: Self = 0i16

Source§

const ONE: Self = 1i16

Source§

const MIN: Self = -32_768i16

Source§

const MAX: Self = 32_767i16

Source§

type Unsigned = u16

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for i32

Source§

const ZERO: Self = 0i32

Source§

const ONE: Self = 1i32

Source§

const MIN: Self = -2_147_483_648i32

Source§

const MAX: Self = 2_147_483_647i32

Source§

type Unsigned = u32

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for i64

Source§

const ZERO: Self = 0i64

Source§

const ONE: Self = 1i64

Source§

const MIN: Self = -9_223_372_036_854_775_808i64

Source§

const MAX: Self = 9_223_372_036_854_775_807i64

Source§

type Unsigned = u64

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for i128

Source§

const ZERO: Self = 0i128

Source§

const ONE: Self = 1i128

Source§

const MIN: Self = -170_141_183_460_469_231_731_687_303_715_884_105_728i128

Source§

const MAX: Self = 170_141_183_460_469_231_731_687_303_715_884_105_727i128

Source§

type Unsigned = u128

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for isize

Source§

const ZERO: Self = 0isize

Source§

const ONE: Self = 1isize

Source§

const MIN: Self = -9_223_372_036_854_775_808isize

Source§

const MAX: Self = 9_223_372_036_854_775_807isize

Source§

type Unsigned = usize

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for u8

Source§

const ZERO: Self = 0u8

Source§

const ONE: Self = 1u8

Source§

const MIN: Self = 0u8

Source§

const MAX: Self = 255u8

Source§

type Unsigned = u8

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for u16

Source§

const ZERO: Self = 0u16

Source§

const ONE: Self = 1u16

Source§

const MIN: Self = 0u16

Source§

const MAX: Self = 65_535u16

Source§

type Unsigned = u16

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for u32

Source§

const ZERO: Self = 0u32

Source§

const ONE: Self = 1u32

Source§

const MIN: Self = 0u32

Source§

const MAX: Self = 4_294_967_295u32

Source§

type Unsigned = u32

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for u64

Source§

const ZERO: Self = 0u64

Source§

const ONE: Self = 1u64

Source§

const MIN: Self = 0u64

Source§

const MAX: Self = 18_446_744_073_709_551_615u64

Source§

type Unsigned = u64

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for u128

Source§

const ZERO: Self = 0u128

Source§

const ONE: Self = 1u128

Source§

const MIN: Self = 0u128

Source§

const MAX: Self = 340_282_366_920_938_463_463_374_607_431_768_211_455u128

Source§

type Unsigned = u128

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Source§

impl IntegerExt for usize

Source§

const ZERO: Self = 0usize

Source§

const ONE: Self = 1usize

Source§

const MIN: Self = 0usize

Source§

const MAX: Self = 18_446_744_073_709_551_615usize

Source§

type Unsigned = usize

Source§

fn abs_unsigned(self) -> Self::Unsigned

Source§

fn number_of_digits(self) -> u32

Source§

fn power(self, n: u32) -> Self

Source§

fn from_u8(n: u8) -> Self

Source§

fn from_i8(n: i8) -> Self

Implementors§