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

Implements the Eq trait for Option #5302

Merged
merged 22 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
98456fc
Placeholder for Eq trait
brandonsurh Nov 23, 2023
5826b40
trait implementation
brandonsurh Nov 24, 2023
9085e50
Changed implementation
brandonsurh Nov 25, 2023
288ebfe
Added test
brandonsurh Nov 25, 2023
cd49cc5
Expanded on tests
brandonsurh Nov 26, 2023
51f2450
Merge branch 'FuelLabs:master' into option_eq_trait
brandonsurh Nov 26, 2023
65a7979
Merge branch 'option_eq_trait' of https://github.com/brandonsurh/sway…
brandonsurh Nov 26, 2023
bb44209
Update test/src/e2e_vm_tests/test_programs/should_pass/stdlib/option_…
brandonsurh Nov 28, 2023
4c67a8d
Reimplemented tests for more cases and formatting
brandonsurh Nov 28, 2023
5428310
Merge branch 'option_eq_trait' of https://github.com/brandonsurh/sway…
brandonsurh Nov 28, 2023
44f38d8
Rearranged tests as they appear in the Sway Book and added types
brandonsurh Nov 28, 2023
e8a54a9
Removed whitespace
brandonsurh Nov 28, 2023
8aeea2a
Removed fixed length string tests
brandonsurh Nov 29, 2023
a02b126
Changed contract address
brandonsurh Nov 29, 2023
7967aec
Merge branch 'option_eq_trait' of https://github.com/brandonsurh/sway…
brandonsurh Nov 29, 2023
55557cb
Changed wrong file's contract address bc sleep deprived - should be g…
brandonsurh Nov 29, 2023
d84e8d7
Changed address in call_basic_storage
brandonsurh Nov 30, 2023
4c6693c
Merge branch 'master' into option_eq_trait
bitzoic Nov 30, 2023
0207268
Added tests for string array, array, struct, tuple, enum
brandonsurh Nov 30, 2023
8df4aed
Merge branch 'master' into option_eq_trait
brandonsurh Dec 1, 2023
df22a9c
Merge branch 'master' into option_eq_trait
brandonsurh Dec 1, 2023
d608a97
Merge branch 'master' into option_eq_trait
brandonsurh Dec 3, 2023
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
10 changes: 10 additions & 0 deletions sway-lib-std/src/option.sw
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ pub enum Option<T> {
}
// ANCHOR_END: docs_option

impl<T> core::ops::Eq for Option<T> where T: Eq {
fn eq(self, other: Self) -> bool {
match (self, other) {
(Option::Some(a), Option::Some(b)) => a == b,
(Option::None, Option::None) => true,
_ => false,
}
}
}

// Type implementation
//
impl<T> Option<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ script;
use basic_storage_abi::{BasicStorage, Quad};

fn main() -> u64 {
let addr = abi(BasicStorage, 0xb0d36fd04e733889ce2b24307a00ec871314ed54964404d441aafb376eb01e7d);
let addr = abi(BasicStorage, 0xe683b03352239f4b23986cd283948b7dfc22b528ace56859e562dff5a0165dc8);
let key = 0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
let value = 4242;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ script;
use storage_enum_abi::*;

fn main() -> u64 {
let contract_id = 0x8836ec87c97907ff0f4cf0cf62f852c7f654b8a6bd6845ee2d0203c5dbd029a5;
let contract_id = 0xe2f370fb01c8c36a521093494a023ce2d7232398e5e88722164c42ec0303f381;
let caller = abi(StorageEnum, contract_id);

let res = caller.read_write_enums();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-9E5F9380FAC5610F'

[[package]]
name = 'option_eq'
source = 'member'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-9E5F9380FAC5610F'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "option_eq"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"configurables": [],
"functions": [
{
"attributes": null,
"inputs": [],
"name": "main",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
],
"loggedTypes": [],
"messagesTypes": [],
"types": [
{
"components": null,
"type": "bool",
"typeId": 0,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
script;

fn main() -> bool {
// Test with u8
let u8_option1 = Option::<u8>::Some(10);
let u8_option2 = Option::<u8>::Some(10);
let u8_option3 = Option::<u8>::Some(20);
let u8_none_option: Option<u8> = Option::None;

// Eq tests
assert(u8_option1 == u8_option1);
assert(u8_option1 == u8_option2);

// Neq tests
assert(u8_option1 != u8_option3);
assert(u8_option1 != u8_none_option);

// None tests
assert(u8_none_option == Option::None);
assert(Option::<u8>::None == u8_none_option);

// Test with u16
let u16_option1 = Option::<u16>::Some(10);
let u16_option2 = Option::<u16>::Some(10);
let u16_option3 = Option::<u16>::Some(20);
let u16_none_option: Option<u16> = Option::None;

// Eq tests
assert(u16_option1 == u16_option1);
assert(u16_option1 == u16_option2);

// Neq tests
assert(u16_option1 != u16_option3);
assert(u16_option1 != u16_none_option);

// None tests
assert(u16_none_option == Option::None);
assert(Option::<u16>::None == u16_none_option);

// Test with u32
let u32_option1 = Option::<u32>::Some(10);
let u32_option2 = Option::<u32>::Some(10);
let u32_option3 = Option::<u32>::Some(20);
let u32_none_option: Option<u32> = Option::None;

// Eq tests
assert(u32_option1 == u32_option1);
assert(u32_option1 == u32_option2);

// Neq tests
assert(u32_option1 != u32_option3);
assert(u32_option1 != u32_none_option);

// None tests
assert(u32_none_option == Option::None);
assert(Option::<u32>::None == u32_none_option);

// Test with u64
let u64_option1 = Option::<u64>::Some(10);
let u64_option2 = Option::<u64>::Some(10);
let u64_option3 = Option::<u64>::Some(20);
let u64_none_option: Option<u64> = Option::None;

// Eq tests
assert(u64_option1 == u64_option1);
assert(u64_option1 == u64_option2);

// Neq tests
assert(u64_option1 != u64_option3);
assert(u64_option1 != u64_none_option);

// None tests
assert(u64_none_option == Option::None);
assert(Option::<u64>::None == u64_none_option);

// Test with u256
let u256_option1 = Option::<u256>::Some(10);
let u256_option2 = Option::<u256>::Some(10);
let u256_option3 = Option::<u256>::Some(20);
let u256_none_option: Option<u256> = Option::None;

// Eq tests
assert(u256_option1 == u256_option1);
assert(u256_option1 == u256_option2);

// Neq tests
assert(u256_option1 != u256_option3);
assert(u256_option1 != u256_none_option);

// None tests
assert(u256_none_option == Option::None);
assert(Option::<u256>::None == u256_none_option);

// Test with str
let str_option1 = Option::<str>::Some("fuel");
let str_option2 = Option::<str>::Some("fuel");
let str_option3 = Option::<str>::Some("sway");
let str_none_option: Option<str> = Option::None;

// Eq tests
assert(str_option1 == str_option1);
assert(str_option1 == str_option2);

// Neq tests
assert(str_option1 != str_option3);
assert(str_option1 != str_none_option);

// None tests
assert(str_none_option == Option::None);
assert(Option::<str>::None == str_none_option);

// Test with bool
let bool_option1 = Option::Some(true);
let bool_option2 = Option::Some(true);
let bool_option3 = Option::Some(false);
let bool_none_option: Option<bool> = Option::None;

// Eq tests
assert(bool_option1 == bool_option1);
assert(bool_option1 == bool_option2);

// Neq tests
assert(bool_option1 != bool_option3);
assert(bool_option1 != bool_none_option);

// None tests
assert(bool_none_option == Option::None);
assert(Option::<bool>::None == bool_none_option);

// Test with b256
let b256_option1 = Option::<b256>::Some(0x0000000000000000000000000000000000000000000000000000000000000001);
let b256_option2 = Option::<b256>::Some(0x0000000000000000000000000000000000000000000000000000000000000001);
let b256_option3 = Option::<b256>::Some(0x0000000000000000000000000000000000000000000000000000000000000002);
let b256_none_option: Option<b256> = Option::None;

// Eq tests
assert(b256_option1 == b256_option1);
assert(b256_option1 == b256_option2);

// Neq tests
assert(b256_option1 != b256_option3);
assert(b256_option1 != b256_none_option);

// None tests
assert(b256_none_option == Option::None);
assert(Option::<b256>::None == b256_none_option);

true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "return", value = 1 }
validate_abi = true
Loading