spade_common/
num_ext.rs

1use num::{BigInt, BigUint, FromPrimitive};
2
3pub trait InfallibleToBigInt {
4    fn to_bigint(self) -> BigInt;
5}
6
7macro_rules! infallible_int_primitives {
8    ($($ty:ty, $from_fn:ident);*) => {
9        $(
10            impl InfallibleToBigInt for $ty {
11                fn to_bigint(self) -> BigInt {
12                    num::BigInt::$from_fn(self).unwrap()
13                }
14            }
15        )*
16    }
17}
18
19infallible_int_primitives!(
20    i8, from_i8;
21    i16, from_i16;
22    i32, from_i32;
23    i64, from_i64;
24    i128, from_i128;
25    isize, from_isize;
26    u8, from_u8;
27    u16, from_u16;
28    u32, from_u32;
29    u64, from_u64;
30    u128, from_u128;
31    usize, from_usize
32);
33
34impl InfallibleToBigInt for &BigUint {
35    fn to_bigint(self) -> BigInt {
36        num::bigint::ToBigInt::to_bigint(self).unwrap()
37    }
38}
39impl InfallibleToBigInt for BigUint {
40    fn to_bigint(self) -> BigInt {
41        num::bigint::ToBigInt::to_bigint(&self).unwrap()
42    }
43}
44
45pub trait InfallibleToBigUint {
46    fn to_biguint(self) -> BigUint;
47}
48
49macro_rules! infallible_uint_primitives {
50    ($($ty:ty, $from_fn:ident);*) => {
51        $(
52            impl InfallibleToBigUint for $ty {
53                fn to_biguint(self) -> BigUint {
54                    num::BigUint::$from_fn(self).unwrap()
55                }
56            }
57        )*
58    }
59}
60
61infallible_uint_primitives!(
62    u8, from_u8;
63    u16, from_u16;
64    u32, from_u32;
65    u64, from_u64;
66    u128, from_u128;
67    usize, from_usize
68);