Skip to content

Commit

Permalink
Implement TryFrom< U128 > for all primitive u types (#5888)
Browse files Browse the repository at this point in the history
## Description
Implements TryFrom< U128 > for <u64,u32,u16,u8>

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).
- [ ] I have requested a review from the relevant team or maintainers.
  • Loading branch information
SwayStar123 committed May 1, 2024
1 parent 2a98742 commit 8642ba3
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
27 changes: 27 additions & 0 deletions sway-lib-std/src/primitive_conversions/u16.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library;

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

impl u16 {
pub fn try_as_u8(self) -> Option<u8> {
Expand Down Expand Up @@ -81,6 +82,16 @@ impl TryFrom<u256> for u16 {
}
}

impl TryFrom<U128> for u16 {
fn try_from(u: U128) -> Option<Self> {
if u.upper() == 0 {
<u16 as TryFrom<u64>>::try_from(u.lower())
} else {
None
}
}
}

#[test]
fn test_u16_from_u8() {
use ::assert::assert;
Expand Down Expand Up @@ -142,3 +153,19 @@ fn test_u16_try_from_u256() {

assert(u16_2.is_none());
}

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

let u128_1: U128 = U128::new();
let u128_2: U128 = U128::from((0, u16::max().as_u64() + 1));

let u16_1 = <u16 as TryFrom<U128>>::try_from(u128_1);
let u16_2 = <u16 as TryFrom<U128>>::try_from(u128_2);

assert(u16_1.is_some());
assert(u16_1.unwrap() == 0u16);

assert(u16_2.is_none());
}
27 changes: 27 additions & 0 deletions sway-lib-std/src/primitive_conversions/u32.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library;

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

impl u32 {
pub fn try_as_u8(self) -> Option<u8> {
Expand Down Expand Up @@ -101,6 +102,16 @@ impl TryFrom<u256> for u32 {
}
}

impl TryFrom<U128> for u32 {
fn try_from(u: U128) -> Option<Self> {
if u.upper() == 0 {
<u32 as TryFrom<u64>>::try_from(u.lower())
} else {
None
}
}
}

// 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() {
Expand Down Expand Up @@ -161,3 +172,19 @@ fn test_u32_try_from_u256() {

assert(u32_2.is_none());
}

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

let u128_1: U128 = U128::new();
let u128_2: U128 = U128::from((0, u32::max().as_u64() + 1));

let u32_1 = <u32 as TryFrom<U128>>::try_from(u128_1);
let u32_2 = <u32 as TryFrom<U128>>::try_from(u128_2);

assert(u32_1.is_some());
assert(u32_1.unwrap() == 0u32);

assert(u32_2.is_none());
}
27 changes: 27 additions & 0 deletions sway-lib-std/src/primitive_conversions/u64.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library;

use ::convert::{TryFrom, TryInto, *};
use ::option::Option::{self, *};
use ::u128::U128;

impl u64 {
pub fn try_as_u8(self) -> Option<u8> {
Expand Down Expand Up @@ -115,6 +116,16 @@ impl TryFrom<u256> for u64 {
}
}

impl TryFrom<U128> for u64 {
fn try_from(u: U128) -> Option<Self> {
if u.upper() == 0 {
Some(u.lower())
} else {
None
}
}
}

// TODO: Replace <u64 as From<T>> with u64::from when https://github.com/FuelLabs/sway/issues/5798 is resolved.
#[test]
fn test_u64_from_u8() {
Expand Down Expand Up @@ -173,3 +184,19 @@ fn test_u64_try_from_u256() {

assert(u64_2.is_none());
}

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

let u128_1: U128 = U128::new();
let u128_2: U128 = U128::from((1, 0));

let u64_1 = <u64 as TryFrom<U128>>::try_from(u128_1);
let u64_2 = <u64 as TryFrom<U128>>::try_from(u128_2);

assert(u64_1.is_some());
assert(u64_1.unwrap() == 0u64);

assert(u64_2.is_none());
}
27 changes: 27 additions & 0 deletions sway-lib-std/src/primitive_conversions/u8.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library;

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

impl TryFrom<u16> for u8 {
fn try_from(u: u16) -> Option<Self> {
Expand Down Expand Up @@ -59,6 +60,16 @@ impl TryFrom<u256> for u8 {
}
}

impl TryFrom<U128> for u8 {
fn try_from(u: U128) -> Option<Self> {
if u.upper() == 0 {
<u8 as TryFrom<u64>>::try_from(u.lower())
} else {
None
}
}
}

#[test]
fn test_u8_try_from_u16() {
use ::assert::assert;
Expand Down Expand Up @@ -122,3 +133,19 @@ fn test_u8_try_from_u256() {

assert(u8_2.is_none());
}

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

let u128_1: U128 = U128::new();
let u128_2: U128 = U128::from((0, u8::max().as_u64() + 1));

let u8_1 = <u8 as TryFrom<U128>>::try_from(u128_1);
let u8_2 = <u8 as TryFrom<U128>>::try_from(u128_2);

assert(u8_1.is_some());
assert(u8_1.unwrap() == 0u8);

assert(u8_2.is_none());
}

0 comments on commit 8642ba3

Please sign in to comment.