Skip to content

Commit

Permalink
Use sway-lib-core functionality in TryFrom implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
bitzoic committed Sep 20, 2023
1 parent bbeb456 commit 59bb5de
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion sway-lib-std/src/storage/storage_api.sw
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn slot_calculator<T>(slot: b256, offset: u64) -> (b256, u64, u64) {

// TODO: Update when u256 <-> b256 conversions exist.
// Determine which starting slot `T` will be stored based on the offset.
let mut u256_slot = asm(r1: slot) {r1: u256};
let mut u256_slot = slot.as_u256();
let u256_increment = asm(r1: (0, 0, 0, last_slot - number_of_slots)) { r1: u256 };
u256_slot += u256_increment;
let offset_slot = asm(r1: u256_slot) { r1: b256 };
Expand Down
22 changes: 11 additions & 11 deletions sway-lib-std/src/u16.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ use ::option::Option;

impl TryFrom<u32> for u16 {
fn try_from(u: u32) -> Option<Self> {
if u > asm(r1: u16::max()) { r1: u32 } {
return Option::None
if u > u16::max().as_u32() {
Option::None
} else {
return Option::Some(asm(r1: u) {r1: u16})
Option::Some(asm(r1: u) {r1: u16})
}
}
}

impl TryFrom<u64> for u16 {
fn try_from(u: u64) -> Option<Self> {
if u > asm(r1: u16::max()) { r1: u64 } {
return Option::None
if u > u16::max().as_u64() {
Option::None
} else {
return Option::Some(asm(r1: u) {r1: u16})
Option::Some(asm(r1: u) {r1: u16})
}
}
}
Expand All @@ -27,10 +27,10 @@ impl TryFrom<u256> for u16 {
fn try_from(u: u256) -> Option<Self> {
let parts = asm(r1: u) { r1: (u64, u64, u64, u64) };

if parts.0 != 0 || parts.1 != 0 || parts.2 != 0 || parts.3 > asm(r1: u16::max()) { r1: u64 } {
return Option::None
if parts.0 != 0 || parts.1 != 0 || parts.2 != 0 || parts.3 > u16::max().as_u64() {
Option::None
} else {
return Option::Some(asm(r1: parts.3) {r1: u16})
Option::Some(asm(r1: parts.3) {r1: u16})
}
}
}
Expand All @@ -40,7 +40,7 @@ fn test_u16_try_from_u32() {
use ::assert::assert;

let u32_1: u32 = 2u32;
let u32_2: u32 = asm(r1: u16::max()) { r1: u32 } + 1;
let u32_2: u32 = u16::max().as_u32() + 1;

let u16_1 = <u16 as TryFrom<u32>>::try_from(u32_1);
let u16_2 = <u16 as TryFrom<u32>>::try_from(u32_2);
Expand All @@ -56,7 +56,7 @@ fn test_u16_try_from_u64() {
use ::assert::assert;

let u64_1: u64 = 2;
let u64_2: u64 = asm(r1: u16::max()) { r1: u64 } + 1;
let u64_2: u64 = u16::max().as_u64() + 1;

let u16_1 = <u16 as TryFrom<u64>>::try_from(u64_1);
let u16_2 = <u16 as TryFrom<u64>>::try_from(u64_2);
Expand Down
14 changes: 7 additions & 7 deletions sway-lib-std/src/u32.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use ::option::Option;

impl TryFrom<u64> for u32 {
fn try_from(u: u64) -> Option<Self> {
if u > asm(r1: u32::max()) { r1: u64 } {
return Option::None
if u > u32::max().as_u64() {
Option::None
} else {
return Option::Some(asm(r1: u) {r1: u32})
Option::Some(asm(r1: u) {r1: u32})
}
}
}
Expand All @@ -17,10 +17,10 @@ impl TryFrom<u256> for u32 {
fn try_from(u: u256) -> Option<Self> {
let parts = asm(r1: u) { r1: (u64, u64, u64, u64) };

if parts.0 != 0 || parts.1 != 0 || parts.2 != 0 || parts.3 > asm(r1: u32::max()) { r1: u64 } {
return Option::None
if parts.0 != 0 || parts.1 != 0 || parts.2 != 0 || parts.3 > u32::max().as_u64() {
Option::None
} else {
return Option::Some(asm(r1: parts.3) {r1: u32})
Option::Some(asm(r1: parts.3) {r1: u32})
}
}
}
Expand All @@ -30,7 +30,7 @@ fn test_u32_try_from_u64() {
use ::assert::assert;

let u64_1: u64 = 2;
let u64_2: u64 = asm(r1: u32::max()) { r1: u64 } + 1;
let u64_2: u64 = u32::max().as_u64() + 1;

let u32_1 = <u32 as TryFrom<u64>>::try_from(u64_1);
let u32_2 = <u32 as TryFrom<u64>>::try_from(u64_2);
Expand Down
4 changes: 2 additions & 2 deletions sway-lib-std/src/u64.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ impl TryFrom<u256> for u64 {
let parts = asm(r1: u) { r1: (u64, u64, u64, u64) };

if parts.0 != 0 || parts.1 != 0 || parts.2 != 0 {
return Option::None
Option::None
} else {
return Option::Some(parts.3)
Option::Some(parts.3)
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions sway-lib-std/src/u8.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ use ::option::Option;

impl TryFrom<u16> for u8 {
fn try_from(u: u16) -> Option<Self> {
if u > asm(r1: u8::max()) { r1: u16 } {
return Option::None
if u > u8::max().as_u16() {
Option::None
} else {
return Option::Some(asm(r1: u) {r1: u8})
Option::Some(asm(r1: u) {r1: u8})
}
}
}

impl TryFrom<u32> for u8 {
fn try_from(u: u32) -> Option<Self> {
if u > asm(r1: u8::max()) { r1: u32 } {
return Option::None
if u > u8::max().as_u32() {
Option::None
} else {
return Option::Some(asm(r1: u) {r1: u8})
Option::Some(asm(r1: u) {r1: u8})
}
}
}

impl TryFrom<u64> for u8 {
fn try_from(u: u64) -> Option<Self> {
if u > asm(r1: u8::max()) { r1: u64 } {
return Option::None
if u > u8::max().as_u64() {
Option::None
} else {
return Option::Some(asm(r1: u) {r1: u8})
Option::Some(asm(r1: u) {r1: u8})
}
}
}
Expand All @@ -37,10 +37,10 @@ impl TryFrom<u256> for u8 {
fn try_from(u: u256) -> Option<Self> {
let parts = asm(r1: u) { r1: (u64, u64, u64, u64) };

if parts.0 != 0 || parts.1 != 0 || parts.2 != 0 || parts.3 > asm(r1: u8::max()) { r1: u64 } {
return Option::None
if parts.0 != 0 || parts.1 != 0 || parts.2 != 0 || parts.3 > u8::max().as_u64() {
Option::None
} else {
return Option::Some(asm(r1: parts.3) {r1: u8})
Option::Some(asm(r1: parts.3) {r1: u8})
}
}
}
Expand All @@ -50,7 +50,7 @@ fn test_u8_try_from_u16() {
use ::assert::assert;

let u16_1: u16 = 2u16;
let u16_2: u16 = asm(r1: u8::max()) { r1: u16 } + 1;
let u16_2: u16 = u8::max().as_u16() + 1;

let u8_1 = <u8 as TryFrom<u16>>::try_from(u16_1);
let u8_2 = <u8 as TryFrom<u16>>::try_from(u16_2);
Expand All @@ -66,7 +66,7 @@ fn test_u8_try_from_u32() {
use ::assert::assert;

let u32_1: u32 = 2u32;
let u32_2: u32 = asm(r1: u16::max()) { r1: u32 } + 1;
let u32_2: u32 = u16::max().as_u32() + 1;

let u8_1 = <u8 as TryFrom<u32>>::try_from(u32_1);
let u8_2 = <u8 as TryFrom<u32>>::try_from(u32_2);
Expand All @@ -82,7 +82,7 @@ fn test_u8_try_from_u64() {
use ::assert::assert;

let u64_1: u64 = 2;
let u64_2: u64 = asm(r1: u16::max()) { r1: u64 } + 1;
let u64_2: u64 = u16::max().as_u64() + 1;

let u8_1 = <u8 as TryFrom<u64>>::try_from(u64_1);
let u8_2 = <u8 as TryFrom<u64>>::try_from(u64_2);
Expand Down

0 comments on commit 59bb5de

Please sign in to comment.