From 8642ba35a24c8bf3740aaea07027cca6eaaec8ba Mon Sep 17 00:00:00 2001 From: SwayStar123 <46050679+SwayStar123@users.noreply.github.com> Date: Wed, 1 May 2024 12:07:11 +0530 Subject: [PATCH] Implement TryFrom< U128 > for all primitive u types (#5888) ## Description Implements TryFrom< U128 > for 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. --- sway-lib-std/src/primitive_conversions/u16.sw | 27 +++++++++++++++++++ sway-lib-std/src/primitive_conversions/u32.sw | 27 +++++++++++++++++++ sway-lib-std/src/primitive_conversions/u64.sw | 27 +++++++++++++++++++ sway-lib-std/src/primitive_conversions/u8.sw | 27 +++++++++++++++++++ 4 files changed, 108 insertions(+) diff --git a/sway-lib-std/src/primitive_conversions/u16.sw b/sway-lib-std/src/primitive_conversions/u16.sw index bdc6022dde5..9ea79166ca1 100644 --- a/sway-lib-std/src/primitive_conversions/u16.sw +++ b/sway-lib-std/src/primitive_conversions/u16.sw @@ -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 { @@ -81,6 +82,16 @@ impl TryFrom for u16 { } } +impl TryFrom for u16 { + fn try_from(u: U128) -> Option { + if u.upper() == 0 { + >::try_from(u.lower()) + } else { + None + } + } +} + #[test] fn test_u16_from_u8() { use ::assert::assert; @@ -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 = >::try_from(u128_1); + let u16_2 = >::try_from(u128_2); + + assert(u16_1.is_some()); + assert(u16_1.unwrap() == 0u16); + + assert(u16_2.is_none()); +} diff --git a/sway-lib-std/src/primitive_conversions/u32.sw b/sway-lib-std/src/primitive_conversions/u32.sw index 99f88ec7671..5d34a275b7b 100644 --- a/sway-lib-std/src/primitive_conversions/u32.sw +++ b/sway-lib-std/src/primitive_conversions/u32.sw @@ -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 { @@ -101,6 +102,16 @@ impl TryFrom for u32 { } } +impl TryFrom for u32 { + fn try_from(u: U128) -> Option { + if u.upper() == 0 { + >::try_from(u.lower()) + } else { + None + } + } +} + // TODO: Replace > with u32::from when https://github.com/FuelLabs/sway/issues/5798 is resolved. #[test] fn test_u32_from_u8() { @@ -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 = >::try_from(u128_1); + let u32_2 = >::try_from(u128_2); + + assert(u32_1.is_some()); + assert(u32_1.unwrap() == 0u32); + + assert(u32_2.is_none()); +} diff --git a/sway-lib-std/src/primitive_conversions/u64.sw b/sway-lib-std/src/primitive_conversions/u64.sw index fd4e98b4a86..246066f4091 100644 --- a/sway-lib-std/src/primitive_conversions/u64.sw +++ b/sway-lib-std/src/primitive_conversions/u64.sw @@ -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 { @@ -115,6 +116,16 @@ impl TryFrom for u64 { } } +impl TryFrom for u64 { + fn try_from(u: U128) -> Option { + if u.upper() == 0 { + Some(u.lower()) + } else { + None + } + } +} + // TODO: Replace > with u64::from when https://github.com/FuelLabs/sway/issues/5798 is resolved. #[test] fn test_u64_from_u8() { @@ -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 = >::try_from(u128_1); + let u64_2 = >::try_from(u128_2); + + assert(u64_1.is_some()); + assert(u64_1.unwrap() == 0u64); + + assert(u64_2.is_none()); +} diff --git a/sway-lib-std/src/primitive_conversions/u8.sw b/sway-lib-std/src/primitive_conversions/u8.sw index 14c1eaeb560..e638196a817 100644 --- a/sway-lib-std/src/primitive_conversions/u8.sw +++ b/sway-lib-std/src/primitive_conversions/u8.sw @@ -2,6 +2,7 @@ library; use ::convert::TryFrom; use ::option::Option::{self, *}; +use ::u128::U128; impl TryFrom for u8 { fn try_from(u: u16) -> Option { @@ -59,6 +60,16 @@ impl TryFrom for u8 { } } +impl TryFrom for u8 { + fn try_from(u: U128) -> Option { + if u.upper() == 0 { + >::try_from(u.lower()) + } else { + None + } + } +} + #[test] fn test_u8_try_from_u16() { use ::assert::assert; @@ -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 = >::try_from(u128_1); + let u8_2 = >::try_from(u128_2); + + assert(u8_1.is_some()); + assert(u8_1.unwrap() == 0u8); + + assert(u8_2.is_none()); +}