Trait core_extensions::integers::IntegerExt[][src]

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 { ... } }
This is supported on crate feature integers only.

Extension trait for built-in integers.

Associated Types

type Unsigned: IntegerExt[src]

The unsigned version of this integer type.

Loading content...

Associated Constants

const MIN: Self[src]

The minimum value of this integer type.

const MAX: Self[src]

The maximum value of this integer type.

const ZERO: Self[src]

0 of this integer type.

const ONE: Self[src]

1 of this integer type.

Loading content...

Required methods

fn from_u8(n: u8) -> Self[src]

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

fn from_i8(n: i8) -> Self[src]

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

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

Raises self to the nth power.

This delegates to the inherent pow method.

fn abs_unsigned(self) -> Self::Unsigned[src]

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

fn number_of_digits(self) -> u32[src]

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);
Loading content...

Provided methods

fn get_sign(self) -> Sign[src]

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

fn safe_div(self, other: Self) -> Self[src]

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);
Loading content...

Implementations on Foreign Types

impl IntegerExt for i8[src]

type Unsigned = u8

impl IntegerExt for u8[src]

type Unsigned = u8

impl IntegerExt for i16[src]

type Unsigned = u16

impl IntegerExt for u16[src]

type Unsigned = u16

impl IntegerExt for i32[src]

type Unsigned = u32

impl IntegerExt for u32[src]

type Unsigned = u32

impl IntegerExt for i64[src]

type Unsigned = u64

impl IntegerExt for u64[src]

type Unsigned = u64

impl IntegerExt for i128[src]

type Unsigned = u128

impl IntegerExt for u128[src]

type Unsigned = u128

impl IntegerExt for isize[src]

type Unsigned = usize

impl IntegerExt for usize[src]

type Unsigned = usize

Loading content...

Implementors

Loading content...