Struct Int

Source
pub struct Int<'l>(/* private fields */);
Expand description

An arbitrary precision integer optimized for small values.

The implementation optimizes for the common case where integers are small enough to fit in native types, while gracefully handling arbitrarily large integers through string representation: small integers that fit in i128 are stored on the stack, while larger integers are stored as validated decimal strings. This avoids the overhead of big integer libraries for typical use cases.

§Examples

use openmath::Int;

// Create from various integer types
let a = Int::from(42u8);
let b = Int::from(-123i64);
let c = Int::from(i128::MAX);

// Small integers are stored efficiently
let small = Int::from(42);
assert_eq!(small.is_i128(), Some(42));

// Create from strings for big integers
let big = Int::new("999999999999999999999999999999999999999999").expect("should be defined");
assert!(big.is_big().is_some());

// representation is chosen automatically:
let small = Int::new("-42").expect("should be defined");
assert_eq!(small.is_i128(), Some(-42));

Implementations§

Source§

impl Int<'_>

Source

pub const fn is_i128(&self) -> Option<i128>

Returns the value as an i128 if it fits, otherwise None.

This method allows you to check if the integer is small enough to be represented as a native i128 value.

§Examples
use openmath::Int;

let small = Int::from(42);
assert_eq!(small.is_i128(), Some(42));

let big = Int::new("999999999999999999999999999999999999999999").expect("should be defined");
assert_eq!(big.is_i128(), None);
Source

pub fn is_big(&self) -> Option<&str>

Returns the value as a string slice if it’s a big integer, otherwise None.

This method allows you to access the string representation of large integers that don’t fit in i128.

§Examples
use openmath::Int;

let small = Int::from(42);
assert_eq!(small.is_big(), None);

let big = Int::new("999999999999999999999999999999999999999999").expect("should be defined");
assert_eq!(big.is_big(), Some("999999999999999999999999999999999999999999"));
Source

pub fn new(num: &str) -> Option<Int<'_>>

Creates a new Int from a string slice.

The string must represent a valid decimal integer, optionally with a leading sign (+ or -). Returns None if the string is not a valid integer.

§Examples
use openmath::Int;

assert!(Int::new("42").is_some());
assert!(Int::new("-123").is_some());
assert!(Int::new("+456").is_some());
assert!(Int::new("999999999999999999999999999999999999999999").is_some());

// Invalid formats
assert!(Int::new("12.34").is_none());
assert!(Int::new("abc").is_none());
assert!(Int::new("").is_none());
Source

pub fn from_string(num: String) -> Option<Int<'static>>

Creates a new Int from an owned string.

Similar to new, but takes ownership of the string for cases where you want a 'static lifetime integer.

§Examples
use openmath::Int;

let big_num = "12345678901234567890123456789012345678901234567890".to_string();
let int = Int::from_string(big_num).unwrap();
assert!(int.is_big().is_some());
Source

pub const fn is_zero(&self) -> bool

Returns true if this integer represents zero.

§Examples
use openmath::Int;

assert!(Int::from(0).is_zero());
assert!(Int::new("0").expect("should be defined").is_zero());
assert!(Int::new("+0").expect("should be defined").is_zero());
assert!(Int::new("-0").expect("should be defined").is_zero());
assert!(!Int::from(1).is_zero());
Source

pub fn is_positive(&self) -> bool

Returns true if this integer is positive (> 0).

§Examples
use openmath::Int;

assert!(Int::from(1).is_positive());
assert!(Int::new("999999999999999999999999999999999999999999").expect("should be defined").is_positive());
assert!(!Int::from(0).is_positive());
assert!(!Int::from(-1).is_positive());
Source

pub fn is_negative(&self) -> bool

Returns true if this integer is negative (< 0).

§Examples
use openmath::Int;

assert!(Int::from(-1).is_negative());
assert!(Int::new("-999999999999999999999999999999999999999999").expect("should be defined").is_negative());
assert!(!Int::from(0).is_negative());
assert!(!Int::from(1).is_negative());
Source

pub fn into_owned(self) -> Int<'static>

Trait Implementations§

Source§

impl<'l> Clone for Int<'l>

Source§

fn clone(&self) -> Int<'l>

Returns a duplicate 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<'l> Debug for Int<'l>

Source§

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

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

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

Available on crate feature serde only.
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<'l> From<i128> for Int<'l>

Source§

fn from(value: i128) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> From<i16> for Int<'l>

Source§

fn from(value: i16) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> From<i32> for Int<'l>

Source§

fn from(value: i32) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> From<i64> for Int<'l>

Source§

fn from(value: i64) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> From<i8> for Int<'l>

Source§

fn from(value: i8) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> From<isize> for Int<'l>

Source§

fn from(value: isize) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> From<u16> for Int<'l>

Source§

fn from(value: u16) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> From<u32> for Int<'l>

Source§

fn from(value: u32) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> From<u64> for Int<'l>

Source§

fn from(value: u64) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> From<u8> for Int<'l>

Source§

fn from(value: u8) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> From<usize> for Int<'l>

Source§

fn from(value: usize) -> Int<'l>

Converts to this type from the input type.
Source§

impl<'l> Hash for Int<'l>

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<'d> OMDeserializable<'d> for Int<'d>

Source§

type Ret = Int<'d>

The type returned by from_openmath; Can be Self, but can also be something more complex so that OM-values can be “deferred” until enough information is there to construct a Self; See above for an example.
Source§

type Err = &'static str

The type of errors that can occur during deserialization.
Source§

fn from_openmath(om: OM<'d, Self>, _: &str) -> Result<Self, Self::Err>
where Self: Sized,

Attempt to deserialize an OpenMath object into this type. Read more
Source§

fn from_openmath_xml(input: &'de str) -> Result<Self, XmlReadError<Self::Err>>
where Self: Sized,

Available on crate feature xml only.
Deserializes self from a string of OpenMath XML. Read more
Source§

impl OMSerializable for Int<'_>

Source§

fn as_openmath<'s, S: OMSerializer<'s>>( &self, serializer: S, ) -> Result<S::Ok, S::Err>

Serialize this value using the provided serializer. Read more
Source§

fn cdbase(&self) -> Option<&str>

Source§

fn openmath_display(&self) -> impl Display + Debug + use<'_, Self>

OpenMath-style Debug and Display implementations Read more
Source§

fn openmath_serde(&self) -> impl Serialize + use<'_, Self>

Available on crate feature serde only.
Create a serde-compatible serializer wrapper. Read more
Source§

fn xml(&self, pretty: bool) -> impl Display

Returns something that Displays as the OpenMath XML of this object.
Source§

fn omobject(&self) -> OMObject<'_, Self>

returns this element as something that serializes into an OMOBJ; i.e. a “top-level” OpenMath object.
Source§

impl<'l> Ord for Int<'l>

Source§

fn cmp(&self, other: &Int<'l>) -> 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<'l> PartialEq for Int<'l>

Source§

fn eq(&self, other: &Int<'l>) -> 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<'l> PartialOrd for Int<'l>

Source§

fn partial_cmp(&self, other: &Int<'l>) -> 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<'l> Serialize for Int<'l>

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<'l> TryFrom<&'l str> for Int<'l>

Source§

type Error = ()

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

fn try_from(value: &'l str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'l> TryFrom<Cow<'l, str>> for Int<'l>

Source§

type Error = ()

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

fn try_from(value: Cow<'l, str>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<String> for Int<'_>

Source§

type Error = ()

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

fn try_from(value: String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'l> Eq for Int<'l>

Source§

impl<'l> StructuralPartialEq for Int<'l>

Auto Trait Implementations§

§

impl<'l> Freeze for Int<'l>

§

impl<'l> RefUnwindSafe for Int<'l>

§

impl<'l> Send for Int<'l>

§

impl<'l> Sync for Int<'l>

§

impl<'l> Unpin for Int<'l>

§

impl<'l> UnwindSafe for Int<'l>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<O> OMOrForeign for O
where O: OMSerializable,

Source§

fn om_or_foreign( self, ) -> Either<impl OMSerializable, (Option<impl Display>, impl Display)>

Returns either an OMSerializable, or a pair (encoding:Option<Display>,foreign:Display)
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 = 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.