use crate::*;
mod ops;
mod func;
mod to;
mod step;
pub use to::ToInt;
pub(crate) use num_bigint::BigInt;
use num_traits::ToPrimitive;
#[derive(Copy, Clone, Hash, GcCompat)]
pub struct Int(IntInner);
#[derive(Copy, Clone, Debug, Hash, GcCompat)]
enum IntInner {
Big(GcCow<BigInt>),
Small(i128),
}
impl<T: ToInt> From<T> for Int {
fn from(t: T) -> Int {
t.to_int()
}
}
impl Int {
pub const fn from_u64(i: u64) -> Int {
Int(IntInner::Small(i as i128))
}
}
impl Int {
pub const ZERO: Int = Int::from_u64(0);
pub const ONE: Int = Int::from_u64(1);
pub(crate) fn into_inner(self) -> BigInt {
match self.0 {
IntInner::Big(x) => x.extract(),
IntInner::Small(x) => x.into(),
}
}
pub(crate) fn wrap(big: BigInt) -> Self {
match big.to_i128() {
Some(x) => Self(IntInner::Small(x)),
None => Self(IntInner::Big(GcCow::new(big)))
}
}
}