Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TryFrom< U128 > for all primitive u types #5888

Merged
merged 13 commits into from
May 1, 2024
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());
}
Loading