From 64a1552fb74977ff99ff9f555fe1e4879b4d913b Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Wed, 3 Apr 2024 15:19:06 +0530 Subject: [PATCH 1/7] add implementations --- sway-lib-std/src/u128.sw | 140 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index 47deb45dbd8..12de9094981 100644 --- a/sway-lib-std/src/u128.sw +++ b/sway-lib-std/src/u128.sw @@ -19,6 +19,98 @@ pub struct U128 { lower: u64, } +impl From for U128 { + /// Casts a `u8` to a `U128`. + /// + /// # Returns + /// + /// * [U128] - The `U128` representation of the `u8` value. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let u128_value = u128::from(0u8); + /// } + /// ``` + fn from(val: u8) -> Self { + Self { + upper: 0, + lower: val.into(), + } + } +} + +impl From for U128 { + /// Casts a `u16` to a `U128`. + /// + /// # Returns + /// + /// * [U128] - The `U128` representation of the `u16` value. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let u128_value = u128::from(0u16); + /// } + /// ``` + fn from(val: u16) -> Self { + Self { + upper: 0, + lower: val.into(), + } + } +} + +impl From for U128 { + /// Casts a `u32` to a `U128`. + /// + /// # Returns + /// + /// * [U128] - The `U128` representation of the `u32` value. + /// + /// # Examples + /// + /// ```sway + /// + /// fn foo() { + /// let u128_value = u128::from(0u32); + /// } + /// ``` + fn from(val: u32) -> Self { + Self { + upper: 0, + lower: val.into(), + } + } +} + +impl From for U128 { + /// Casts a `u64` to a `U128`. + /// + /// # Returns + /// + /// * [U128] - The `U128` representation of the `u64` value. + /// + /// # Examples + /// + /// ```sway + /// + /// 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 +685,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 = U128::from(u8_1); + let u128_2 = U128::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 = U128::from(u16_1); + let u128_2 = U128::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 = U128::from(u32_1); + let u128_2 = U128::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 = U128::from(u64_1); + let u128_2 = U128::from(u64_2); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 18446744073709551615u64); +} From 88511c04e26673cc6d68d16e41144538cd4988d6 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Wed, 3 Apr 2024 17:37:02 +0530 Subject: [PATCH 2/7] use > --- sway-lib-std/src/u128.sw | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index 12de9094981..ece9ce73063 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, *}; @@ -691,8 +692,8 @@ fn test_u128_from_u8() { let u8_1: u8 = 0u8; let u8_2: u8 = 255u8; - let u128_1 = U128::from(u8_1); - let u128_2 = U128::from(u8_2); + 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); @@ -703,8 +704,8 @@ fn test_u128_from_u16() { let u16_1: u16 = 0u16; let u16_2: u16 = 65535u16; - let u128_1 = U128::from(u16_1); - let u128_2 = U128::from(u16_2); + 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); @@ -715,8 +716,8 @@ fn test_u128_from_u32() { let u32_1: u32 = 0u32; let u32_2: u32 = 4294967295u32; - let u128_1 = U128::from(u32_1); - let u128_2 = U128::from(u32_2); + 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); @@ -727,8 +728,8 @@ fn test_u128_from_u64() { let u64_1: u64 = 0u64; let u64_2: u64 = 18446744073709551615u64; - let u128_1 = U128::from(u64_1); - let u128_2 = U128::from(u64_2); + 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); From 6d5d56151b0f3503acce5cd673882a5ce6f753c8 Mon Sep 17 00:00:00 2001 From: SwayStar123 <46050679+SwayStar123@users.noreply.github.com> Date: Thu, 18 Apr 2024 18:39:33 +0530 Subject: [PATCH 3/7] Update sway-lib-std/src/u128.sw Co-authored-by: Cameron Carstens --- sway-lib-std/src/u128.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index ece9ce73063..4f85e76fc90 100644 --- a/sway-lib-std/src/u128.sw +++ b/sway-lib-std/src/u128.sw @@ -21,7 +21,7 @@ pub struct U128 { } impl From for U128 { - /// Casts a `u8` to a `U128`. + /// Converts a `u8` to a `U128`. /// /// # Returns /// From 3543adb302d19cc18bcc14ec3a1e1de66d5b262f Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Thu, 18 Apr 2024 18:44:57 +0530 Subject: [PATCH 4/7] add imports and capitalize U128 in docs --- sway-lib-std/src/u128.sw | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index 4f85e76fc90..1a6a4d89ac1 100644 --- a/sway-lib-std/src/u128.sw +++ b/sway-lib-std/src/u128.sw @@ -30,9 +30,10 @@ impl From for U128 { /// # Examples /// /// ```sway + /// use std::u128::U128; /// /// fn foo() { - /// let u128_value = u128::from(0u8); + /// let u128_value = U128::from(0u8); /// } /// ``` fn from(val: u8) -> Self { @@ -53,9 +54,10 @@ impl From for U128 { /// # Examples /// /// ```sway + /// use std::u128::U128; /// /// fn foo() { - /// let u128_value = u128::from(0u16); + /// let u128_value = U128::from(0u16); /// } /// ``` fn from(val: u16) -> Self { @@ -76,9 +78,10 @@ impl From for U128 { /// # Examples /// /// ```sway + /// use std::u128::U128; /// /// fn foo() { - /// let u128_value = u128::from(0u32); + /// let u128_value = U128::from(0u32); /// } /// ``` fn from(val: u32) -> Self { @@ -99,9 +102,10 @@ impl From for U128 { /// # Examples /// /// ```sway + /// use std::u128::U128; /// /// fn foo() { - /// let u128_value = u128::from(0u64); + /// let u128_value = U128::from(0u64); /// } /// ``` fn from(val: u64) -> Self { From ffdae6e068743935a79208dfb1a914519bec269e Mon Sep 17 00:00:00 2001 From: SwayStar123 <46050679+SwayStar123@users.noreply.github.com> Date: Fri, 19 Apr 2024 10:17:01 +0530 Subject: [PATCH 5/7] Update sway-lib-std/src/u128.sw Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com> --- sway-lib-std/src/u128.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index 1a6a4d89ac1..d845befb95c 100644 --- a/sway-lib-std/src/u128.sw +++ b/sway-lib-std/src/u128.sw @@ -93,7 +93,7 @@ impl From for U128 { } impl From for U128 { - /// Casts a `u64` to a `U128`. + /// Converts a `u64` to a `U128`. /// /// # Returns /// From c227befe7d106a22f52086d17b68c51d8c59efce Mon Sep 17 00:00:00 2001 From: SwayStar123 <46050679+SwayStar123@users.noreply.github.com> Date: Fri, 19 Apr 2024 10:17:06 +0530 Subject: [PATCH 6/7] Update sway-lib-std/src/u128.sw Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com> --- sway-lib-std/src/u128.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index d845befb95c..6af8464f399 100644 --- a/sway-lib-std/src/u128.sw +++ b/sway-lib-std/src/u128.sw @@ -69,7 +69,7 @@ impl From for U128 { } impl From for U128 { - /// Casts a `u32` to a `U128`. + /// Converts a `u32` to a `U128`. /// /// # Returns /// From 8eeb77610d2d6fd6b6ae821681c5ec7ccb3bc739 Mon Sep 17 00:00:00 2001 From: SwayStar123 <46050679+SwayStar123@users.noreply.github.com> Date: Fri, 19 Apr 2024 10:17:10 +0530 Subject: [PATCH 7/7] Update sway-lib-std/src/u128.sw Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com> --- sway-lib-std/src/u128.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index 6af8464f399..d5c1cca2fc3 100644 --- a/sway-lib-std/src/u128.sw +++ b/sway-lib-std/src/u128.sw @@ -45,7 +45,7 @@ impl From for U128 { } impl From for U128 { - /// Casts a `u16` to a `U128`. + /// Converts a `u16` to a `U128`. /// /// # Returns ///