Skip to content

Commit 3e3a704

Browse files
authored
From<u8, u16> for u32 (#5816)
## Description Adds implementation for From<u8> and From<u16> for u32 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 7e209ea commit 3e3a704

File tree

1 file changed

+74
-1
lines changed
  • sway-lib-std/src/primitive_conversions

1 file changed

+74
-1
lines changed

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

+74-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
library;
22

3-
use ::convert::TryFrom;
3+
use ::convert::{From, TryFrom};
44
use ::option::Option::{self, *};
55

66
impl u32 {
@@ -25,6 +25,50 @@ impl u32 {
2525
}
2626
}
2727

28+
impl From<u8> for u32 {
29+
/// Casts a `u8` to a `u32`.
30+
///
31+
/// # Returns
32+
///
33+
/// * [u32] - The `u32` representation of the `u8` value.
34+
///
35+
/// # Examples
36+
///
37+
/// ```sway
38+
///
39+
/// fn foo() {
40+
/// let u32_value = u32::from(0u8);
41+
/// }
42+
/// ```
43+
fn from(u: u8) -> Self {
44+
asm(r1: u) {
45+
r1: u32
46+
}
47+
}
48+
}
49+
50+
impl From<u16> for u32 {
51+
/// Casts a `u16` to a `u32`.
52+
///
53+
/// # Returns
54+
///
55+
/// * [u32] - The `u32` representation of the `u16` value.
56+
///
57+
/// # Examples
58+
///
59+
/// ```sway
60+
///
61+
/// fn foo() {
62+
/// let u32_value = u32::from(0u16);
63+
/// }
64+
/// ```
65+
fn from(u: u16) -> Self {
66+
asm(r1: u) {
67+
r1: u32
68+
}
69+
}
70+
}
71+
2872
impl TryFrom<u64> for u32 {
2973
fn try_from(u: u64) -> Option<Self> {
3074
if u > u32::max().as_u64() {
@@ -57,6 +101,35 @@ impl TryFrom<u256> for u32 {
57101
}
58102
}
59103

104+
// TODO: Replace <u32 as From<T>> with u32::from when https://github.com/FuelLabs/sway/issues/5798 is resolved.
105+
#[test]
106+
fn test_u32_from_u8() {
107+
use ::assert::assert;
108+
109+
let u8_1: u8 = 0u8;
110+
let u8_2: u8 = 255u8;
111+
112+
let u32_1 = <u32 as From<u8>>::from(u8_1);
113+
let u32_2 = <u32 as From<u8>>::from(u8_2);
114+
115+
assert(u32_1 == 0u32);
116+
assert(u32_2 == 255u32);
117+
}
118+
119+
#[test]
120+
fn test_u32_from_u16() {
121+
use ::assert::assert;
122+
123+
let u16_1: u16 = 0u16;
124+
let u16_2: u16 = 65535u16;
125+
126+
let u32_1 = <u32 as From<u16>>::from(u16_1);
127+
let u32_2 = <u32 as From<u16>>::from(u16_2);
128+
129+
assert(u32_1 == 0u32);
130+
assert(u32_2 == 65535u32);
131+
}
132+
60133
#[test]
61134
fn test_u32_try_from_u64() {
62135
use ::assert::assert;

0 commit comments

Comments
 (0)