js_int

Struct Int

Source
pub struct Int(/* private fields */);
Expand description

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

Implementations§

Source§

impl Int

Source

pub const MIN: Self = _

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

pub const MAX: Self = _

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

pub fn new(val: i64) -> Option<Self>

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

pub fn new_saturating(val: i64) -> Self

Creates an Int from the given i64 clamped to the safe interval.

The given value gets clamped into the closed interval between MIN_SAFE_INT and MAX_SAFE_INT.

§Examples

Basic usage:

assert_eq!(Int::new_saturating(0), int!(0));
assert_eq!(Int::new_saturating(js_int::MAX_SAFE_INT), Int::MAX);
assert_eq!(Int::new_saturating(js_int::MAX_SAFE_INT + 1), Int::MAX);
assert_eq!(Int::new_saturating(js_int::MIN_SAFE_INT), Int::MIN);
assert_eq!(Int::new_saturating(js_int::MIN_SAFE_INT - 1), Int::MIN);
Source

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

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

pub const fn min_value() -> Self

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

pub const fn max_value() -> Self

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

pub fn abs(self) -> Self

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

pub const fn is_positive(self) -> bool

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

pub const fn is_negative(self) -> bool

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

pub fn checked_add(self, rhs: Self) -> Option<Self>

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

pub fn checked_sub(self, rhs: Self) -> Option<Self>

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

pub fn checked_mul(self, rhs: Self) -> Option<Self>

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

pub fn checked_div(self, rhs: Self) -> Option<Self>

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

pub fn checked_rem(self, rhs: Self) -> Option<Self>

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

pub fn checked_pow(self, exp: u32) -> Option<Self>

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

pub fn saturating_add(self, rhs: Self) -> Self

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);
assert_eq!(Int::MIN.saturating_add(int!(-1)), Int::MIN);
Source

pub fn saturating_sub(self, rhs: Self) -> Self

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);
assert_eq!(Int::MAX.saturating_sub(int!(-1)), Int::MAX);
Source

pub fn saturating_mul(self, rhs: Self) -> Self

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

pub fn saturating_pow(self, exp: u32) -> Self

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§

Source§

impl Add for Int

Source§

type Output = Int

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl AddAssign for Int

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl Clone for Int

Source§

fn clone(&self) -> Int

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Int

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Int

Source§

fn default() -> Int

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

impl<'de> Deserialize<'de> for Int

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Int

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Div for Int

Source§

type Output = Int

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self

Performs the / operation. Read more
Source§

impl DivAssign for Int

Source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
Source§

impl From<Int> for f64

Source§

fn from(val: Int) -> Self

Converts to this type from the input type.
Source§

impl From<Int> for i128

Source§

fn from(val: Int) -> Self

Converts to this type from the input type.
Source§

impl From<Int> for i64

Source§

fn from(val: Int) -> Self

Converts to this type from the input type.
Source§

impl From<UInt> for Int

Source§

fn from(val: UInt) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Int

Source§

fn from(val: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Int

Source§

fn from(val: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Int

Source§

fn from(val: i8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Int

Source§

fn from(val: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Int

Source§

fn from(val: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Int

Source§

fn from(val: u8) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Int

Source§

type Err = ParseIntError

The associated error which can be returned from parsing.
Source§

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

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

impl Hash for Int

Source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

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

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul for Int

Source§

type Output = Int

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
Source§

impl MulAssign for Int

Source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
Source§

impl Neg for Int

Source§

type Output = Int

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Ord for Int

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Int

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Int

Source§

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

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

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Product<&'a Int> for Int

Source§

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

Takes an iterator and generates Self from the elements by multiplying the items.
Source§

impl Product for Int

Source§

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

Takes an iterator and generates Self from the elements by multiplying the items.
Source§

impl Rem for Int

Source§

type Output = Int

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Self) -> Self

Performs the % operation. Read more
Source§

impl RemAssign for Int

Source§

fn rem_assign(&mut self, other: Self)

Performs the %= operation. Read more
Source§

impl Serialize for Int

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub for Int

Source§

type Output = Int

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl SubAssign for Int

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl<'a> Sum<&'a Int> for Int

Source§

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

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl Sum for Int

Source§

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

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl TryFrom<Int> for i16

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(val: Int) -> Result<Self, TryFromIntError>

Performs the conversion.
Source§

impl TryFrom<Int> for i32

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(val: Int) -> Result<Self, TryFromIntError>

Performs the conversion.
Source§

impl TryFrom<Int> for i8

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(val: Int) -> Result<Self, TryFromIntError>

Performs the conversion.
Source§

impl TryFrom<Int> for isize

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(val: Int) -> Result<Self, TryFromIntError>

Performs the conversion.
Source§

impl TryFrom<Int> for u16

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(val: Int) -> Result<Self, TryFromIntError>

Performs the conversion.
Source§

impl TryFrom<Int> for u32

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(val: Int) -> Result<Self, TryFromIntError>

Performs the conversion.
Source§

impl TryFrom<Int> for u8

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(val: Int) -> Result<Self, TryFromIntError>

Performs the conversion.
Source§

impl TryFrom<Int> for usize

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(val: Int) -> Result<Self, TryFromIntError>

Performs the conversion.
Source§

impl TryFrom<i128> for Int

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl TryFrom<i64> for Int

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl TryFrom<isize> for Int

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(val: isize) -> Result<Self, TryFromIntError>

Performs the conversion.
Source§

impl TryFrom<u128> for Int

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl TryFrom<u64> for Int

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl TryFrom<usize> for Int

Source§

type Error = TryFromIntError

The type returned in the event of a conversion error.
Source§

fn try_from(val: usize) -> Result<Self, TryFromIntError>

Performs the conversion.
Source§

impl Copy for Int

Source§

impl Eq for Int

Source§

impl StructuralPartialEq for Int

Auto Trait Implementations§

§

impl Freeze for Int

§

impl RefUnwindSafe for Int

§

impl Send for Int

§

impl Sync for Int

§

impl Unpin for Int

§

impl UnwindSafe for Int

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,