Skip to main content

Int

Enum Int 

Source
pub enum Int {
    Small(i64),
    Big(Box<BigInt>),
}
Expand description

A Python integer.

Cloning a big one copies its digits. That is deliberate rather than a missing Rc: keeping this plain data is what makes it Send and Sync, a literal is cloned about as often as it is created, and the object model this is a stand-in for gives every heap value a refcounted header of its own.

Variants§

§

Small(i64)

Fits in a machine word, which nearly everything does.

§

Big(Box<BigInt>)

Does not. Never holds a value that would fit in Int::Small.

Implementations§

Source§

impl Int

Source

pub const ZERO: Self

Zero.

Source

pub const fn from_i64(value: i64) -> Self

An integer from a machine word.

Source

pub fn from_big(value: BigInt) -> Self

An integer from a bignum, narrowed if it fits.

Every path that can produce a big value goes through here, which is what keeps the invariant that a Big never holds something an i64 could.

Source

pub fn parse(digits: &str, radix: u32) -> Option<Self>

The digits of an integer literal in some radix, without a sign.

Returns None if digits is empty or holds anything the radix does not allow, which is a caller bug rather than a syntax error: the lexer has already decided what a number looks like. Leading zeros are not an error, since 007 and 7 are the same integer.

Source

pub fn to_i64(&self) -> Option<i64>

This as a machine word, if it fits.

Source

pub fn to_usize(&self) -> Option<usize>

This as a usize, for the places an index or a count is wanted.

Source

pub fn to_f64(&self) -> Option<f64>

This as a double, or None when it is too large to be one.

Python raises OverflowError for that case rather than returning infinity, which is why this is not a plain f64.

Source

pub fn is_zero(&self) -> bool

Whether this is zero, which is also whether it is falsey.

Source

pub fn is_negative(&self) -> bool

Source

pub fn to_big(&self) -> BigInt

This as a bignum, whichever arm it is in.

Source

pub fn add(&self, other: &Self) -> Self

Source

pub fn sub(&self, other: &Self) -> Self

Source

pub fn mul(&self, other: &Self) -> Self

Source

pub fn bitand(&self, other: &Self) -> Self

Source

pub fn bitor(&self, other: &Self) -> Self

Source

pub fn bitxor(&self, other: &Self) -> Self

Source

pub fn neg(&self) -> Self

-self.

Source

pub fn invert(&self) -> Self

~self, which is -self - 1 on the infinite two’s complement.

Source

pub fn abs(&self) -> Self

Source

pub fn floor_div(&self, other: &Self) -> Result<Self, DivideByZero>

self // other, flooring toward negative infinity as Python does.

Source

pub fn modulo(&self, other: &Self) -> Result<Self, DivideByZero>

self % other, taking the sign of the divisor as Python does.

Source

pub fn div_mod(&self, other: &Self) -> Result<(Self, Self), DivideByZero>

divmod(self, other), which is the quotient and remainder together.

Both are derived from the truncating pair rather than taken from the underlying type, so a change of arm cannot change the answer. The correction is the same in either arm: when the remainder is non-zero and its sign disagrees with the divisor, the truncating quotient is one too large and the remainder is a whole divisor short.

Source

pub fn true_div(&self, other: &Self) -> Result<Option<f64>, DivideByZero>

self / other, which in Python is always a float.

None means the true quotient is out of range for a double, which Python reports as OverflowError rather than as infinity.

Source

pub fn pow(&self, exponent: &Self) -> Option<Self>

self ** exponent for a non-negative exponent.

None for a negative one, which Python answers with a float rather than an integer, and for an exponent so large that the result could not be held. Both are the caller’s to turn into the right thing.

Source

pub fn shl(&self, places: &Self) -> Option<Self>

self << places, which needs a non-negative count.

None for a negative one, which Python reports as ValueError: negative shift count, and for a count large enough that the result would not fit in memory.

Source

pub fn shr(&self, places: &Self) -> Option<Self>

self >> places, which needs a non-negative count.

An arithmetic shift, so a negative number shifted far enough lands on -1 rather than on zero, which is what flooring means here.

Trait Implementations§

Source§

impl Clone for Int

Source§

fn clone(&self) -> Int

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 Display for Int

Source§

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

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

impl Eq for Int

Source§

impl From<BigInt> for Int

Source§

fn from(value: BigInt) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Int

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl Ord for Int

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Int

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Int

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 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 UnsafeUnpin 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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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§

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 = !

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.