|
| 1 | +library; |
| 2 | + |
| 3 | +use ::convert::From; |
| 4 | + |
| 5 | +/// Functions for casting between `u256` and other types. |
| 6 | +impl From<u8> for u256 { |
| 7 | + /// Casts a `u8` to a `u256`. |
| 8 | + /// |
| 9 | + /// # Arguments |
| 10 | + /// |
| 11 | + /// * `num`: [u8] - The `u8` to be casted. |
| 12 | + /// |
| 13 | + /// # Returns |
| 14 | + /// |
| 15 | + /// * [u256] - The `u256` representation of the `u8` value. |
| 16 | + /// |
| 17 | + /// # Examples |
| 18 | + /// |
| 19 | + /// ```sway |
| 20 | + /// |
| 21 | + /// fn foo() { |
| 22 | + /// let u256_value = u256::from(255_u8); |
| 23 | + /// } |
| 24 | + /// ``` |
| 25 | + fn from(num: u8) -> Self { |
| 26 | + num.as_u256() |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl From<u16> for u256 { |
| 31 | + /// Casts a `u16` to a `u256`. |
| 32 | + /// |
| 33 | + /// # Arguments |
| 34 | + /// |
| 35 | + /// * `num`: [u16] - The `u16` to be casted. |
| 36 | + /// |
| 37 | + /// # Returns |
| 38 | + /// |
| 39 | + /// * [u256] - The `u256` representation of the `u16` value. |
| 40 | + /// |
| 41 | + /// # Examples |
| 42 | + /// |
| 43 | + /// ```sway |
| 44 | + /// |
| 45 | + /// fn foo() { |
| 46 | + /// let u256_value = u256::from(65535_u16); |
| 47 | + /// } |
| 48 | + /// ``` |
| 49 | + fn from(num: u16) -> Self { |
| 50 | + num.as_u256() |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl From<u32> for u256 { |
| 55 | + /// Casts a `u32` to a `u256`. |
| 56 | + /// |
| 57 | + /// # Arguments |
| 58 | + /// |
| 59 | + /// * `num`: [u32] - The `u32` to be casted. |
| 60 | + /// |
| 61 | + /// # Returns |
| 62 | + /// |
| 63 | + /// * [u256] - The `u256` representation of the `u32` value. |
| 64 | + /// |
| 65 | + /// # Examples |
| 66 | + /// |
| 67 | + /// ```sway |
| 68 | + /// |
| 69 | + /// fn foo() { |
| 70 | + /// let u256_value = u256::from(4294967295_u32); |
| 71 | + /// } |
| 72 | + /// ``` |
| 73 | + fn from(num: u32) -> Self { |
| 74 | + num.as_u256() |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl From<u64> for u256 { |
| 79 | + /// Casts a `u64` to a `u256`. |
| 80 | + /// |
| 81 | + /// # Arguments |
| 82 | + /// |
| 83 | + /// * `num`: [u64] - The `u64` to be casted. |
| 84 | + /// |
| 85 | + /// # Returns |
| 86 | + /// |
| 87 | + /// * [u256] - The `u256` representation of the `u64` value. |
| 88 | + /// |
| 89 | + /// # Examples |
| 90 | + /// |
| 91 | + /// ```sway |
| 92 | + /// |
| 93 | + /// fn foo() { |
| 94 | + /// let u256_value = u256::from(18446744073709551615_u64); |
| 95 | + /// } |
| 96 | + /// ``` |
| 97 | + fn from(num: u64) -> Self { |
| 98 | + num.as_u256() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl From<b256> for u256 { |
| 103 | + /// Casts raw `b256` data to a `u256`. |
| 104 | + /// |
| 105 | + /// # Arguments |
| 106 | + /// |
| 107 | + /// * `bits`: [b256] - The raw `b256` data to be casted. |
| 108 | + /// |
| 109 | + /// # Returns |
| 110 | + /// |
| 111 | + /// * [u256] - The `u256` representation of the raw `b256`. |
| 112 | + /// |
| 113 | + /// # Examples |
| 114 | + /// |
| 115 | + /// ```sway |
| 116 | + /// use std::constants::ZERO_B256; |
| 117 | + /// |
| 118 | + /// fn foo() { |
| 119 | + /// let u256_value = u256::from(ZERO_B256); |
| 120 | + /// } |
| 121 | + /// ``` |
| 122 | + fn from(bits: b256) -> Self { |
| 123 | + bits.as_u256() |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// TODO: Replace <u256 as From<T>> with u256::from when https://github.com/FuelLabs/sway/issues/5798 is resolved. |
| 128 | +#[test] |
| 129 | +fn test_u256_from_u8() { |
| 130 | + use ::assert::assert; |
| 131 | + |
| 132 | + let u256_value = <u256 as From<u8>>::from(255_u8); |
| 133 | + assert( |
| 134 | + u256_value == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256, |
| 135 | + ); |
| 136 | +} |
| 137 | + |
| 138 | +#[test] |
| 139 | +fn test_u256_from_u16() { |
| 140 | + use ::assert::assert; |
| 141 | + |
| 142 | + let u256_value = <u256 as From<u16>>::from(65535_u16); |
| 143 | + assert( |
| 144 | + u256_value == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256, |
| 145 | + ); |
| 146 | +} |
| 147 | + |
| 148 | +#[test] |
| 149 | +fn test_u256_from_u32() { |
| 150 | + use ::assert::assert; |
| 151 | + |
| 152 | + let u256_value = <u256 as From<u32>>::from(4294967295_u32); |
| 153 | + assert( |
| 154 | + u256_value == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256, |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +#[test] |
| 159 | +fn test_u256_from_u64() { |
| 160 | + use ::assert::assert; |
| 161 | + |
| 162 | + let u256_value = <u256 as From<u64>>::from(18446744073709551615_u64); |
| 163 | + assert( |
| 164 | + u256_value == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256, |
| 165 | + ); |
| 166 | +} |
| 167 | + |
| 168 | +#[test] |
| 169 | +fn test_u256_from_b256() { |
| 170 | + use ::assert::assert; |
| 171 | + |
| 172 | + let u256_value = <u256 as From<b256>>::from(0x0000000000000000000000000000000000000000000000000000000000000000); |
| 173 | + assert( |
| 174 | + u256_value == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, |
| 175 | + ); |
| 176 | + |
| 177 | + let u256_value = <u256 as From<b256>>::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); |
| 178 | + assert( |
| 179 | + u256_value == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256, |
| 180 | + ); |
| 181 | +} |
0 commit comments