libspecr/int/to.rs
1use crate::int::*;
2
3/// Conversion to `Int`.
4///
5/// This is implemented for primitive integer types and usable in `const`-contexts.
6pub trait ToInt {
7 /// Converts `self` to `Int`.
8 fn to_int(self) -> Int;
9}
10
11macro_rules! setup {
12 ( $( $t:ty ),* ) => {
13 $(
14 impl ToInt for $t {
15 fn to_int(self) -> Int {
16 Int(IntInner::Small(self.try_into().unwrap()))
17 }
18 }
19 )*
20 };
21}
22
23
24setup!(u8, i8, u16, i16, u32, i32, u64, i64, i128, usize, isize);
25
26// u128 doesn't fit into i128, hence heap alloc required.
27impl ToInt for u128 {
28 fn to_int(self) -> Int {
29 Int::wrap(self.into())
30 }
31}