Skip to content

Commit

Permalink
From<u8, u16> for u32 (#5816)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
SwayStar123 authored Apr 3, 2024
1 parent 7e209ea commit 3e3a704
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion sway-lib-std/src/primitive_conversions/u32.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use ::convert::TryFrom;
use ::convert::{From, TryFrom};
use ::option::Option::{self, *};

impl u32 {
Expand All @@ -25,6 +25,50 @@ impl u32 {
}
}

impl From<u8> for u32 {
/// Casts a `u8` to a `u32`.
///
/// # Returns
///
/// * [u32] - The `u32` representation of the `u8` value.
///
/// # Examples
///
/// ```sway
///
/// fn foo() {
/// let u32_value = u32::from(0u8);
/// }
/// ```
fn from(u: u8) -> Self {
asm(r1: u) {
r1: u32
}
}
}

impl From<u16> for u32 {
/// Casts a `u16` to a `u32`.
///
/// # Returns
///
/// * [u32] - The `u32` representation of the `u16` value.
///
/// # Examples
///
/// ```sway
///
/// fn foo() {
/// let u32_value = u32::from(0u16);
/// }
/// ```
fn from(u: u16) -> Self {
asm(r1: u) {
r1: u32
}
}
}

impl TryFrom<u64> for u32 {
fn try_from(u: u64) -> Option<Self> {
if u > u32::max().as_u64() {
Expand Down Expand Up @@ -57,6 +101,35 @@ impl TryFrom<u256> for u32 {
}
}

// TODO: Replace <u32 as From<T>> with u32::from when https://github.com/FuelLabs/sway/issues/5798 is resolved.
#[test]
fn test_u32_from_u8() {
use ::assert::assert;

let u8_1: u8 = 0u8;
let u8_2: u8 = 255u8;

let u32_1 = <u32 as From<u8>>::from(u8_1);
let u32_2 = <u32 as From<u8>>::from(u8_2);

assert(u32_1 == 0u32);
assert(u32_2 == 255u32);
}

#[test]
fn test_u32_from_u16() {
use ::assert::assert;

let u16_1: u16 = 0u16;
let u16_2: u16 = 65535u16;

let u32_1 = <u32 as From<u16>>::from(u16_1);
let u32_2 = <u32 as From<u16>>::from(u16_2);

assert(u32_1 == 0u32);
assert(u32_2 == 65535u32);
}

#[test]
fn test_u32_try_from_u64() {
use ::assert::assert;
Expand Down

0 comments on commit 3e3a704

Please sign in to comment.