[][src]Struct js_int::Int

pub struct Int(_);

An integer limited to the range of integers that can be represented exactly by an f64.

Methods

impl Int[src]

#[must_use] pub fn new(val: i64) -> Option<Self>[src]

Try to create an Int from the provided i64, returning None if it is smaller than MIN_SAFE_INT or larger than MAX_SAFE_INT.

This is the same as the TryFrom<u64> implementation for Int, except that it returns an Option instead of a Result.

Examples

Basic usage:

assert_eq!(Int::new(js_int::MIN_SAFE_INT), Some(Int::min_value()));
assert_eq!(Int::new(js_int::MAX_SAFE_INT), Some(Int::max_value()));
assert_eq!(Int::new(js_int::MIN_SAFE_INT - 1), None);
assert_eq!(Int::new(js_int::MAX_SAFE_INT + 1), None);

#[must_use] pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>[src]

Converts a string slice in a given base to an integer.

The string is expected to be an optional + or - sign followed by digits. Leading and trailing whitespace represent an error. Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This function panics if radix is not in the range from 2 to 36.

Examples

Basic usage:

assert_eq!(Int::from_str_radix("A", 16), Ok(Int::from(10)));

#[must_use] pub const fn min_value() -> Self[src]

Returns the smallest value that can be represented by this integer type.

Examples

Basic usage:

assert_eq!(Int::min_value(), Int::try_from(-9_007_199_254_740_991i64).unwrap());

#[must_use] pub const fn max_value() -> Self[src]

Returns the largest value that can be represented by this integer type.

Examples

Basic usage:

assert_eq!(Int::max_value(), Int::try_from(9_007_199_254_740_991i64).unwrap());

#[must_use] pub fn abs(self) -> Self[src]

Computes the absolute value of self.

Examples

Basic usage:

assert_eq!(Int::from(10).abs(), Int::from(10));
assert_eq!(Int::from(-10).abs(), Int::from(10));

// Differently from i8 / i16 / i32 / i128, Int's min_value is its max_value negated
assert_eq!(Int::min_value().abs(), Int::max_value());

#[must_use] pub const fn is_positive(self) -> bool[src]

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

Examples

Basic usage:

assert!(Int::from(10).is_positive());
assert!(!Int::from(0).is_positive());
assert!(!Int::from(-10).is_positive());

#[must_use] pub const fn is_negative(self) -> bool[src]

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

Examples

Basic usage:

assert!(Int::from(-10).is_negative());
assert!(!Int::from(0).is_negative());
assert!(!Int::from(10).is_negative());

#[must_use] pub fn checked_add(self, rhs: Self) -> Option<Self>[src]

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

Examples

Basic usage:

assert_eq!(
    (Int::max_value() - Int::from(1)).checked_add(Int::from(1)),
    Some(Int::max_value())
);

assert_eq!(
    (Int::max_value() - Int::from(1)).checked_add(Int::from(2)),
    None
);

#[must_use] pub fn checked_sub(self, rhs: Self) -> Option<Self>[src]

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

Examples

Basic usage:

assert_eq!(
    (Int::min_value() + Int::from(2)).checked_sub(Int::from(1)),
    Some(Int::min_value() + Int::from(1))
);
assert_eq!((Int::min_value() + Int::from(2)).checked_sub(Int::from(3)), None);

#[must_use] pub fn checked_mul(self, rhs: Self) -> Option<Self>[src]

Checked integer multiplication. Computes self * rhs, returning None if overflow occurred.

Examples

Basic usage:

assert_eq!(Int::from(5).checked_mul(Int::from(1)), Some(Int::from(5)));
assert_eq!(Int::max_value().checked_mul(Int::from(2)), None);

#[must_use] pub fn checked_div(self, rhs: Self) -> Option<Self>[src]

Checked integer division. Computes self / rhs, returning None if rhs == 0.

Examples

Basic usage:

assert_eq!(Int::min_value().checked_div(Int::from(-1)), Some(Int::max_value()));
assert_eq!(Int::from(1).checked_div(Int::from(0)), None);

#[must_use] pub fn checked_rem(self, rhs: Self) -> Option<Self>[src]

Checked integer remainder. Computes self % rhs, returning None if rhs == 0.

Examples

Basic usage:

assert_eq!(Int::from(5).checked_rem(Int::from(2)), Some(Int::from(1)));
assert_eq!(Int::from(5).checked_rem(Int::from(0)), None);
assert_eq!(Int::min_value().checked_rem(Int::from(-1)), Some(Int::from(0)));

#[must_use] pub fn checked_pow(self, exp: u32) -> Option<Self>[src]

Checked exponentiation. Computes self.pow(exp), returning None if overflow or underflow occurred.

Examples

Basic usage:

assert_eq!(Int::from(8).checked_pow(2), Some(Int::from(64)));
assert_eq!(Int::max_value().checked_pow(2), None);
assert_eq!(Int::min_value().checked_pow(2), None);
assert_eq!(Int::from(1_000_000_000).checked_pow(2), None);

#[must_use] pub fn saturating_add(self, rhs: Self) -> Self[src]

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

Examples

Basic usage:

assert_eq!(Int::from(100).saturating_add(Int::from(1)), Int::from(101));
assert_eq!(Int::max_value().saturating_add(Int::from(1)), Int::max_value());

#[must_use] pub fn saturating_sub(self, rhs: Self) -> Self[src]

Saturating integer subtraction. Computes self - rhs, saturating at the numeric bounds instead of underflowing.

Examples

Basic usage:

assert_eq!(Int::from(100).saturating_sub(Int::from(1)), Int::from(99));
assert_eq!(Int::min_value().saturating_sub(Int::from(1)), Int::min_value());

#[must_use] pub fn saturating_mul(self, rhs: Self) -> Self[src]

Saturating integer multiplication. Computes self * rhs, saturating at the numeric bounds instead of overflowing.

Examples

Basic usage:

assert_eq!(Int::from(100).saturating_mul(Int::from(2)), Int::from(200));
assert_eq!(Int::max_value().saturating_mul(Int::from(2)), Int::max_value());
assert_eq!(Int::max_value().saturating_mul(Int::max_value()), Int::max_value());
assert_eq!(Int::max_value().saturating_mul(Int::min_value()), Int::min_value());

#[must_use] pub fn saturating_pow(self, exp: u32) -> Self[src]

Saturating integer exponentiation. Computes self.pow(exp), saturating at the numeric bounds instead of overflowing or underflowing.

Examples

Basic usage:

assert_eq!(Int::from(5).saturating_pow(2), Int::from(25));
assert_eq!(Int::from(-2).saturating_pow(3), Int::from(-8));
assert_eq!(Int::max_value().saturating_pow(2), Int::max_value());
assert_eq!(Int::min_value().saturating_pow(2), Int::max_value());

Trait Implementations

impl Clone for Int[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Default for Int[src]

impl From<i8> for Int[src]

impl From<i16> for Int[src]

impl From<i32> for Int[src]

impl From<Int> for i64[src]

impl From<Int> for f64[src]

impl From<u8> for Int[src]

impl From<u16> for Int[src]

impl From<u32> for Int[src]

impl PartialEq<Int> for Int[src]

impl Copy for Int[src]

impl Eq for Int[src]

impl Display for Int[src]

impl Debug for Int[src]

impl Div<Int> for Int[src]

type Output = Self

The resulting type after applying the / operator.

impl Add<Int> for Int[src]

type Output = Self

The resulting type after applying the + operator.

impl Sub<Int> for Int[src]

type Output = Self

The resulting type after applying the - operator.

impl Mul<Int> for Int[src]

type Output = Self

The resulting type after applying the * operator.

impl Rem<Int> for Int[src]

type Output = Self

The resulting type after applying the % operator.

impl Neg for Int[src]

type Output = Self

The resulting type after applying the - operator.

impl AddAssign<Int> for Int[src]

impl SubAssign<Int> for Int[src]

impl MulAssign<Int> for Int[src]

impl DivAssign<Int> for Int[src]

impl RemAssign<Int> for Int[src]

impl Hash for Int[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl Product<Int> for Int[src]

impl<'a> Product<&'a Int> for Int[src]

impl Sum<Int> for Int[src]

impl<'a> Sum<&'a Int> for Int[src]

impl TryFrom<i64> for Int[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<Int> for i8[src]

type Error = StdTryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<Int> for i16[src]

type Error = StdTryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<Int> for i32[src]

type Error = StdTryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<u64> for Int[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

impl FromStr for Int[src]

type Err = ParseIntError

The associated error which can be returned from parsing.

Auto Trait Implementations

impl Sync for Int

impl Send for Int

impl Unpin for Int

impl RefUnwindSafe for Int

impl UnwindSafe for Int

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]