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<'_>
impl Int<'_>
Sourcepub const fn is_i128(&self) -> Option<i128>
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);Sourcepub fn is_big(&self) -> Option<&str>
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"));Sourcepub fn new(num: &str) -> Option<Int<'_>>
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());Sourcepub fn from_string(num: String) -> Option<Int<'static>>
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());Sourcepub const fn is_zero(&self) -> bool
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());Sourcepub fn is_positive(&self) -> bool
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());Sourcepub fn is_negative(&self) -> bool
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());pub fn into_owned(self) -> Int<'static>
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Int<'de>
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Int<'de>
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'d> OMDeserializable<'d> for Int<'d>
impl<'d> OMDeserializable<'d> for Int<'d>
Source§type Ret = Int<'d>
type Ret = Int<'d>
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§impl OMSerializable for Int<'_>
impl OMSerializable for Int<'_>
Source§fn as_openmath<'s, S: OMSerializer<'s>>(
&self,
serializer: S,
) -> Result<S::Ok, S::Err>
fn as_openmath<'s, S: OMSerializer<'s>>( &self, serializer: S, ) -> Result<S::Ok, S::Err>
fn cdbase(&self) -> Option<&str>
Source§fn openmath_display(&self) -> impl Display + Debug + use<'_, Self>
fn openmath_display(&self) -> impl Display + Debug + use<'_, Self>
Source§fn openmath_serde(&self) -> impl Serialize + use<'_, Self>
fn openmath_serde(&self) -> impl Serialize + use<'_, Self>
serde only.Source§impl<'l> Ord for Int<'l>
impl<'l> Ord for Int<'l>
Source§impl<'l> PartialOrd for Int<'l>
impl<'l> PartialOrd for Int<'l>
impl<'l> Eq for Int<'l>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<O> OMOrForeign for Owhere
O: OMSerializable,
impl<O> OMOrForeign for Owhere
O: OMSerializable,
Source§fn om_or_foreign(
self,
) -> Either<impl OMSerializable, (Option<impl Display>, impl Display)>
fn om_or_foreign( self, ) -> Either<impl OMSerializable, (Option<impl Display>, impl Display)>
OMSerializable, or a pair
(encoding:Option<Display>,foreign:Display)