Skip to content

Commit 35df2a7

Browse files
authored
From<u8, u16, u32> for u64 implementations (#5817)
## Description Adds implementations for From<u8>, From<u16>, From<u32> for u64 Partially addresses #5797 ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
1 parent c1ea517 commit 35df2a7

File tree

1 file changed

+109
-0
lines changed
  • sway-lib-std/src/primitive_conversions

1 file changed

+109
-0
lines changed

sway-lib-std/src/primitive_conversions/u64.sw

+109
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,72 @@ impl u64 {
3535
}
3636
}
3737

38+
impl From<u8> for u64 {
39+
/// Casts a `u8` to a `u64`.
40+
///
41+
/// # Returns
42+
///
43+
/// * [u64] - The `u64` representation of the `u8` value.
44+
///
45+
/// # Examples
46+
///
47+
/// ```sway
48+
///
49+
/// fn foo() {
50+
/// let u64_value = u64::from(0u8);
51+
/// }
52+
/// ```
53+
fn from(u: u8) -> Self {
54+
asm(r1: u) {
55+
r1: u64
56+
}
57+
}
58+
}
59+
60+
impl From<u16> for u64 {
61+
/// Casts a `u16` to a `u64`.
62+
///
63+
/// # Returns
64+
///
65+
/// * [u64] - The `u64` representation of the `u16` value.
66+
///
67+
/// # Examples
68+
///
69+
/// ```sway
70+
///
71+
/// fn foo() {
72+
/// let u64_value = u64::from(0u16);
73+
/// }
74+
/// ```
75+
fn from(u: u16) -> Self {
76+
asm(r1: u) {
77+
r1: u64
78+
}
79+
}
80+
}
81+
82+
impl From<u32> for u64 {
83+
/// Casts a `u32` to a `u64`.
84+
///
85+
/// # Returns
86+
///
87+
/// * [u64] - The `u64` representation of the `u32` value.
88+
///
89+
/// # Examples
90+
///
91+
/// ```sway
92+
///
93+
/// fn foo() {
94+
/// let u64_value = u64::from(0u32);
95+
/// }
96+
/// ```
97+
fn from(u: u32) -> Self {
98+
asm(r1: u) {
99+
r1: u64
100+
}
101+
}
102+
}
103+
38104
impl TryFrom<u256> for u64 {
39105
fn try_from(u: u256) -> Option<Self> {
40106
let parts = asm(r1: u) {
@@ -49,6 +115,49 @@ impl TryFrom<u256> for u64 {
49115
}
50116
}
51117

118+
// TODO: Replace <u64 as From<T>> with u64::from when https://github.com/FuelLabs/sway/issues/5798 is resolved.
119+
#[test]
120+
fn test_u64_from_u8() {
121+
use ::assert::assert;
122+
123+
let u8_1: u8 = 0u8;
124+
let u8_2: u8 = 255u8;
125+
126+
let u64_1 = <u64 as From<u8>>::from(u8_1);
127+
let u64_2 = <u64 as From<u8>>::from(u8_2);
128+
129+
assert(u64_1 == 0u64);
130+
assert(u64_2 == 255u64);
131+
}
132+
133+
#[test]
134+
fn test_u64_from_u16() {
135+
use ::assert::assert;
136+
137+
let u16_1: u16 = 0u16;
138+
let u16_2: u16 = 65535u16;
139+
140+
let u64_1 = <u64 as From<u16>>::from(u16_1);
141+
let u64_2 = <u64 as From<u16>>::from(u16_2);
142+
143+
assert(u64_1 == 0u64);
144+
assert(u64_2 == 65535u64);
145+
}
146+
147+
#[test]
148+
fn test_u64_from_u32() {
149+
use ::assert::assert;
150+
151+
let u32_1: u32 = 0u32;
152+
let u32_2: u32 = 4294967295u32;
153+
154+
let u64_1 = <u64 as From<u32>>::from(u32_1);
155+
let u64_2 = <u64 as From<u32>>::from(u32_2);
156+
157+
assert(u64_1 == 0u64);
158+
assert(u64_2 == 4294967295u64);
159+
}
160+
52161
#[test]
53162
fn test_u64_try_from_u256() {
54163
use ::assert::assert;

0 commit comments

Comments
 (0)