Struct js_int::Int[][src]

pub struct Int(_);
Expand description

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

Implementations

impl Int[src]

pub const MIN: Self[src]

The smallest value that can be represented by this integer type.

Examples

Basic usage:

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

pub const MAX: Self[src]

The largest value that can be represented by this integer type.

Examples

Basic usage:

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

#[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));
assert_eq!(Int::new(js_int::MAX_SAFE_INT), Some(Int::MAX));
assert_eq!(Int::new(js_int::MIN_SAFE_INT - 1), None);
assert_eq!(Int::new(js_int::MAX_SAFE_INT + 1), None);

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!(10)));

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

👎 Deprecated:

Use UInt::MIN instead.

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]

👎 Deprecated:

Use Int::MAX instead.

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!(10).abs(), int!(10));
assert_eq!(int!(-10).abs(), int!(10));

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

#[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!(10).is_positive());
assert!(!int!(0).is_positive());
assert!(!int!(-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!(-10).is_negative());
assert!(!int!(0).is_negative());
assert!(!int!(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 - int!(1)).checked_add(int!(1)),
    Some(Int::MAX)
);
assert_eq!((Int::MAX - int!(1)).checked_add(int!(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 + int!(2)).checked_sub(int!(1)),
    Some(Int::MIN + int!(1))
);
assert_eq!((Int::MIN + int!(2)).checked_sub(int!(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!(5).checked_mul(int!(1)), Some(int!(5)));
assert_eq!(Int::MAX.checked_mul(int!(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.checked_div(int!(-1)), Some(Int::MAX));
assert_eq!(int!(1).checked_div(int!(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!(5).checked_rem(int!(2)), Some(int!(1)));
assert_eq!(int!(5).checked_rem(int!(0)), None);
assert_eq!(Int::MIN.checked_rem(int!(-1)), Some(int!(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!(8).checked_pow(2), Some(int!(64)));
assert_eq!(Int::MAX.checked_pow(2), None);
assert_eq!(Int::MIN.checked_pow(2), None);
assert_eq!(int!(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!(100).saturating_add(int!(1)), int!(101));
assert_eq!(Int::MAX.saturating_add(int!(1)), Int::MAX);

#[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!(100).saturating_sub(int!(1)), int!(99));
assert_eq!(Int::MIN.saturating_sub(int!(1)), Int::MIN);

#[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!(100).saturating_mul(int!(2)), int!(200));
assert_eq!(Int::MAX.saturating_mul(int!(2)), Int::MAX);
assert_eq!(Int::MAX.saturating_mul(Int::MAX), Int::MAX);
assert_eq!(Int::MAX.saturating_mul(Int::MIN), Int::MIN);

#[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!(5).saturating_pow(2), int!(25));
assert_eq!(int!(-2).saturating_pow(3), int!(-8));
assert_eq!(Int::MAX.saturating_pow(2), Int::MAX);
assert_eq!(Int::MIN.saturating_pow(2), Int::MAX);

Trait Implementations

impl Add<Int> for Int[src]

type Output = Self

The resulting type after applying the + operator.

fn add(self, rhs: Self) -> Self[src]

Performs the + operation. Read more

impl AddAssign<Int> for Int[src]

fn add_assign(&mut self, other: Self)[src]

Performs the += operation. Read more

impl Clone for Int[src]

fn clone(&self) -> Int[src]

Returns a copy of the value. Read more

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

Performs copy-assignment from source. Read more

impl Debug for Int[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl Default for Int[src]

fn default() -> Int[src]

Returns the “default value” for a type. Read more

impl Display for Int[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl Div<Int> for Int[src]

type Output = Self

The resulting type after applying the / operator.

fn div(self, rhs: Self) -> Self[src]

Performs the / operation. Read more

impl DivAssign<Int> for Int[src]

fn div_assign(&mut self, other: Self)[src]

Performs the /= operation. Read more

impl From<UInt> for Int[src]

fn from(val: UInt) -> Self[src]

Performs the conversion.

impl From<i16> for Int[src]

fn from(val: i16) -> Self[src]

Performs the conversion.

impl From<i32> for Int[src]

fn from(val: i32) -> Self[src]

Performs the conversion.

impl From<i8> for Int[src]

fn from(val: i8) -> Self[src]

Performs the conversion.

impl From<u16> for Int[src]

fn from(val: u16) -> Self[src]

Performs the conversion.

impl From<u32> for Int[src]

fn from(val: u32) -> Self[src]

Performs the conversion.

impl From<u8> for Int[src]

fn from(val: u8) -> Self[src]

Performs the conversion.

impl FromStr for Int[src]

type Err = ParseIntError

The associated error which can be returned from parsing.

fn from_str(src: &str) -> Result<Self, Self::Err>[src]

Parses a string s to return a value of this type. Read more

impl Hash for Int[src]

fn hash<__H: Hasher>(&self, state: &mut __H)[src]

Feeds this value into the given Hasher. Read more

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 Mul<Int> for Int[src]

type Output = Self

The resulting type after applying the * operator.

fn mul(self, rhs: Self) -> Self[src]

Performs the * operation. Read more

impl MulAssign<Int> for Int[src]

fn mul_assign(&mut self, other: Self)[src]

Performs the *= operation. Read more

impl Neg for Int[src]

type Output = Self

The resulting type after applying the - operator.

fn neg(self) -> Self[src]

Performs the unary - operation. Read more

impl Ord for Int[src]

fn cmp(&self, other: &Int) -> Ordering[src]

This method returns an Ordering between self and other. Read more

#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]

Compares and returns the maximum of two values. Read more

#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]

Compares and returns the minimum of two values. Read more

#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]

Restrict a value to a certain interval. Read more

impl PartialEq<Int> for Int[src]

fn eq(&self, other: &Int) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, other: &Int) -> bool[src]

This method tests for !=.

impl PartialOrd<Int> for Int[src]

fn partial_cmp(&self, other: &Int) -> Option<Ordering>[src]

This method returns an ordering between self and other values if one exists. Read more

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than (for self and other) and is used by the < operator. Read more

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

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

fn product<I>(iter: I) -> Self where
    I: Iterator<Item = &'a Int>, 
[src]

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

impl Product<Int> for Int[src]

fn product<I>(iter: I) -> Self where
    I: Iterator<Item = Int>, 
[src]

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

impl Rem<Int> for Int[src]

type Output = Self

The resulting type after applying the % operator.

fn rem(self, rhs: Self) -> Self[src]

Performs the % operation. Read more

impl RemAssign<Int> for Int[src]

fn rem_assign(&mut self, other: Self)[src]

Performs the %= operation. Read more

impl Sub<Int> for Int[src]

type Output = Self

The resulting type after applying the - operator.

fn sub(self, rhs: Self) -> Self[src]

Performs the - operation. Read more

impl SubAssign<Int> for Int[src]

fn sub_assign(&mut self, other: Self)[src]

Performs the -= operation. Read more

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

fn sum<I>(iter: I) -> Self where
    I: Iterator<Item = &'a Int>, 
[src]

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

impl Sum<Int> for Int[src]

fn sum<I>(iter: I) -> Self where
    I: Iterator<Item = Int>, 
[src]

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

impl TryFrom<i128> for Int[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

fn try_from(val: i128) -> Result<Self, TryFromIntError>[src]

Performs the conversion.

impl TryFrom<i64> for Int[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

fn try_from(val: i64) -> Result<Self, TryFromIntError>[src]

Performs the conversion.

impl TryFrom<u128> for Int[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

fn try_from(val: u128) -> Result<Self, TryFromIntError>[src]

Performs the conversion.

impl TryFrom<u64> for Int[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

fn try_from(val: u64) -> Result<Self, TryFromIntError>[src]

Performs the conversion.

impl Copy for Int[src]

impl Eq for Int[src]

impl StructuralEq for Int[src]

impl StructuralPartialEq for Int[src]

Auto Trait Implementations

impl RefUnwindSafe for Int

impl Send for Int

impl Sync for Int

impl Unpin for Int

impl UnwindSafe for Int

Blanket Implementations

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

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

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

pub fn from(t: T) -> T[src]

Performs the conversion.

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

pub fn into(self) -> U[src]

Performs the conversion.

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

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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

pub default fn to_string(&self) -> String[src]

Converts the given value to a String. Read more

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.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

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.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.