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

Remove duplicate functions from conversions library #5800

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/converting_types/src/to_u16.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16> = u32_1.try_as_u16();
let u16_from_u32_1: Option<u16> = u32_1.try_into();
let u16_from_u32_2: Option<u16> = <u16 as TryFrom<u32>>::try_from(u32_1);

let u16_from_u64_1: Option<u16> = u64_1.try_as_u16();
let u16_from_u64_1: Option<u16> = u64_1.try_into();
let u16_from_u64_2: Option<u16> = <u16 as TryFrom<u64>>::try_from(u64_1);

let u16_from_u256: Option<u16> = <u16 as TryFrom<u256>>::try_from(u256_1);
Expand Down
2 changes: 1 addition & 1 deletion examples/converting_types/src/to_u32.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = u64_1.try_as_u32();
let u32_from_u64_1: Option<u32> = u64_1.try_into();
let u32_from_u64_2: Option<u32> = <u32 as TryFrom<u64>>::try_from(u64_1);

let u32_from_u256: Option<u32> = <u32 as TryFrom<u256>>::try_from(u256_1);
Expand Down
6 changes: 3 additions & 3 deletions examples/converting_types/src/to_u8.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = u16_1.try_as_u8();
let u8_from_u16_1: Option<u8> = u16_1.try_into();
let u8_from_u16_2: Option<u8> = <u8 as TryFrom<u16>>::try_from(u16_1);

let u8_from_u32_1: Option<u8> = u32_1.try_as_u8();
let u8_from_u32_1: Option<u8> = u32_1.try_into();
let u8_from_u32_2: Option<u8> = <u8 as TryFrom<u32>>::try_from(u32_1);

let u8_from_u64_1: Option<u8> = u64_1.try_as_u8();
let u8_from_u64_1: Option<u8> = u64_1.try_into();
let u8_from_u64_2: Option<u8> = <u8 as TryFrom<u64>>::try_from(u64_1);

let u8_from_u256: Option<u8> = <u8 as TryFrom<u256>>::try_from(u256_1);
Expand Down
12 changes: 0 additions & 12 deletions sway-lib-std/src/primitive_conversions/u16.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ use ::convert::{From, TryFrom};
use ::option::Option::{self, *};
use ::u128::U128;

impl u16 {
pub fn try_as_u8(self) -> Option<u8> {
if self <= u8::max().as_u16() {
Some(asm(input: self) {
input: u8
})
} else {
None
}
}
}

impl From<u8> for u16 {
/// Casts a `u8` to a `u16`.
///
Expand Down
22 changes: 0 additions & 22 deletions sway-lib-std/src/primitive_conversions/u32.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,6 @@ use ::convert::{From, TryFrom};
use ::option::Option::{self, *};
use ::u128::U128;

impl u32 {
pub fn try_as_u8(self) -> Option<u8> {
if self <= u8::max().as_u32() {
Some(asm(input: self) {
input: u8
})
} else {
None
}
}

pub fn try_as_u16(self) -> Option<u16> {
if self <= u16::max().as_u32() {
Some(asm(input: self) {
input: u16
})
} else {
None
}
}
}

impl From<u8> for u32 {
/// Casts a `u8` to a `u32`.
///
Expand Down
32 changes: 0 additions & 32 deletions sway-lib-std/src/primitive_conversions/u64.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,6 @@ use ::convert::{TryFrom, TryInto, *};
use ::option::Option::{self, *};
use ::u128::U128;

impl u64 {
pub fn try_as_u8(self) -> Option<u8> {
if self <= u8::max().as_u64() {
Some(asm(input: self) {
input: u8
})
} else {
None
}
}

pub fn try_as_u16(self) -> Option<u16> {
if self <= u16::max().as_u64() {
Some(asm(input: self) {
input: u16
})
} else {
None
}
}

pub fn try_as_u32(self) -> Option<u32> {
if self <= u32::max().as_u64() {
Some(asm(input: self) {
input: u32
})
} else {
None
}
}
}

impl From<u8> for u64 {
/// Casts a `u8` to a `u64`.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,25 @@ 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<u8> = Option::Some(1);
let x7: Option<u8> = OptionAlias::Some(1);
let x8: OptionAlias<u8> = Option::Some(1);

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<u16> = 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<u32> = Option::Some(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ impl U128Duplicate {
// Multiply two u64 values, producing a U128
pub fn mul64(a: u64, b: u64) -> U128Duplicate {
// 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();
Expand All @@ -110,9 +110,9 @@ pub fn mul64(a: u64, b: u64) -> U128Duplicate {
// 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();

Expand Down
6 changes: 3 additions & 3 deletions test/src/sdk-harness/test_projects/asset_ops/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ impl TestFuelCoin for Contract {
fn send_message(recipient: b256, msg_data: Vec<u64>, 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);
Expand Down
Loading