Skip to content

Commit 343bc34

Browse files
SwayStar123IGI-111
andauthored
From<u8> for u16 implementation (#5815)
## Description Adds From<u8> for u16 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. --------- Co-authored-by: IGI-111 <igi-111@protonmail.com>
1 parent 73fd7ec commit 343bc34

File tree

1 file changed

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

1 file changed

+37
-1
lines changed

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

+37-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 u16 {
@@ -15,6 +15,28 @@ impl u16 {
1515
}
1616
}
1717

18+
impl From<u8> for u16 {
19+
/// Casts a `u8` to a `u16`.
20+
///
21+
/// # Returns
22+
///
23+
/// * [u16] - The `u16` representation of the `u8` value.
24+
///
25+
/// # Examples
26+
///
27+
/// ```sway
28+
///
29+
/// fn foo() {
30+
/// let u16_value = u16::from(0u8);
31+
/// }
32+
/// ```
33+
fn from(u: u8) -> Self {
34+
asm(r1: u) {
35+
r1: u16
36+
}
37+
}
38+
}
39+
1840
impl TryFrom<u32> for u16 {
1941
fn try_from(u: u32) -> Option<Self> {
2042
if u > u16::max().as_u32() {
@@ -59,6 +81,20 @@ impl TryFrom<u256> for u16 {
5981
}
6082
}
6183

84+
#[test]
85+
fn test_u16_from_u8() {
86+
use ::assert::assert;
87+
88+
let u8_1: u8 = 0u8;
89+
let u8_2: u8 = 255u8;
90+
91+
let u16_1 = u16::from(u8_1);
92+
let u16_2 = u16::from(u8_2);
93+
94+
assert(u16_1 == 0u16);
95+
assert(u16_2 == 255u16);
96+
}
97+
6298
#[test]
6399
fn test_u16_try_from_u32() {
64100
use ::assert::assert;

0 commit comments

Comments
 (0)