Skip to main content

Int

Enum Int 

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

An integer: inline when it fits in an i64, otherwise a shared big integer.

Constructors keep the representation canonical (Big only outside i64 range); equality, ordering and hashing are numeric even for a hand-built non-canonical Big.

Variants§

§

Small(i64)

A value in i64 range.

§

Big(Arc<BigInt>)

A value outside i64 range.

Implementations§

Source§

impl Int

Source

pub const ZERO: Self

Zero.

Source

pub const ONE: Self

One.

Source

pub fn from_big(b: BigInt) -> Self

Converts a big integer, keeping the representation canonical.

Source

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

The value as an i64, if it fits.

Source

pub fn to_i32(&self) -> Option<i32>

The value as an i32 (a TLC IntValue), if it fits.

Source

pub fn to_big(&self) -> BigInt

The value as a big integer.

Source

pub fn is_negative(&self) -> bool

Whether the value is negative.

Source

pub fn from_literal(digits: &str, mode: IntMode) -> Result<Self>

Parses a decimal numeral as written in a spec.

In IntMode::Tlc a numeral above 2^31 - 1 is EC.TLC_INTEGER_TOO_BIG, even when it is the operand of unary minus (-2147483648 is rejected, as in TLC).

§Errors

ErrorKind::IntegerTooBig in TLC mode; ErrorKind::Argument if digits is not a non-empty string of ASCII digits.

Source

pub fn from_literal_value( value: BigInt, text: &str, mode: IntMode, ) -> Result<Self>

Accepts a numeral already converted to a big integer (for \b, \o, \h numerals). text is the numeral as SANY prints it, used in the error message.

§Errors

ErrorKind::IntegerTooBig in TLC mode when the value exceeds 2^31 - 1.

Source

pub fn add(&self, other: &Self, mode: IntMode) -> Result<Self>

a + b.

§Errors

ErrorKind::Overflow in TLC mode (Overflow when computing a+b).

Source

pub fn sub(&self, other: &Self, mode: IntMode) -> Result<Self>

a - b.

§Errors

ErrorKind::Overflow in TLC mode (Overflow when computing a-b).

Source

pub fn mul(&self, other: &Self, mode: IntMode) -> Result<Self>

a * b.

§Errors

ErrorKind::Overflow in TLC mode (Overflow when computing a*b).

Source

pub fn neg(&self, mode: IntMode) -> Result<Self>

Unary minus, -a (Integers.Neg).

§Errors

ErrorKind::Overflow in TLC mode for -(-2^31) (Overflow when computing --2147483648).

Source

pub fn div(&self, other: &Self, mode: IntMode) -> Result<Self>

Floored division, a \div b (Integers.Divide).

§Errors

ErrorKind::DivisionByZero when b = 0; ErrorKind::Overflow in TLC mode for -2^31 \div -1.

Source

pub fn div_naturals(&self, other: &Self, mode: IntMode) -> Result<Self>

a \div b as Naturals.Divide computes it, for specifications that load Naturals but not Integers.

In TLC mode this ports tlc2.module.Naturals.Divide (tlaplus 94d0c50): Java’s truncating int division, decremented when the quotient is negative and inexact. A quotient that truncates to 0 is therefore never decremented, so -1 \div 2 and 1 \div -2 are 0, not the floored -1; and -2147483648 \div -1 wraps to -2147483648 instead of overflowing. In big mode it is floored division, like Int::div.

use dive_value::{Int, IntMode};
let q = |a: i64, b: i64| Int::from(a).div_naturals(&Int::from(b), IntMode::Tlc);
assert_eq!(q(-1, 2)?, Int::from(0_i64));
assert_eq!(q(-7, 2)?, Int::from(-4_i64));
assert_eq!(q(-2_147_483_648, -1)?, Int::from(-2_147_483_648_i64));
assert_eq!(Int::from(-1_i64).div(&Int::from(2_i64), IntMode::Tlc)?, Int::from(-1_i64));
§Errors

ErrorKind::DivisionByZero when b = 0.

Source

pub fn rem(&self, other: &Self, mode: IntMode) -> Result<Self>

a % b, defined for b > 0, with a result in 0 .. b-1 (Integers.Mod).

§Errors

ErrorKind::Argument when b <= 0.

Source

pub fn pow(&self, other: &Self, mode: IntMode) -> Result<Self>

a ^ b for b >= 0 (Integers.Expt).

§Errors

ErrorKind::Argument when b < 0; ErrorKind::NullPowerNull for 0^0; ErrorKind::Overflow in TLC mode (Overflow when computing a^b); ErrorKind::DiveLimit in big mode when the result would exceed 2^26 bits.

Trait Implementations§

Source§

impl Clone for Int

Source§

fn clone(&self) -> Self

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<Int> for Value

Source§

fn from(v: Int) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Int

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Int

Source§

fn from(v: i64) -> Self

Converts to this type from the input type.
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 Ord for Int

Numeric order. A Big that fits in an i64 (possible only by building the variant directly) still compares by value.

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: &Self) -> 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

Auto Trait Implementations§

§

impl Forget for Int

§

impl Freeze for Int

§

impl Move 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, !>

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.