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;
pub(crate) use num_bigint::BigInt as ExtInt;
use num_traits::ToPrimitive;
#[derive(Copy, Clone, Debug, Hash, GcCompat)]
pub struct Int(IntInner);
#[derive(Copy, Clone, Debug, Hash, GcCompat)]
enum IntInner {
Big(GcCow<ExtInt>),
Small(i128),
}
impl<T: ~const ToInt> const From<T> for Int {
fn from(t: T) -> Int {
t.to_int()
}
}
impl Int {
pub const ZERO: Int = Int::from(0);
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)))
}
}
}