From 7d288306072b9a091b46876044707999d1327b22 Mon Sep 17 00:00:00 2001 From: SwayStar123 <46050679+SwayStar123@users.noreply.github.com> Date: Wed, 24 Apr 2024 17:24:15 +0530 Subject: [PATCH] Allow conversions between native u types and U128 (#5825) ## Description Adds impl From for U128 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: Cameron Carstens Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com> --- sway-lib-std/src/u128.sw | 147 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 1 deletion(-) diff --git a/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index 47deb45dbd8..d5c1cca2fc3 100644 --- a/sway-lib-std/src/u128.sw +++ b/sway-lib-std/src/u128.sw @@ -2,7 +2,8 @@ library; use ::assert::assert; -use ::convert::From; +use ::convert::{From, Into}; +use ::primitive_conversions::u64::*; use ::flags::{disable_panic_on_overflow, set_flags}; use ::math::*; use ::result::Result::{self, *}; @@ -19,6 +20,102 @@ pub struct U128 { lower: u64, } +impl From for U128 { + /// Converts a `u8` to a `U128`. + /// + /// # Returns + /// + /// * [U128] - The `U128` representation of the `u8` value. + /// + /// # Examples + /// + /// ```sway + /// use std::u128::U128; + /// + /// fn foo() { + /// let u128_value = U128::from(0u8); + /// } + /// ``` + fn from(val: u8) -> Self { + Self { + upper: 0, + lower: val.into(), + } + } +} + +impl From for U128 { + /// Converts a `u16` to a `U128`. + /// + /// # Returns + /// + /// * [U128] - The `U128` representation of the `u16` value. + /// + /// # Examples + /// + /// ```sway + /// use std::u128::U128; + /// + /// fn foo() { + /// let u128_value = U128::from(0u16); + /// } + /// ``` + fn from(val: u16) -> Self { + Self { + upper: 0, + lower: val.into(), + } + } +} + +impl From for U128 { + /// Converts a `u32` to a `U128`. + /// + /// # Returns + /// + /// * [U128] - The `U128` representation of the `u32` value. + /// + /// # Examples + /// + /// ```sway + /// use std::u128::U128; + /// + /// fn foo() { + /// let u128_value = U128::from(0u32); + /// } + /// ``` + fn from(val: u32) -> Self { + Self { + upper: 0, + lower: val.into(), + } + } +} + +impl From for U128 { + /// Converts a `u64` to a `U128`. + /// + /// # Returns + /// + /// * [U128] - The `U128` representation of the `u64` value. + /// + /// # Examples + /// + /// ```sway + /// use std::u128::U128; + /// + /// fn foo() { + /// let u128_value = U128::from(0u64); + /// } + /// ``` + fn from(val: u64) -> Self { + Self { + upper: 0, + lower: val, + } + } +} + /// The error type used for `U128` type errors. pub enum U128Error { /// This error occurs when a `U128` is attempted to be downcast to a `u64` and the conversion would result in a loss of precision. @@ -593,3 +690,51 @@ impl Logarithm for U128 { self_log2 / base_log2 } } + +#[test] +fn test_u128_from_u8() { + let u8_1: u8 = 0u8; + let u8_2: u8 = 255u8; + + let u128_1 = >::from(u8_1); + let u128_2 = >::from(u8_2); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 255u64); +} + +#[test] +fn test_u128_from_u16() { + let u16_1: u16 = 0u16; + let u16_2: u16 = 65535u16; + + let u128_1 = >::from(u16_1); + let u128_2 = >::from(u16_2); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 65535u64); +} + +#[test] +fn test_u128_from_u32() { + let u32_1: u32 = 0u32; + let u32_2: u32 = 4294967295u32; + + let u128_1 = >::from(u32_1); + let u128_2 = >::from(u32_2); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 4294967295u64); +} + +#[test] +fn test_u128_from_u64() { + let u64_1: u64 = 0u64; + let u64_2: u64 = 18446744073709551615u64; + + let u128_1 = >::from(u64_1); + let u128_2 = >::from(u64_2); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 18446744073709551615u64); +}