/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bnum-0.12.1/src/buint/convert.rs
Line | Count | Source |
1 | | macro_rules! from_uint { |
2 | | ($BUint: ident, $Digit: ident; $($uint: tt),*) => { |
3 | | $( |
4 | | impl<const N: usize> From<$uint> for $BUint<N> { |
5 | | #[inline] |
6 | 0 | fn from(int: $uint) -> Self { |
7 | | const UINT_BITS: usize = $uint::BITS as usize; |
8 | 0 | let mut out = Self::ZERO; |
9 | 0 | let mut i = 0; |
10 | 0 | while i << crate::digit::$Digit::BIT_SHIFT < UINT_BITS { |
11 | 0 | let d = (int >> (i << crate::digit::$Digit::BIT_SHIFT)) as $Digit; |
12 | 0 | if d != 0 { |
13 | 0 | out.digits[i] = d; |
14 | 0 | } |
15 | 0 | i += 1; |
16 | | } |
17 | 0 | out |
18 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::From<u8>>::from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::From<u16>>::from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::From<u32>>::from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::From<u64>>::from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::From<u128>>::from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::From<usize>>::from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::From<u8>>::from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::From<u8>>::from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::From<u16>>::from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::From<u32>>::from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::From<u64>>::from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::From<u128>>::from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::From<usize>>::from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::From<u16>>::from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::From<u32>>::from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::From<u64>>::from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::From<u128>>::from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::From<usize>>::from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::From<u8>>::from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::From<u16>>::from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::From<u32>>::from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::From<u64>>::from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::From<u128>>::from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::From<usize>>::from |
19 | | } |
20 | | )* |
21 | | } |
22 | | } |
23 | | |
24 | | macro_rules! try_from_iint { |
25 | | ($BUint: ident; $($int: tt -> $uint: tt),*) => { |
26 | | $( |
27 | | impl<const N: usize> TryFrom<$int> for $BUint<N> { |
28 | | type Error = TryFromIntError; |
29 | | |
30 | | #[inline] |
31 | 0 | fn try_from(int: $int) -> Result<Self, Self::Error> { |
32 | 0 | if int.is_negative() { |
33 | 0 | return Err(TryFromIntError(())); |
34 | 0 | } |
35 | 0 | let bits = int as $uint; |
36 | 0 | Ok(Self::from(bits)) |
37 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::TryFrom<i32>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::TryFrom<isize>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::TryFrom<i64>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::TryFrom<i128>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::TryFrom<i8>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::TryFrom<i16>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::TryFrom<i8>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::TryFrom<i16>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::TryFrom<i32>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::TryFrom<isize>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::TryFrom<i64>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::TryFrom<i128>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::TryFrom<i8>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::TryFrom<i16>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::TryFrom<i8>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::TryFrom<i16>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::TryFrom<i32>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::TryFrom<isize>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::TryFrom<i64>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::TryFrom<i128>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::TryFrom<i32>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::TryFrom<isize>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::TryFrom<i64>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::TryFrom<i128>>::try_from |
38 | | } |
39 | | )* |
40 | | } |
41 | | } |
42 | | |
43 | | macro_rules! try_from_buint { |
44 | | ($BUint: ident, $Digit: ident; $($int: ty), *) => { |
45 | | $( |
46 | | impl<const N: usize> TryFrom<$BUint<N>> for $int { |
47 | | type Error = TryFromIntError; |
48 | | |
49 | | #[inline] |
50 | 0 | fn try_from(u: $BUint<N>) -> Result<$int, Self::Error> { |
51 | 0 | let mut out = 0; |
52 | 0 | let mut i = 0; |
53 | 0 | if $Digit::BITS > <$int>::BITS { |
54 | 0 | let small = u.digits[i] as $int; |
55 | 0 | let trunc = small as $Digit; |
56 | 0 | if u.digits[i] != trunc { |
57 | 0 | return Err(TryFromIntError(())); |
58 | 0 | } |
59 | 0 | out = small; |
60 | 0 | i = 1; |
61 | | } else { |
62 | | loop { |
63 | 0 | let shift = i << crate::digit::$Digit::BIT_SHIFT; |
64 | 0 | if i >= N || shift >= <$int>::BITS as usize { |
65 | 0 | break; |
66 | 0 | } |
67 | 0 | out |= u.digits[i] as $int << shift; |
68 | 0 | i += 1; |
69 | | } |
70 | | } |
71 | | |
72 | | #[allow(unused_comparisons)] |
73 | 0 | if out < 0 { |
74 | 0 | return Err(TryFromIntError(())); |
75 | 0 | } |
76 | | |
77 | 0 | while i < N { |
78 | 0 | if u.digits[i] != 0 { |
79 | 0 | return Err(TryFromIntError(())); |
80 | 0 | } |
81 | 0 | i += 1; |
82 | | } |
83 | | |
84 | 0 | Ok(out) |
85 | 0 | } Unexecuted instantiation: <u128 as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <usize as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <i8 as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <i16 as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <i32 as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <i64 as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <i128 as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <isize as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <u8 as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <u16 as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <u32 as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <u64 as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <u128 as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <usize as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <i8 as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <i16 as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <i32 as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <i64 as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <i128 as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <isize as core::convert::TryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <u8 as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <u16 as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <u32 as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <u64 as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <u128 as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <usize as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <i8 as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <i16 as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <i32 as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <i64 as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <i128 as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <isize as core::convert::TryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <u8 as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <u16 as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <u32 as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <u64 as core::convert::TryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <u8 as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <u16 as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <u32 as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <u64 as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <u128 as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <usize as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <i8 as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <i16 as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <i32 as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <i64 as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <i128 as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <isize as core::convert::TryFrom<bnum::buint::BUint<_>>>::try_from |
86 | | } |
87 | | )* |
88 | | }; |
89 | | } |
90 | | |
91 | | macro_rules! uint_try_from_uint { |
92 | | ($Trait: ident; $To: ident; $($From: ident $(<$N: ident>)?), *) => { |
93 | | $( |
94 | | impl<$(const $N: usize,)? const M: usize> $Trait<$From $(<$N>)?> for $To<M> { |
95 | | type Error = TryFromIntError; |
96 | | |
97 | 0 | fn try_from(from: $From $(<$N>)?) -> Result<Self, Self::Error> { |
98 | 0 | if $From $(::<$N>)?::BITS <= Self::BITS || $From $(::<$N>)?::BITS - from.leading_zeros() <= Self::BITS { |
99 | 0 | Ok(Self::cast_from(from)) |
100 | | } else { |
101 | 0 | Err(TryFromIntError(())) |
102 | | } |
103 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::BTryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::BTryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::BTryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::BTryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::BTryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::BTryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::BTryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::BTryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::BTryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::BTryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::BTryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::BTryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::BTryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::BTryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::BTryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::BTryFrom<bnum::buint::BUintD8<_>>>::try_from |
104 | | } |
105 | | )* |
106 | | }; |
107 | | } |
108 | | |
109 | | macro_rules! uint_try_from_int { |
110 | | ($Trait: ident; $To: ident; $($From: ident $(<$N: ident>)?), *) => { |
111 | | $( |
112 | | impl<$(const $N: usize,)? const M: usize> $Trait<$From $(<$N>)?> for $To<M> { |
113 | | type Error = TryFromIntError; |
114 | | |
115 | 0 | fn try_from(from: $From $(<$N>)?) -> Result<Self, Self::Error> { |
116 | 0 | if from.is_negative() { |
117 | 0 | Err(TryFromIntError(())) |
118 | | } else { |
119 | 0 | if $From $(::<$N>)?::BITS.saturating_sub(1) <= Self::BITS || $From $(::<$N>)?::BITS - from.leading_zeros() <= Self::BITS { |
120 | 0 | Ok(Self::cast_from(from)) |
121 | | } else { |
122 | 0 | Err(TryFromIntError(())) |
123 | | } |
124 | | } |
125 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::BTryFrom<bnum::bint::BInt<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::BTryFrom<bnum::bint::BIntD32<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::BTryFrom<bnum::bint::BIntD16<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::BTryFrom<bnum::bint::BIntD8<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::BTryFrom<bnum::bint::BInt<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::BTryFrom<bnum::bint::BIntD32<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::BTryFrom<bnum::bint::BIntD16<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::BTryFrom<bnum::bint::BIntD8<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::BTryFrom<bnum::bint::BInt<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::BTryFrom<bnum::bint::BIntD32<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::BTryFrom<bnum::bint::BIntD16<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::BTryFrom<bnum::bint::BIntD8<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::BTryFrom<bnum::bint::BInt<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::BTryFrom<bnum::bint::BIntD32<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::BTryFrom<bnum::bint::BIntD16<_>>>::try_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::BTryFrom<bnum::bint::BIntD8<_>>>::try_from |
126 | | } |
127 | | )* |
128 | | }; |
129 | | } |
130 | | |
131 | | macro_rules! int_try_from_uint { |
132 | | ($Trait: ident; $To: ident; $($From: ident $(<$N: ident>)?), *) => { |
133 | | $( |
134 | | impl<$(const $N: usize,)? const M: usize> $Trait<$From $(<$N>)?> for $To<M> { |
135 | | type Error = TryFromIntError; |
136 | | |
137 | 0 | fn try_from(from: $From $(<$N>)?) -> Result<Self, Self::Error> { |
138 | 0 | if $From $(::<$N>)?::BITS <= Self::BITS - 1 || $From $(::<$N>)?::BITS - from.leading_zeros() <= Self::BITS - 1 { // Self::BITS - 1 as otherwise return value would be negative |
139 | 0 | Ok(Self::cast_from(from)) |
140 | | } else { |
141 | 0 | Err(TryFromIntError(())) |
142 | | } |
143 | 0 | } Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::BTryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::BTryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::BTryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::BTryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::BTryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::BTryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::BTryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::BTryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::BTryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::BTryFrom<bnum::buint::BUintD32<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::BTryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::BTryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::BTryFrom<bnum::buint::BUintD16<_>>>::try_from Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::BTryFrom<bnum::buint::BUintD8<_>>>::try_from Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::BTryFrom<bnum::buint::BUint<_>>>::try_from Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::BTryFrom<bnum::buint::BUintD32<_>>>::try_from |
144 | | } |
145 | | )* |
146 | | }; |
147 | | } |
148 | | |
149 | | macro_rules! int_try_from_int { |
150 | | ($Trait: ident; $To: ident; $($From: ident $(<$N: ident>)?), *) => { |
151 | | $( |
152 | | impl<$(const $N: usize,)? const M: usize> $Trait<$From $(<$N>)?> for $To<M> { |
153 | | type Error = TryFromIntError; |
154 | | |
155 | 0 | fn try_from(from: $From $(<$N>)?) -> Result<Self, Self::Error> { |
156 | 0 | if $From $(::<$N>)?::BITS <= Self::BITS { |
157 | 0 | return Ok(Self::cast_from(from)); |
158 | 0 | } |
159 | 0 | if from.is_negative() { |
160 | 0 | if $From $(::<$N>)?::BITS - from.leading_ones() <= Self::BITS - 1 { |
161 | 0 | Ok(Self::cast_from(from)) |
162 | | } else { |
163 | 0 | Err(TryFromIntError(())) |
164 | | } |
165 | | } else { |
166 | 0 | if $From $(::<$N>)?::BITS - from.leading_zeros() <= Self::BITS - 1 { |
167 | 0 | Ok(Self::cast_from(from)) |
168 | | } else { |
169 | 0 | Err(TryFromIntError(())) |
170 | | } |
171 | | } |
172 | 0 | } Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::BTryFrom<bnum::bint::BInt<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::BTryFrom<bnum::bint::BIntD32<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::BTryFrom<bnum::bint::BIntD16<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::BTryFrom<bnum::bint::BIntD8<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::BTryFrom<bnum::bint::BInt<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::BTryFrom<bnum::bint::BIntD32<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::BTryFrom<bnum::bint::BIntD16<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::BTryFrom<bnum::bint::BIntD8<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::BTryFrom<bnum::bint::BInt<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::BTryFrom<bnum::bint::BIntD32<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::BTryFrom<bnum::bint::BIntD16<_>>>::try_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::BTryFrom<bnum::bint::BIntD8<_>>>::try_from Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::BTryFrom<bnum::bint::BInt<_>>>::try_from Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::BTryFrom<bnum::bint::BIntD32<_>>>::try_from Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::BTryFrom<bnum::bint::BIntD16<_>>>::try_from Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::BTryFrom<bnum::bint::BIntD8<_>>>::try_from |
173 | | } |
174 | | )* |
175 | | }; |
176 | | } |
177 | | |
178 | | use crate::BTryFrom; |
179 | | |
180 | | macro_rules! mixed_try_from { |
181 | | ($BUint: ident, $BInt: ident) => { |
182 | | uint_try_from_uint!(BTryFrom; $BUint; BUint<N>, BUintD32<N>, BUintD16<N>, BUintD8<N>/*, u8, u16, u32, u64, u128, usize*/); |
183 | | uint_try_from_int!(BTryFrom; $BUint; BInt<N>, BIntD32<N>, BIntD16<N>, BIntD8<N>/*, i8, i16, i32, i64, i128, isize*/); |
184 | | int_try_from_uint!(BTryFrom; $BInt; BUint<N>, BUintD32<N>, BUintD16<N>, BUintD8<N>/*, u8, u16, u32, u64, u128, usize*/); |
185 | | int_try_from_int!(BTryFrom; $BInt; BInt<N>, BIntD32<N>, BIntD16<N>, BIntD8<N>/*, i8, i16, i32, i64, i128, isize*/); |
186 | | }; |
187 | | } |
188 | | |
189 | | use crate::cast::CastFrom; |
190 | | use crate::errors::TryFromIntError; |
191 | | |
192 | | macro_rules! convert { |
193 | | ($BUint: ident, $BInt: ident, $Digit: ident) => { |
194 | | impl<const N: usize> From<bool> for $BUint<N> { |
195 | | #[inline] |
196 | 0 | fn from(small: bool) -> Self { |
197 | 0 | Self::cast_from(small) |
198 | 0 | } Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::From<bool>>::from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::From<bool>>::from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::From<bool>>::from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::From<bool>>::from |
199 | | } |
200 | | |
201 | | impl<const N: usize> From<char> for $BUint<N> { |
202 | | #[inline] |
203 | 0 | fn from(c: char) -> Self { |
204 | 0 | Self::cast_from(c) |
205 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::From<char>>::from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::From<char>>::from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::From<char>>::from Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::From<char>>::from |
206 | | } |
207 | | |
208 | | from_uint!($BUint, $Digit; u8, u16, u32, u64, u128, usize); |
209 | | |
210 | | try_from_iint!($BUint; i8 -> u8, i16 -> u16, i32 -> u32, isize -> usize, i64 -> u64, i128 -> u128); |
211 | | |
212 | | try_from_buint!($BUint, $Digit; u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); |
213 | | |
214 | | mixed_try_from!($BUint, $BInt); |
215 | | |
216 | | impl<const N: usize> From<[$Digit; N]> for $BUint<N> { |
217 | | #[inline] |
218 | 0 | fn from(digits: [$Digit; N]) -> Self { |
219 | 0 | Self::from_digits(digits) |
220 | 0 | } Unexecuted instantiation: <bnum::buint::BUint<_> as core::convert::From<[u64; _]>>::from Unexecuted instantiation: <bnum::buint::BUintD16<_> as core::convert::From<[u16; _]>>::from Unexecuted instantiation: <bnum::buint::BUintD8<_> as core::convert::From<[u8; _]>>::from Unexecuted instantiation: <bnum::buint::BUintD32<_> as core::convert::From<[u32; _]>>::from |
221 | | } |
222 | | |
223 | | impl<const N: usize> From<$BUint<N>> for [$Digit; N] { |
224 | | #[inline] |
225 | 0 | fn from(uint: $BUint<N>) -> Self { |
226 | 0 | uint.digits |
227 | 0 | } Unexecuted instantiation: <[u64; _] as core::convert::From<bnum::buint::BUint<_>>>::from Unexecuted instantiation: <[u16; _] as core::convert::From<bnum::buint::BUintD16<_>>>::from Unexecuted instantiation: <[u8; _] as core::convert::From<bnum::buint::BUintD8<_>>>::from Unexecuted instantiation: <[u32; _] as core::convert::From<bnum::buint::BUintD32<_>>>::from |
228 | | } |
229 | | }; |
230 | | } |
231 | | |
232 | | #[cfg(test)] |
233 | | crate::test::all_digit_tests! { |
234 | | use crate::test::{self, types::utest}; |
235 | | use crate::test::cast_types::*; |
236 | | use super::BTryFrom; |
237 | | |
238 | | test::test_btryfrom!(utest; TestUint1, TestUint2, TestUint3, TestUint4, TestUint5, TestUint6, TestUint7, TestUint8, TestUint9, TestUint10, TestInt1, TestInt2, TestInt3, TestInt4, TestInt5, TestInt6, TestInt7, TestInt8, TestInt9, TestInt10/*, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize*/); |
239 | | |
240 | | #[cfg(not(any(test_int_bits = "16", test_int_bits = "32")))] // TODO: due to incorrectly implementing From instead of TryFrom for small bnum ints |
241 | | test::test_from! { |
242 | | function: <utest as TryFrom>::try_from, |
243 | | from_types: (u8, u16, u32, u64, bool, char, i8, i16, i32, i64, isize, usize) // TODO: when we can use TryFrom for conversions between bnum ints, we can just add the list of test types here, same as in the casting tests |
244 | | } |
245 | | |
246 | | test::test_into! { |
247 | | function: <utest as TryInto>::try_into, |
248 | | into_types: (u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize) |
249 | | } |
250 | | } |
251 | | |
252 | | crate::macro_impl!(convert); |