1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::*;

mod ops;
mod func;
mod to;
mod step;

pub use to::ToInt;

/// The external Bigint Type, which we use under the hood.
pub(crate) use num_bigint::BigInt as ExtInt;
use num_traits::ToPrimitive;

#[derive(Copy, Clone, Debug, Hash, GcCompat)]
/// Garbage collected big integer that implements `Copy` and supports construction in `const` contexts.
pub struct Int(IntInner);

// IntInner only exists to hide the enum implementation details.
#[derive(Copy, Clone, Debug, Hash, GcCompat)]
enum IntInner {
    Big(GcCow<ExtInt>),
    /// i128 is used to contain u64 and i64.
    Small(i128),
}

impl<T: ~const ToInt> const From<T> for Int {
    fn from(t: T) -> Int {
        t.to_int()
    }
}

impl Int {
    /// The number 0
    pub const ZERO: Int = Int::from(0);
    /// The number 1
    pub const ONE: Int = Int::from(1);

    pub(crate) fn ext(self) -> ExtInt {
        match self.0 {
            IntInner::Big(x) => x.get(),
            IntInner::Small(x) => x.into(),
        }
    }

    pub(crate) fn wrap(ext: ExtInt) -> Self {
        match ext.to_i128() {
            Some(x) => Self(IntInner::Small(x)),
            None => Self(IntInner::Big(GcCow::new(ext)))
        }
    }
}