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
impl Int
Sourcepub fn from_big(value: BigInt) -> Self
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.
Sourcepub fn parse(digits: &str, radix: u32) -> Option<Self>
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.
Sourcepub fn to_usize(&self) -> Option<usize>
pub fn to_usize(&self) -> Option<usize>
This as a usize, for the places an index or a count is wanted.
Sourcepub fn to_f64(&self) -> Option<f64>
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.
pub fn is_negative(&self) -> bool
pub fn add(&self, other: &Self) -> Self
pub fn sub(&self, other: &Self) -> Self
pub fn mul(&self, other: &Self) -> Self
pub fn bitand(&self, other: &Self) -> Self
pub fn bitor(&self, other: &Self) -> Self
pub fn bitxor(&self, other: &Self) -> Self
pub fn abs(&self) -> Self
Sourcepub fn floor_div(&self, other: &Self) -> Result<Self, DivideByZero>
pub fn floor_div(&self, other: &Self) -> Result<Self, DivideByZero>
self // other, flooring toward negative infinity as Python does.
Sourcepub fn modulo(&self, other: &Self) -> Result<Self, DivideByZero>
pub fn modulo(&self, other: &Self) -> Result<Self, DivideByZero>
self % other, taking the sign of the divisor as Python does.
Sourcepub fn div_mod(&self, other: &Self) -> Result<(Self, Self), DivideByZero>
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.
Sourcepub fn true_div(&self, other: &Self) -> Result<Option<f64>, DivideByZero>
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.
Sourcepub fn pow(&self, exponent: &Self) -> Option<Self>
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.