From e6f0b6dccec8b45f03f4743ecddca9572623fb0b Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Fri, 29 Mar 2024 16:41:41 +0530 Subject: [PATCH 1/2] remove functions --- sway-lib-std/src/primitive_conversions/u16.sw | 12 ------- sway-lib-std/src/primitive_conversions/u32.sw | 22 ------------- sway-lib-std/src/primitive_conversions/u64.sw | 32 ------------------- 3 files changed, 66 deletions(-) diff --git a/sway-lib-std/src/primitive_conversions/u16.sw b/sway-lib-std/src/primitive_conversions/u16.sw index f1524b13f6f..7ca9ba1bcd6 100644 --- a/sway-lib-std/src/primitive_conversions/u16.sw +++ b/sway-lib-std/src/primitive_conversions/u16.sw @@ -3,18 +3,6 @@ library; use ::convert::TryFrom; use ::option::Option::{self, *}; -impl u16 { - pub fn try_as_u8(self) -> Option { - if self <= u8::max().as_u16() { - Some(asm(input: self) { - input: u8 - }) - } else { - None - } - } -} - impl TryFrom for u16 { fn try_from(u: u32) -> Option { if u > u16::max().as_u32() { diff --git a/sway-lib-std/src/primitive_conversions/u32.sw b/sway-lib-std/src/primitive_conversions/u32.sw index 0a44d8e3090..4294e43afb3 100644 --- a/sway-lib-std/src/primitive_conversions/u32.sw +++ b/sway-lib-std/src/primitive_conversions/u32.sw @@ -3,28 +3,6 @@ library; use ::convert::TryFrom; use ::option::Option::{self, *}; -impl u32 { - pub fn try_as_u8(self) -> Option { - if self <= u8::max().as_u32() { - Some(asm(input: self) { - input: u8 - }) - } else { - None - } - } - - pub fn try_as_u16(self) -> Option { - if self <= u16::max().as_u32() { - Some(asm(input: self) { - input: u16 - }) - } else { - None - } - } -} - impl TryFrom for u32 { fn try_from(u: u64) -> Option { if u > u32::max().as_u64() { diff --git a/sway-lib-std/src/primitive_conversions/u64.sw b/sway-lib-std/src/primitive_conversions/u64.sw index b4a400c1bd1..46cd3274430 100644 --- a/sway-lib-std/src/primitive_conversions/u64.sw +++ b/sway-lib-std/src/primitive_conversions/u64.sw @@ -3,38 +3,6 @@ library; use ::convert::{TryFrom, TryInto, *}; use ::option::Option::{self, *}; -impl u64 { - pub fn try_as_u8(self) -> Option { - if self <= u8::max().as_u64() { - Some(asm(input: self) { - input: u8 - }) - } else { - None - } - } - - pub fn try_as_u16(self) -> Option { - if self <= u16::max().as_u64() { - Some(asm(input: self) { - input: u16 - }) - } else { - None - } - } - - pub fn try_as_u32(self) -> Option { - if self <= u32::max().as_u64() { - Some(asm(input: self) { - input: u32 - }) - } else { - None - } - } -} - impl TryFrom for u64 { fn try_from(u: u256) -> Option { let parts = asm(r1: u) { From 3b72fb70fa685a814e21d26b0c23e9a44fd6ebb0 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Fri, 29 Mar 2024 16:51:58 +0530 Subject: [PATCH 2/2] change tests to use try_into --- examples/converting_types/src/to_u16.sw | 4 ++-- examples/converting_types/src/to_u32.sw | 2 +- examples/converting_types/src/to_u8.sw | 6 +++--- .../language/integer_type_inference/src/main.sw | 12 ++++++------ .../test_contracts/issue_1512_repro/src/main.sw | 14 +++++++------- .../test_projects/asset_ops/src/main.sw | 6 +++--- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/examples/converting_types/src/to_u16.sw b/examples/converting_types/src/to_u16.sw index 69d86a2d7d4..4e5a330b4c4 100644 --- a/examples/converting_types/src/to_u16.sw +++ b/examples/converting_types/src/to_u16.sw @@ -14,10 +14,10 @@ pub fn convert_uint_to_u16() { let u16_from_u8: u16 = u8_1.as_u16(); - let u16_from_u32_1: Option = u32_1.try_as_u16(); + let u16_from_u32_1: Option = u32_1.try_into(); let u16_from_u32_2: Option = >::try_from(u32_1); - let u16_from_u64_1: Option = u64_1.try_as_u16(); + let u16_from_u64_1: Option = u64_1.try_into(); let u16_from_u64_2: Option = >::try_from(u64_1); let u16_from_u256: Option = >::try_from(u256_1); diff --git a/examples/converting_types/src/to_u32.sw b/examples/converting_types/src/to_u32.sw index dfa2d35fa08..f7c86994c00 100644 --- a/examples/converting_types/src/to_u32.sw +++ b/examples/converting_types/src/to_u32.sw @@ -16,7 +16,7 @@ pub fn convert_uint_to_u32() { let u32_from_u16: u32 = u16_1.as_u32(); - let u32_from_u64_1: Option = u64_1.try_as_u32(); + let u32_from_u64_1: Option = u64_1.try_into(); let u32_from_u64_2: Option = >::try_from(u64_1); let u32_from_u256: Option = >::try_from(u256_1); diff --git a/examples/converting_types/src/to_u8.sw b/examples/converting_types/src/to_u8.sw index 1c320a45d73..0b98733d4d7 100644 --- a/examples/converting_types/src/to_u8.sw +++ b/examples/converting_types/src/to_u8.sw @@ -12,13 +12,13 @@ pub fn convert_uint_to_u8() { let u64_1: u64 = 2; let u256_1: u256 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; - let u8_from_u16_1: Option = u16_1.try_as_u8(); + let u8_from_u16_1: Option = u16_1.try_into(); let u8_from_u16_2: Option = >::try_from(u16_1); - let u8_from_u32_1: Option = u32_1.try_as_u8(); + let u8_from_u32_1: Option = u32_1.try_into(); let u8_from_u32_2: Option = >::try_from(u32_1); - let u8_from_u64_1: Option = u64_1.try_as_u8(); + let u8_from_u64_1: Option = u64_1.try_into(); let u8_from_u64_2: Option = >::try_from(u64_1); let u8_from_u256: Option = >::try_from(u256_1); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/integer_type_inference/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/integer_type_inference/src/main.sw index f1d0fe1823b..485340346d6 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/integer_type_inference/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/integer_type_inference/src/main.sw @@ -79,9 +79,9 @@ fn main() { /* Make sure that the resulting types of variables are correct */ let x1: u8 = 4u8; - let x2: u8 = 4u16.try_as_u8().unwrap(); - let x3: u8 = 4u32.try_as_u8().unwrap(); - let x4: u8 = 4u64.try_as_u8().unwrap(); + let x2: u8 = 4u16.try_into().unwrap(); + let x3: u8 = 4u32.try_into().unwrap(); + let x4: u8 = 4u64.try_into().unwrap(); let x5: u8 = 4; let x6: Option = Option::Some(1); let x7: Option = OptionAlias::Some(1); @@ -89,15 +89,15 @@ fn main() { let y1: u16 = 4u8.as_u16(); let y2: u16 = 4u16; - let y3: u16 = 4u32.try_as_u16().unwrap(); - let y4: u16 = 4u64.try_as_u16().unwrap(); + let y3: u16 = 4u32.try_into().unwrap(); + let y4: u16 = 4u64.try_into().unwrap(); let y5: u16 = 4; let y6: Option = Option::Some(1); let z1: u32 = 4u8.as_u32(); let z2: u32 = 4u16.as_u32(); let z3: u32 = 4u32; - let z4: u32 = 4u64.try_as_u32().unwrap(); + let z4: u32 = 4u64.try_into().unwrap(); let z5: u32 = 4; let z6: Option = Option::Some(1); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/issue_1512_repro/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/issue_1512_repro/src/main.sw index ea39bf95f0f..4d3216f281b 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/issue_1512_repro/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/issue_1512_repro/src/main.sw @@ -96,10 +96,10 @@ impl U128 { // Multiply two u64 values, producing a U128 pub fn mul64(a: u64, b: u64) -> U128 { // Split a and b into 32-bit lo and hi components - let a_lo = (a & 0x00000000ffffffff).try_as_u32().unwrap(); - let a_hi = (a >> 32).try_as_u32().unwrap(); - let b_lo = (b & 0x00000000ffffffff).try_as_u32().unwrap(); - let b_hi = (b >> 32).try_as_u32().unwrap(); + let a_lo: u32 = (a & 0x00000000ffffffff).try_into().unwrap(); + let a_hi: u32 = (a >> 32).try_into().unwrap(); + let b_lo: u32 = (b & 0x00000000ffffffff).try_into().unwrap(); + let b_hi: u32 = (b >> 32).try_into().unwrap(); // Calculate low, high, and mid multiplications let ab_hi = (a_hi * b_hi).as_u64(); @@ -110,9 +110,9 @@ pub fn mul64(a: u64, b: u64) -> U128 { // Calculate the carry bit let carry_bit = ( ( - ab_mid.try_as_u32().unwrap() + - ba_mid.try_as_u32().unwrap() + - (ab_lo >> 32).try_as_u32().unwrap() + ab_mid.try_into().unwrap() + + ba_mid.try_into().unwrap() + + (ab_lo >> 32).try_into().unwrap() ) >> 32 ).as_u64(); diff --git a/test/src/sdk-harness/test_projects/asset_ops/src/main.sw b/test/src/sdk-harness/test_projects/asset_ops/src/main.sw index 8e82076cc86..450a3399c19 100644 --- a/test/src/sdk-harness/test_projects/asset_ops/src/main.sw +++ b/test/src/sdk-harness/test_projects/asset_ops/src/main.sw @@ -59,9 +59,9 @@ impl TestFuelCoin for Contract { fn send_message(recipient: b256, msg_data: Vec, coins: u64) { let mut data = Bytes::new(); if msg_data.len() > 0 { - data.push(msg_data.get(0).unwrap().try_as_u8().unwrap()); - data.push(msg_data.get(1).unwrap().try_as_u8().unwrap()); - data.push(msg_data.get(2).unwrap().try_as_u8().unwrap()); + data.push(msg_data.get(0).unwrap().try_into().unwrap()); + data.push(msg_data.get(1).unwrap().try_into().unwrap()); + data.push(msg_data.get(2).unwrap().try_into().unwrap()); } send_message(recipient, data, coins);