| 1 | /// Creates an `Int` from a numeric literal. |
| 2 | #[macro_export] |
| 3 | macro_rules! int { |
| 4 | ($n:literal) => { |
| 5 | <$crate::Int as ::core::convert::From<i32>>::from($n) |
| 6 | }; |
| 7 | } |
| 8 | |
| 9 | /// Creates a `UInt` from a numeric literal. |
| 10 | #[macro_export] |
| 11 | macro_rules! uint { |
| 12 | ($n:literal) => { |
| 13 | <$crate::UInt as ::core::convert::From<u32>>::from($n) |
| 14 | }; |
| 15 | } |
| 16 | |
| 17 | macro_rules! fmt_impls { |
| 18 | ($type:ident) => { |
| 19 | impl ::core::fmt::Display for $type { |
| 20 | fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { |
| 21 | write!(f, "{}", self.0) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl ::core::fmt::Debug for $type { |
| 26 | fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { |
| 27 | write!(f, "{:?}", self.0) |
| 28 | } |
| 29 | } |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | macro_rules! convert_impls { |
| 34 | ( |
| 35 | $type:ident, |
| 36 | $t8:ident, |
| 37 | $t16:ident, |
| 38 | $t32:ident, |
| 39 | $t64:ident, |
| 40 | $t128:ident, |
| 41 | $tsize:ident, |
| 42 | $ot8:ident, |
| 43 | $ot16:ident, |
| 44 | $ot32:ident, |
| 45 | $otsize:ident |
| 46 | ) => { |
| 47 | impl ::core::convert::From<$t8> for $type { |
| 48 | fn from(val: $t8) -> Self { |
| 49 | Self($t64::from(val)) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl ::core::convert::From<$t16> for $type { |
| 54 | fn from(val: $t16) -> Self { |
| 55 | Self($t64::from(val)) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl ::core::convert::From<$t32> for $type { |
| 60 | fn from(val: $t32) -> Self { |
| 61 | Self($t64::from(val)) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl ::core::convert::TryFrom<$t64> for $type { |
| 66 | type Error = crate::error::TryFromIntError; |
| 67 | |
| 68 | fn try_from(val: $t64) -> Result<Self, crate::error::TryFromIntError> { |
| 69 | Self::new(val).ok_or_else(crate::error::TryFromIntError::new) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | impl ::core::convert::TryFrom<$t128> for $type { |
| 74 | type Error = crate::error::TryFromIntError; |
| 75 | |
| 76 | fn try_from(val: $t128) -> Result<Self, crate::error::TryFromIntError> { |
| 77 | $t64::try_from(val) |
| 78 | .map_err(|_| crate::error::TryFromIntError::new()) |
| 79 | .and_then($type::try_from) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl ::core::convert::TryFrom<$tsize> for $type { |
| 84 | type Error = crate::error::TryFromIntError; |
| 85 | |
| 86 | fn try_from(val: $tsize) -> Result<Self, crate::error::TryFromIntError> { |
| 87 | $t64::try_from(val) |
| 88 | .map_err(|_| crate::error::TryFromIntError::new()) |
| 89 | .and_then($type::try_from) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | impl ::core::convert::TryFrom<$otsize> for $type { |
| 94 | type Error = crate::error::TryFromIntError; |
| 95 | |
| 96 | fn try_from(val: $otsize) -> Result<Self, crate::error::TryFromIntError> { |
| 97 | $t64::try_from(val) |
| 98 | .map_err(|_| crate::error::TryFromIntError::new()) |
| 99 | .and_then($type::try_from) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl ::core::convert::TryFrom<$type> for $t8 { |
| 104 | type Error = ::core::num::TryFromIntError; |
| 105 | |
| 106 | fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> { |
| 107 | Self::try_from(val.0) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | impl ::core::convert::TryFrom<$type> for $ot8 { |
| 112 | type Error = ::core::num::TryFromIntError; |
| 113 | |
| 114 | fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> { |
| 115 | Self::try_from(val.0) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl ::core::convert::TryFrom<$type> for $t16 { |
| 120 | type Error = ::core::num::TryFromIntError; |
| 121 | |
| 122 | fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> { |
| 123 | Self::try_from(val.0) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl ::core::convert::TryFrom<$type> for $ot16 { |
| 128 | type Error = ::core::num::TryFromIntError; |
| 129 | |
| 130 | fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> { |
| 131 | Self::try_from(val.0) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | impl ::core::convert::TryFrom<$type> for $t32 { |
| 136 | type Error = ::core::num::TryFromIntError; |
| 137 | |
| 138 | fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> { |
| 139 | Self::try_from(val.0) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | impl ::core::convert::TryFrom<$type> for $ot32 { |
| 144 | type Error = ::core::num::TryFromIntError; |
| 145 | |
| 146 | fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> { |
| 147 | Self::try_from(val.0) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | impl ::core::convert::From<$type> for $t64 { |
| 152 | fn from(val: $type) -> Self { |
| 153 | val.0 |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | impl ::core::convert::From<$type> for $t128 { |
| 158 | fn from(val: $type) -> Self { |
| 159 | $t128::from(val.0) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | impl ::core::convert::TryFrom<$type> for $tsize { |
| 164 | type Error = ::core::num::TryFromIntError; |
| 165 | |
| 166 | fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> { |
| 167 | Self::try_from(val.0) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | impl ::core::convert::TryFrom<$type> for $otsize { |
| 172 | type Error = ::core::num::TryFromIntError; |
| 173 | |
| 174 | fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> { |
| 175 | Self::try_from(val.0) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | impl ::core::convert::From<$type> for f64 { |
| 180 | fn from(val: $type) -> Self { |
| 181 | val.0 as f64 |
| 182 | } |
| 183 | } |
| 184 | }; |
| 185 | } |